From 734455fc052c47cfc7078b6de23809552521b471 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 14 Feb 2026 10:02:53 +0530 Subject: [PATCH 1/7] use mimalloc for raw allocations --- Include/internal/pycore_pymem_init.h | 5 +++++ Objects/obmalloc.c | 30 +++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Include/internal/pycore_pymem_init.h b/Include/internal/pycore_pymem_init.h index c593edc86d9952..6e080476bb2e8f 100644 --- a/Include/internal/pycore_pymem_init.h +++ b/Include/internal/pycore_pymem_init.h @@ -29,6 +29,11 @@ extern void* _PyMem_MiMalloc(void *, size_t); extern void* _PyMem_MiCalloc(void *, size_t, size_t); extern void _PyMem_MiFree(void *, void *); extern void* _PyMem_MiRealloc(void *, void *, size_t); +extern void* _PyMem_MiRawMalloc(void *, size_t); +extern void* _PyMem_MiRawCalloc(void *, size_t, size_t); +extern void _PyMem_MiRawFree(void *, void *); +extern void* _PyMem_MiRawRealloc(void *, void *, size_t); +# define PYRAW_ALLOC {NULL, _PyMem_MiRawMalloc, _PyMem_MiRawCalloc, _PyMem_MiRawRealloc, _PyMem_MiRawFree} # define PYMEM_ALLOC {NULL, _PyMem_MiMalloc, _PyMem_MiCalloc, _PyMem_MiRealloc, _PyMem_MiFree} #elif defined(WITH_PYMALLOC) extern void* _PyObject_Malloc(void *, size_t); diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c index ce2e39790bd76c..92692ffce9e69d 100644 --- a/Objects/obmalloc.c +++ b/Objects/obmalloc.c @@ -358,6 +358,29 @@ _PyObject_MiFree(void *ctx, void *ptr) mi_free(ptr); } +void * +_PyMem_MiRawMalloc(void *, size_t size) +{ + return mi_malloc(size); +} + +void * +_PyMem_MiRawCalloc(void *, size_t nelem, size_t elsize) +{ + return mi_calloc(nelem, elsize); +} + +void * +_PyMem_MiRawRealloc(void *, void *ptr, size_t size) +{ + return mi_realloc(ptr, size); +} + +void +_PyMem_MiRawFree(void *, void *ptr) +{ + mi_free(ptr); +} #endif // WITH_MIMALLOC @@ -365,7 +388,8 @@ _PyObject_MiFree(void *ctx, void *ptr) #ifdef WITH_MIMALLOC -# define MIMALLOC_ALLOC {NULL, _PyMem_MiMalloc, _PyMem_MiCalloc, _PyMem_MiRealloc, _PyMem_MiFree} +# define MIMALLOC_ALLOC {NULL, _PyMem_MiMalloc, _PyMem_MiCalloc, _PyMem_MiRealloc, _PyMem_MiFree} +# define MIMALLOC_RAWALLOC {NULL, _PyMem_MiRawMalloc, _PyMem_MiRawCalloc, _PyMem_MiRawRealloc, _PyMem_MiRawFree} # define MIMALLOC_OBJALLOC {NULL, _PyObject_MiMalloc, _PyObject_MiCalloc, _PyObject_MiRealloc, _PyObject_MiFree} #endif @@ -383,7 +407,7 @@ void* _PyObject_Realloc(void *ctx, void *ptr, size_t size); #if defined(Py_GIL_DISABLED) // Py_GIL_DISABLED requires using mimalloc for "mem" and "obj" domains. -# define PYRAW_ALLOC MALLOC_ALLOC +# define PYRAW_ALLOC MIMALLOC_RAWALLOC # define PYMEM_ALLOC MIMALLOC_ALLOC # define PYOBJ_ALLOC MIMALLOC_OBJALLOC #elif defined(WITH_PYMALLOC) @@ -758,7 +782,7 @@ set_up_allocators_unlocked(PyMemAllocatorName allocator) case PYMEM_ALLOCATOR_MIMALLOC: case PYMEM_ALLOCATOR_MIMALLOC_DEBUG: { - PyMemAllocatorEx malloc_alloc = MALLOC_ALLOC; + PyMemAllocatorEx malloc_alloc = MIMALLOC_RAWALLOC; set_allocator_unlocked(PYMEM_DOMAIN_RAW, &malloc_alloc); PyMemAllocatorEx pymalloc = MIMALLOC_ALLOC; From 423dc483030cee178d86f5e1214a7a6ec06a4905 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Tue, 17 Feb 2026 19:37:32 +0530 Subject: [PATCH 2/7] add ctx --- Objects/obmalloc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c index 92692ffce9e69d..60e1981653a32d 100644 --- a/Objects/obmalloc.c +++ b/Objects/obmalloc.c @@ -359,25 +359,25 @@ _PyObject_MiFree(void *ctx, void *ptr) } void * -_PyMem_MiRawMalloc(void *, size_t size) +_PyMem_MiRawMalloc(void *ctx, size_t size) { return mi_malloc(size); } void * -_PyMem_MiRawCalloc(void *, size_t nelem, size_t elsize) +_PyMem_MiRawCalloc(void *ctx, size_t nelem, size_t elsize) { return mi_calloc(nelem, elsize); } void * -_PyMem_MiRawRealloc(void *, void *ptr, size_t size) +_PyMem_MiRawRealloc(void *ctx, void *ptr, size_t size) { return mi_realloc(ptr, size); } void -_PyMem_MiRawFree(void *, void *ptr) +_PyMem_MiRawFree(void *ctx, void *ptr) { mi_free(ptr); } From 4ab697cea4598fe591780248ce5face86e71e5d3 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Tue, 17 Feb 2026 20:20:11 +0530 Subject: [PATCH 3/7] fix test --- Objects/obmalloc.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c index 60e1981653a32d..b59ebdfbda3897 100644 --- a/Objects/obmalloc.c +++ b/Objects/obmalloc.c @@ -852,6 +852,7 @@ get_current_allocator_name_unlocked(void) #ifdef WITH_MIMALLOC PyMemAllocatorEx mimalloc = MIMALLOC_ALLOC; PyMemAllocatorEx mimalloc_obj = MIMALLOC_OBJALLOC; + PyMemAllocatorEx mimalloc_raw = MIMALLOC_RAWALLOC; #endif if (pymemallocator_eq(&_PyMem_Raw, &malloc_alloc) && @@ -869,7 +870,7 @@ get_current_allocator_name_unlocked(void) } #endif #ifdef WITH_MIMALLOC - if (pymemallocator_eq(&_PyMem_Raw, &malloc_alloc) && + if (pymemallocator_eq(&_PyMem_Raw, &mimalloc_raw) && pymemallocator_eq(&_PyMem, &mimalloc) && pymemallocator_eq(&_PyObject, &mimalloc_obj)) { @@ -901,7 +902,7 @@ get_current_allocator_name_unlocked(void) } #endif #ifdef WITH_MIMALLOC - if (pymemallocator_eq(&_PyMem_Debug.raw.alloc, &malloc_alloc) && + if (pymemallocator_eq(&_PyMem_Debug.raw.alloc, &mimalloc_raw) && pymemallocator_eq(&_PyMem_Debug.mem.alloc, &mimalloc) && pymemallocator_eq(&_PyMem_Debug.obj.alloc, &mimalloc_obj)) { From 40c33a51954014963719ec623beaeba574764ada Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Tue, 17 Feb 2026 23:53:46 +0530 Subject: [PATCH 4/7] fix macro redefinition --- Include/internal/pycore_pymem_init.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Include/internal/pycore_pymem_init.h b/Include/internal/pycore_pymem_init.h index 6e080476bb2e8f..2a0e0817dcc7f8 100644 --- a/Include/internal/pycore_pymem_init.h +++ b/Include/internal/pycore_pymem_init.h @@ -29,12 +29,13 @@ extern void* _PyMem_MiMalloc(void *, size_t); extern void* _PyMem_MiCalloc(void *, size_t, size_t); extern void _PyMem_MiFree(void *, void *); extern void* _PyMem_MiRealloc(void *, void *, size_t); +# define PYMEM_ALLOC {NULL, _PyMem_MiMalloc, _PyMem_MiCalloc, _PyMem_MiRealloc, _PyMem_MiFree} extern void* _PyMem_MiRawMalloc(void *, size_t); extern void* _PyMem_MiRawCalloc(void *, size_t, size_t); extern void _PyMem_MiRawFree(void *, void *); extern void* _PyMem_MiRawRealloc(void *, void *, size_t); +# undef PYRAW_ALLOC # define PYRAW_ALLOC {NULL, _PyMem_MiRawMalloc, _PyMem_MiRawCalloc, _PyMem_MiRawRealloc, _PyMem_MiRawFree} -# define PYMEM_ALLOC {NULL, _PyMem_MiMalloc, _PyMem_MiCalloc, _PyMem_MiRealloc, _PyMem_MiFree} #elif defined(WITH_PYMALLOC) extern void* _PyObject_Malloc(void *, size_t); extern void* _PyObject_Calloc(void *, size_t, size_t); From 0ce845d5d2ed905bf16950601d2d9b1bc2ccbfcd Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 17 Feb 2026 18:27:33 +0000 Subject: [PATCH 5/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2026-02-17-18-27-28.gh-issue-144914.DcXO4m.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-02-17-18-27-28.gh-issue-144914.DcXO4m.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-17-18-27-28.gh-issue-144914.DcXO4m.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-17-18-27-28.gh-issue-144914.DcXO4m.rst new file mode 100644 index 00000000000000..1b56588c59e7e7 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-17-18-27-28.gh-issue-144914.DcXO4m.rst @@ -0,0 +1 @@ +Use ``mimalloc`` for raw memory allocations such as via :c:func:`PyMem_RawMalloc` for better performance on :term:`free-threaded builds`. Patch by Kumar Aditya. From 5bd4c3d2bb7af0a8144542f232856a1431cc31b3 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Wed, 18 Feb 2026 00:03:49 +0530 Subject: [PATCH 6/7] fix news --- .../2026-02-17-18-27-28.gh-issue-144914.DcXO4m.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-17-18-27-28.gh-issue-144914.DcXO4m.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-17-18-27-28.gh-issue-144914.DcXO4m.rst index 1b56588c59e7e7..f13b8541a0ebe2 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-17-18-27-28.gh-issue-144914.DcXO4m.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-17-18-27-28.gh-issue-144914.DcXO4m.rst @@ -1 +1 @@ -Use ``mimalloc`` for raw memory allocations such as via :c:func:`PyMem_RawMalloc` for better performance on :term:`free-threaded builds`. Patch by Kumar Aditya. +Use ``mimalloc`` for raw memory allocations such as via :c:func:`PyMem_RawMalloc` for better performance on :term:`free-threaded builds `. Patch by Kumar Aditya. From 396d4ae1f170fb00ddf466813dfd1739e095d4bc Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Wed, 18 Feb 2026 09:19:43 +0530 Subject: [PATCH 7/7] add whatsnew entry --- Doc/whatsnew/3.15.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index e5714765208ff6..62ce7121424651 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -1204,6 +1204,11 @@ Optimizations (Contributed by Chris Eibl, Ken Jin, and Brandt Bucher in :gh:`143068`. Special thanks to the MSVC team including Hulon Jenkins.) +* ``mimalloc`` is now used as the default allocator for + for raw memory allocations such as via :c:func:`PyMem_RawMalloc` + for better performance on :term:`free-threaded builds `. + (Contributed by Kumar Aditya in :gh:`144914`.) + base64 & binascii -----------------