GeezerVison is showing github.com — Add mimalloc memory allocator · Issue #90815 · python/cpython
Add mimalloc memory allocator #90815Description
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:
assignee = None
closed_at = None
created_at = <Date 2022-02-06.14:49:12.884>
labels = ['interpreter-core', 'type-feature', '3.11']
title = 'Add mimalloc memory allocator'
updated_at = <Date 2022-03-24.02:10:23.628>
user = 'https://github.com/tiran'
bugs.python.org fields:
activity = <Date 2022-03-24.02:10:23.628>
actor = 'h-vetinari'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2022-02-06.14:49:12.884>
creator = 'christian.heimes'
dependencies = []
files = []
hgrepos = []
issue_num = 46657
keywords = ['patch']
message_count = 12.0
messages = ['412639', '412641', '412645', '412654', '412669', '412679', '412741', '412749', '412796', '412835', '412867', '412986']
nosy_count = 5.0
nosy_names = ['nascheme', 'christian.heimes', 'corona10', 'erlendaasland', 'h-vetinari']
pr_nums = ['31164']
priority = 'normal'
resolution = None
stage = 'patch review'
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue46657'
versions = ['Python 3.11']
Linked PRs
Reacted by Vladimir, Daniel Giger, Rodolfo De Nadai, Erlend E. Aasland, Vasily Ryabov, Mix and Tanel Liiv Activitytiran commented on Feb 6, 2022 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 corona10 commented on Feb 6, 2022 I add Neil to the nosy list since he is one of the kick-off members with this amazing works :) tiran commented on Feb 6, 2022 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
tiran commented on Feb 6, 2022 Buildbots "PPC64 Fedora PR" and all RHEL 7 build bots provided by David Edelsohn are failing because compiler is missing support for stdatomic.h. nascheme commented on Feb 6, 2022 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. tiran commented on Feb 6, 2022 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. tiran commented on Feb 7, 2022 ICC might be a problem. Apparently some version have an incomplete stdatomic.h, see bpo-37415. tiran commented on Feb 7, 2022 nascheme commented on Feb 7, 2022 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://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. nascheme commented on Dec 23, 2023 I think this issue could be closed now that mimalloc went into HEAD already. Reacted by Vasily Ryabov and Erlend E. Aasland Reacted by Vasily Ryabov erlend-aasland commented on Jan 4, 2024
[...]
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:
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|