Repository navigation
Add mimalloc memory allocator #90815
Description
Activity
From https://github.com/microsoft/mimalloc
mimalloc (pronounced "me-malloc") is a general purpose allocator with excellent performance characteristics. Initially developed by Daan Leijen for the run-time systems of the Koka and Lean languages.
mimalloc has several interesting properties that make it useful for CPython. Amongst other it is fast, thread-safe, and NUMA-aware. It has built-in free lists with multi-sharding and allocation heaps. While Python's obmalloc requires the GIL to protect its data structures, mimalloc uses mostly thread-local and atomic instructions (compare-and-swap) for efficiency. Sam Gross' nogil relies on mimalloc's thread safety and uses first-class heaps for heap walking GC.
mimalloc works on majority of platforms and CPU architectures. However it requires a compiler with C11 atomics support. CentOS 7's default GCC is slightly too old, more recent GCC from Developer Toolset is required.
For 3.11 I plan to integrate mimalloc as an optional drop-in replacement for obmalloc. Users will be able to compile CPython without mimalloc or disable mimalloc with PYTHONMALLOC env var. Since mimalloc will be optional in 3.11, Python won't depend or expose on any of the advanced features yet. The approach enables the community to test and give feedback with minimal risk of breakage.
mimalloc sources will vendored without any option to use system libraries. Python's mimalloc requires several non-standard compile-time flags. In the future Python may extend or modify mimalloc for heap walking and nogil, too.
(This is a tracking bug until I find time to finish a PEP.)
Reacted by jadon, Vladimir, Erlend E. Aasland, Vasily Ryabov, Murplugg and sparkleMing- added3.11only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement
on Feb 6, 2022 I add Neil to the nosy list since he is one of the kick-off members with this amazing works :)
New features:
- vendored mimalloc 2.0.3 + two patches from mimalloc dev branch. Mimalloc is embedded in obmalloc.o. Symbols are either hidden or names are mangled to have a _Py_ prefix.
- ./configure --with[out]-mimalloc (default: yes), fails if atomics are not available.
- PYTHONMALLOC=mimalloc, PYTHONMALLOC=mimalloc-debug env var settings
- PYMEM_ALLOCATOR_MIMALLOC, PYMEM_ALLOCATOR_MIMALLOC_DEBUG
- sys.debugmallocstats() and _PyObject_DebugMallocStats() prints mimalloc stats
- sys._malloc_info struct, contains information about available and current allocator
Buildbots "PPC64 Fedora PR" and all RHEL 7 build bots provided by David Edelsohn are failing because compiler is missing support for stdatomic.h.
Thanks, I'm indeed interested. Most credit goes to Christian for advancing this.
For the missing stdatomic.h, would it be appropriate to have an autoconfig check for it? Can just disable mimalloc if it doesn't exist.
We have an autoconf check for stdatomic.h. The test even verifies that a program with atomic_load_explicit() compiles and links.
How do we want to use mimalloc in the future? Is it going to stay optional in 3.12? Then the default setting for --with-mimalloc should depend on presence of stdatomic.h. Do we want to make it mandatory for GC heap walking and nogil? Then --with-mimalloc should default to "yes" and configure should abort when stdatomic.h is missing.
I'm leaning towards --with-mimalloc=yes. It will make users aware that they need a compiler with atomics:
configure: error: --with-mimalloc requires stdatomic.h. Update your compiler or rebuild with --without-mimalloc. Python 3.12 will require stdatomic.
ICC might be a problem. Apparently some version have an incomplete stdatomic.h, see bpo-37415.
References:
- C11 stdatomic.h https://en.cppreference.com/w/c/atomic
- mimalloc-atomic.h https://github.com/microsoft/mimalloc/blob/master/include/mimalloc-atomic.h
- MSVC Interlocked Variable Access https://docs.microsoft.com/de-de/windows/win32/sync/interlocked-variable-access
My preference would be for --with-mimalloc=yes in an upcoming release. For platforms without the required stdatomic.h stuff, they can manually specify --with-mimalloc=no. That will make them aware that a future release of Python might no longer build (if mimalloc is no longer optional).
A soft-landing for merging nogil is not a good enough reason to merge mimalloc, IMHO. nogil may never be merged. There should be some concrete and immediate advantage to switch to mimalloc. The idea of using the "heap walking" to improve is cyclic GC is not concrete enough. It's just an idea at this point.
I think the (small) performance win could be enough of a reason to merge. This seems to be the most recent benchmark:
https://github.com/proxy/gist.github.com/pablogsal/8027937b71cd30f17aaaa5ef7c885d3e
There is also the long-term maintenance issue. So far, mimalloc upstream has been responsive. The mimalloc code is not so huge or complicated that we couldn't maintain it (if for some reason it gets abandoned upstream). However, I think we would prefer to maintain obmalloc rather than mimalloc, all else being equal. Abandonment by the upstream seems fairly unlikely. So, I'm not too concerned about maintenance.
51 remaining items
I think this issue could be closed now that mimalloc went into HEAD already.
Reacted by Vasily Ryabov and Erlend E. AaslandReacted by Vasily Ryabov[...]
There is also the long-term maintenance issue. So far, mimalloc upstream has been responsive. The mimalloc code is not so huge or complicated that we couldn't maintain it (if for some reason it gets abandoned upstream). However, I think we would prefer to maintain obmalloc rather than mimalloc, all else being equal. Abandonment by the upstream seems fairly unlikely. So, I'm not too concerned about maintenance.I made a similar point wrt the bundling at #109914 (comment).
See follow-up issue:
- added 5 commits that reference this issue
on Feb 11, 2024 - added a commit that references this issue
on Apr 18, 2024 - added 5 commits that reference this issue
on Sep 2, 2024
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs