Skip to content

Commit 3869efe

Browse files
authored
Use mimalloc to improve performance and reduce memory allocation lock contention (#6)
* Add mimalloc submodule and update build script * Add steps to build and test with UseMimalloc * Update mimalloc submodule * Change UseMimalloc parameter type to switch * Add ExpectMimalloc parameter * Fetch mimalloc submodule and use mimalloc parameters * Prevent MIMALLOC_VERBOSE aborting tests * Uninstall driver after tests; upload mimalloc artifacts
1 parent 4e1c125 commit 3869efe

File tree

8 files changed

+186
-39
lines changed

8 files changed

+186
-39
lines changed

.github/workflows/main.yml

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -184,54 +184,116 @@ jobs:
184184
sc config "postgresql-x64-14" start= auto
185185
- name: get psqlodbc
186186
uses: actions/checkout@v4
187+
with:
188+
submodules: true
187189
- name: 'setup msvc for psqlodbc'
188190
uses: TheMrMilchmann/setup-msvc-dev@v3
189191
with:
190192
arch: x86
191-
- name: build psqlodbc
193+
194+
- name: build psqlodbc standard
192195
shell: powershell
193196
run: |
194197
copy .github\workflows\configuration.xml winbuild
195198
winbuild\BuildAll.ps1
196199
installer\buildInstallers.ps1
197-
- name: test psqlodbc
200+
env:
201+
PSQLODBC_OBJBASE: ${{ github.workspace }}\winbuild\standard
202+
- name: test psqlodbc standard
198203
shell: powershell
199204
run: |
200205
winbuild\regress.ps1 -DsnInfo "SERVER=localhost|DATABASE=contrib_regression|PORT=5432|UID=postgres|PWD=password"
201-
- name: Upload x64 merge module
206+
standard\test_x86\RegisterRegdsn.exe uninstall_driver postgres_devw
207+
standard\test_x64\RegisterRegdsn.exe uninstall_driver postgres_devw
208+
env:
209+
PSQLODBC_OBJBASE: ${{ github.workspace }}\winbuild\standard
210+
211+
- name: build psqlodbc mimalloc
212+
shell: powershell
213+
run: |
214+
copy .github\workflows\configuration.xml winbuild
215+
winbuild\BuildAll.ps1 -UseMimalloc
216+
installer\buildInstallers.ps1
217+
env:
218+
PSQLODBC_OBJBASE: ${{ github.workspace }}\winbuild\mimalloc
219+
- name: test psqlodbc mimalloc
220+
shell: powershell
221+
run: |
222+
winbuild\regress.ps1 -DsnInfo "SERVER=localhost|DATABASE=contrib_regression|PORT=5432|UID=postgres|PWD=password" -ExpectMimalloc
223+
mimalloc\test_x86\RegisterRegdsn.exe uninstall_driver postgres_devw
224+
mimalloc\test_x64\RegisterRegdsn.exe uninstall_driver postgres_devw
225+
env:
226+
PSQLODBC_OBJBASE: ${{ github.workspace }}\winbuild\mimalloc
227+
228+
- name: Upload standard x64 merge module
229+
uses: actions/upload-artifact@v4
230+
with:
231+
name: psqlODBC Standard x64 Merge Module
232+
path: winbuild/standard/installer/x64/*.msm
233+
retention-days: 5
234+
if-no-files-found: error
235+
- name: Upload standard x64 installer package
236+
uses: actions/upload-artifact@v4
237+
with:
238+
name: psqlODBC Standard x64 Installer
239+
path: winbuild/standard/installer/x64/*.msi
240+
retention-days: 5
241+
if-no-files-found: error
242+
- name: Upload standard x86 merge module
243+
uses: actions/upload-artifact@v4
244+
with:
245+
name: psqlODBC Standard x86 Merge Module
246+
path: winbuild/standard/installer/x86/*.msm
247+
retention-days: 5
248+
if-no-files-found: error
249+
- name: Upload standard x86 installer package
250+
uses: actions/upload-artifact@v4
251+
with:
252+
name: psqlODBC Standard x86 Installer
253+
path: winbuild/standard/installer/x86/*.msi
254+
retention-days: 5
255+
if-no-files-found: error
256+
- name: Upload standard x64 setup
257+
uses: actions/upload-artifact@v4
258+
with:
259+
name: psqlODBC Standard x64 Setup
260+
path: winbuild/standard/installer/psqlodbc-setup/bin/Release/psqlodbc-setup.exe
261+
retention-days: 5
262+
if-no-files-found: error
263+
264+
- name: Upload mimalloc x64 merge module
202265
uses: actions/upload-artifact@v4
203266
with:
204-
name: psqlODBC x64 Merge Module
205-
path: ./installer/x64/*.msm
267+
name: psqlODBC mimalloc x64 Merge Module
268+
path: winbuild/mimalloc/installer/x64/*.msm
206269
retention-days: 5
207270
if-no-files-found: error
208-
- name: Upload x64 installer package
271+
- name: Upload mimalloc x64 installer package
209272
uses: actions/upload-artifact@v4
210273
with:
211-
name: psqlODBC x64 Installer
212-
path: ./installer/x64/*.msi
274+
name: psqlODBC mimalloc x64 Installer
275+
path: winbuild/mimalloc/installer/x64/*.msi
213276
retention-days: 5
214277
if-no-files-found: error
215-
- name: Upload x86 merge module
278+
- name: Upload mimalloc x86 merge module
216279
uses: actions/upload-artifact@v4
217280
with:
218-
name: psqlODBC x86 Merge Module
219-
path: ./installer/x86/*.msm
281+
name: psqlODBC mimalloc x86 Merge Module
282+
path: winbuild/mimalloc/installer/x86/*.msm
220283
retention-days: 5
221284
if-no-files-found: error
222-
- name: Upload x86 installer package
285+
- name: Upload mimalloc x86 installer package
223286
uses: actions/upload-artifact@v4
224287
with:
225-
name: psqlODBC x86 Installer
226-
path: ./installer/x86/*.msi
288+
name: psqlODBC mimalloc x86 Installer
289+
path: winbuild/mimalloc/installer/x86/*.msi
227290
retention-days: 5
228291
if-no-files-found: error
229-
- name: Upload x64 setup
230-
id: x64_setup
292+
- name: Upload mimalloc x64 setup
231293
uses: actions/upload-artifact@v4
232294
with:
233-
name: psqlODBC x64 Setup
234-
path: ./installer/psqlodbc-setup/bin/Release/psqlodbc-setup.exe
295+
name: psqlODBC mimalloc x64 Setup
296+
path: winbuild/mimalloc/installer/psqlodbc-setup/bin/Release/psqlodbc-setup.exe
235297
retention-days: 5
236298
if-no-files-found: error
237299

@@ -243,4 +305,4 @@ jobs:
243305
draft: false
244306
prerelease: false
245307
token: ${{secrets.RELEASE_TOKEN}}
246-
artifacts: "./installer/x86/*.msi,./installer/x86/*.msm,./installer/psqlodbc-setup/bin/Release/psqlodbc-setup.exe"
308+
artifacts: "winbuild/standard/installer/x86/*.msi,winbuild/standard/installer/x86/*.msm,winbuild/standard/installer/psqlodbc-setup/bin/Release/psqlodbc-setup.exe"

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[submodule "libs/mimalloc"]
2+
path = libs/mimalloc
3+
url = https://github.com/microsoft/mimalloc
4+
branch = master

inouealc.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#undef _MEMORY_DEBUG_
22
#include "psqlodbc.h"
33

4+
#ifndef _MIMALLOC_
45
#ifdef WIN32
56
#ifdef _DEBUG
67
/* #include <stdlib.h> */
@@ -10,6 +11,7 @@
1011
#include <malloc.h>
1112
#endif /* _DEBUG */
1213
#endif /* WIN32 */
14+
#endif /* _MIMALLOC_ */
1315
#include <string.h>
1416

1517
#include "misc.h"
@@ -26,20 +28,20 @@ CSTR ALCERR = "alcerr";
2628
void * pgdebug_alloc(size_t size)
2729
{
2830
void * alloced;
29-
alloced = malloc(size);
31+
alloced = pg_malloc(size);
3032
MYLOG(2, " alloced=%p(" FORMAT_SIZE_T ")\n", alloced, size);
3133
if (alloced)
3234
{
3335
if (!alsize)
3436
{
3537
alsize = 100;
36-
altbl = (ALADR *) malloc(alsize * sizeof(ALADR));
38+
altbl = (ALADR *) pg_malloc(alsize * sizeof(ALADR));
3739
}
3840
else if (tbsize >= alsize)
3941
{
4042
ALADR *al;
4143
alsize *= 2;
42-
if (al = (ALADR *) realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
44+
if (al = (ALADR *) pg_realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
4345
return alloced;
4446
altbl = al;
4547
}
@@ -53,20 +55,20 @@ MYLOG(2, " alloced=%p(" FORMAT_SIZE_T ")\n", alloced, size);
5355
}
5456
void * pgdebug_calloc(size_t n, size_t size)
5557
{
56-
void * alloced = calloc(n, size);
58+
void * alloced = pg_calloc(n, size);
5759

5860
if (alloced)
5961
{
6062
if (!alsize)
6163
{
6264
alsize = 100;
63-
altbl = (ALADR *) malloc(alsize * sizeof(ALADR));
65+
altbl = (ALADR *) pg_malloc(alsize * sizeof(ALADR));
6466
}
6567
else if (tbsize >= alsize)
6668
{
6769
ALADR *al;
6870
alsize *= 2;
69-
if (al = (ALADR *) realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
71+
if (al = (ALADR *) pg_realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
7072
return alloced;
7173
altbl = al;
7274
}
@@ -85,7 +87,7 @@ void * pgdebug_realloc(void * ptr, size_t size)
8587

8688
if (!ptr)
8789
return pgdebug_alloc(size);
88-
alloced = realloc(ptr, size);
90+
alloced = pg_realloc(ptr, size);
8991
if (!alloced)
9092
{
9193
MYLOG(0, "%s %p error\n", ALCERR, ptr);
@@ -109,7 +111,7 @@ void * pgdebug_realloc(void * ptr, size_t size)
109111
}
110112
char * pgdebug_strdup(const char * ptr)
111113
{
112-
char * alloced = strdup(ptr);
114+
char * alloced = pg_strdup(ptr);
113115
if (!alloced)
114116
{
115117
MYLOG(0, "%s %p error\n", ALCERR, ptr);
@@ -119,13 +121,13 @@ char * pgdebug_strdup(const char * ptr)
119121
if (!alsize)
120122
{
121123
alsize = 100;
122-
altbl = (ALADR *) malloc(alsize * sizeof(ALADR));
124+
altbl = (ALADR *) pg_malloc(alsize * sizeof(ALADR));
123125
}
124126
else if (tbsize >= alsize)
125127
{
126128
ALADR *al;
127129
alsize *= 2;
128-
if (al = (ALADR *) realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
130+
if (al = (ALADR *) pg_realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
129131
return alloced;
130132
altbl = al;
131133
}
@@ -168,7 +170,7 @@ void pgdebug_free(void * ptr)
168170
}
169171
else
170172
MYLOG(2, "ptr=%p\n", ptr);
171-
free(ptr);
173+
pg_free(ptr);
172174
}
173175

174176
static BOOL out_check(void *out, size_t len, const char *name)
@@ -253,7 +255,7 @@ void debug_memory_check(void)
253255
if (0 == tbsize)
254256
{
255257
MYLOG(0, "no memry leak found and max count allocated so far is %d\n", alsize);
256-
free(altbl);
258+
pg_free(altbl);
257259
alsize = 0;
258260
}
259261
else

libs/mimalloc

Submodule mimalloc added at 229ec9c

psqlodbc.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#endif
2525
#include "version.h"
2626

27+
#ifdef _MIMALLOC_
28+
#include <stdlib.h>
29+
#else /* _MIMALLOC_ */
2730
#ifdef WIN32
2831
#ifdef _DEBUG
2932
#ifndef _MEMORY_DEBUG_
@@ -41,6 +44,11 @@
4144
#else /* WIN32 */
4245
#include <stdlib.h>
4346
#endif /* WIN32 */
47+
#endif /* _MIMALLOC_ */
48+
49+
#ifdef WIN32
50+
#include <stdbool.h>
51+
#endif /* WIN32 */
4452

4553
#ifdef __INCLUDE_POSTGRES_FE_H__ /* currently not defined */
4654
/*
@@ -61,6 +69,21 @@
6169
#endif /* __GNUC__ || __IBMC__ */
6270
#endif /* __INCLUDE_POSTGRES_FE_H__ */
6371

72+
#ifdef _MIMALLOC_
73+
#include <mimalloc.h>
74+
#define pg_malloc mi_malloc
75+
#define pg_realloc mi_realloc
76+
#define pg_calloc mi_calloc
77+
#define pg_strdup mi_strdup
78+
#define pg_free mi_free
79+
#else /* _MIMALLOC_ */
80+
#define pg_malloc malloc
81+
#define pg_realloc realloc
82+
#define pg_calloc calloc
83+
#define pg_strdup _strdup
84+
#define pg_free free
85+
#endif /* _MIMALLOC_ */
86+
6487
#ifdef _MEMORY_DEBUG_
6588
void *pgdebug_alloc(size_t);
6689
void *pgdebug_calloc(size_t, size_t);
@@ -87,6 +110,15 @@ void debug_memory_check(void);
87110
/* #define strncpy_null pgdebug_strncpy_null */
88111
#define memcpy pgdebug_memcpy
89112
#define memset pgdebug_memset
113+
#else /* _MEMORY_DEBUG_ */
114+
#ifdef WIN32
115+
#undef strdup
116+
#endif /* WIN32 */
117+
#define malloc pg_malloc
118+
#define realloc pg_realloc
119+
#define calloc pg_calloc
120+
#define strdup pg_strdup
121+
#define free pg_free
90122
#endif /* _MEMORY_DEBUG_ */
91123

92124
#ifdef WIN32

winbuild/BuildAll.ps1

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
Specify the configuration xml file name if you want to use
3131
the configuration file other than standard one.
3232
The relative path is relative to the current directory.
33+
.PARAMETER UseMimalloc
34+
Specify whether to use the mimalloc allocator for improved performance.
35+
Requires a toolset of v141, v142 or later.
3336
.EXAMPLE
3437
> .\BuildAll
3538
Build with default or automatically selected parameters.
@@ -65,7 +68,8 @@ Param(
6568
[ValidateSet("Debug", "Release")]
6669
[String]$Configuration="Release",
6770
[string]$BuildConfigPath,
68-
[switch]$AlongWithInstallers
71+
[switch]$AlongWithInstallers,
72+
[switch]$UseMimalloc
6973
)
7074

7175
function buildPlatform([xml]$configInfo, [string]$Platform)
@@ -100,7 +104,27 @@ function buildPlatform([xml]$configInfo, [string]$Platform)
100104
$BUILD_MACROS = $BUILD_MACROS -replace '"', '`"'
101105
$macroList = iex "write-output $BUILD_MACROS"
102106
}
103-
& ${msbuildexe} ./platformbuild.vcxproj /tv:$MSToolsV "/p:Platform=$Platform;Configuration=$Configuration;PlatformToolset=${Toolset}" /t:$target /p:VisualStudioVersion=${VCVersion} /p:DRIVERVERSION=$DRIVERVERSION /p:PG_INC=$PG_INC /p:PG_LIB=$PG_LIB /p:PG_BIN=$PG_BIN $macroList
107+
108+
if ($UseMimalloc) {
109+
$mimallocProperty = "yes"
110+
111+
switch ($VCVersion) {
112+
"10.0" { $mimallocIdeDir = "vs2017" }
113+
"11.0" { $mimallocIdeDir = "vs2017" }
114+
"12.0" { $mimallocIdeDir = "vs2017" }
115+
"14.0" { $mimallocIdeDir = "vs2017" }
116+
"15.0" { $mimallocIdeDir = "vs2017" }
117+
"16.0" { $mimallocIdeDir = "vs2019" }
118+
"17.0" { $mimallocIdeDir = "vs2022" }
119+
default { throw "Unable to resolve mimalloc IDE directory for VC ${VCVersion}."}
120+
}
121+
122+
# build mimalloc dependency
123+
& ${msbuildexe} ..\libs\mimalloc\ide\$mimallocIdeDir\mimalloc.vcxproj /tv:$MSToolsV "/p:Platform=$Platform;Configuration=$Configuration;PlatformToolset=${Toolset}" /t:$target /p:VisualStudioVersion=${VCVersion}
124+
}
125+
126+
# build psqlodbc
127+
& ${msbuildexe} ./platformbuild.vcxproj /tv:$MSToolsV "/p:Platform=$Platform;Configuration=$Configuration;PlatformToolset=${Toolset}" /t:$target /p:VisualStudioVersion=${VCVersion} /p:DRIVERVERSION=$DRIVERVERSION /p:PG_INC=$PG_INC /p:PG_LIB=$PG_LIB /p:PG_BIN=$PG_BIN /p:MIMALLOC=$mimallocProperty $macroList
104128
}
105129

106130
$scriptPath = (Split-Path $MyInvocation.MyCommand.Path)

0 commit comments

Comments
 (0)