GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-90815: Add mimalloc memory allocator by DinoV · Pull Request #109914 · python/cpython

Conversation

@DinoV

@DinoV DinoV commented Sep 26, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • Issue: Add mimalloc memory allocator #90815

This adds mimalloc as an optional (but preferred when available) allocator to CPython. This is a bit of a mashup of the work from #109914 and the work of @colesbury to use mimalloc for no-gil and various updates to bring it up to current CPython.

The configuration logic added by @tiran is re-used and we keep pymalloc support unlike in the version from @colesbury. mimalloc is updated to 2.12 and along with a few changes @colesbury made to it.

This has run into some issues with subinterpreter support in that the allocator's are now stored in thread state and are per-thread. Sub interpreters in some scenarios will create a thread state on one thread and run that on another thread. Most of these are documented in the code base as being known issues. I've modified these so that we will find the right thread based upon the current thread ID and switch to it rather than getting the head thread. This seems pretty reasonable but looks pretty weird when we need to do it at interpreter shutdown.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vasily-v-ryabov and sergey-miryanov reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
@DinoV
DinoV force-pushed the nogil/mimalloc_212 branch 7 times, most recently from 8b3ec52 to 06e86d7 Compare September 27, 2023 00:25
@DinoV DinoV changed the title Nogil/mimalloc 212 gh-90815: Add mimalloc memory allocator Sep 27, 2023
@bedevere-app bedevere-app Bot mentioned this pull request Sep 27, 2023
Add mimalloc memory allocator #90815
Closed
@DinoV
DinoV force-pushed the nogil/mimalloc_212 branch 4 times, most recently from e8c4f01 to 2dd8675 Compare September 27, 2023 15:59
@DinoV
DinoV requested a review from ericsnowcurrently September 27, 2023 16:04
@ericsnowcurrently

ericsnowcurrently commented Sep 27, 2023

Copy link
Copy Markdown
Member

FTR, @tiran's gh-31164 is a similar change and has a bunch of discussion.

CC @daanx

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DinoV
DinoV force-pushed the nogil/mimalloc_212 branch from 2dd8675 to 672b6f4 Compare September 27, 2023 17:35
@DinoV
DinoV requested a review from colesbury September 27, 2023 17:36
@DinoV
DinoV force-pushed the nogil/mimalloc_212 branch from 672b6f4 to 41b62cb Compare September 27, 2023 17:52
@DinoV

DinoV commented Sep 27, 2023

Copy link
Copy Markdown
Contributor Author

@ericsnowcurrently Yep, and much of the autoconf stuff is taken from there... I just finally filled in the description with a little more background on where things are coming from :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DinoV
DinoV force-pushed the nogil/mimalloc_212 branch from 41b62cb to 0dbe76b Compare September 27, 2023 18:19
@ericsnowcurrently

ericsnowcurrently commented Sep 27, 2023

Copy link
Copy Markdown
Member

This has run into some issues with subinterpreter support in that the allocator's are now stored in thread state and are per-thread.

I expect that most of this PR is a relatively vanilla use of mimalloc in CPython, like @tiran's PR is. Would it be much trouble to keep the whole PR vanilla and have a follow-up PR that applies the customizations to mimalloc (e.g. per-thread allocator state)? That distinction would help, possibly a lot, when reviewing.

DinoV reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DinoV
DinoV force-pushed the nogil/mimalloc_212 branch 7 times, most recently from 7bda1e4 to 35d1ebd Compare September 28, 2023 19:52
@DinoV
DinoV marked this pull request as ready for review September 28, 2023 20:21
@DinoV
DinoV requested a review from rhettinger as a code owner September 28, 2023 20:21
vstinner
vstinner reviewed Oct 30, 2023
View reviewed changes
Comment thread Lib/test/pythoninfo.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

vstinner added 2 commits October 30, 2023 15:40
@vstinner
Update Lib/test/pythoninfo.py
3d43b3a
@vstinner
license.rst: remove empty line at the end
3261bcb
vstinner
vstinner reviewed Oct 30, 2023
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also issue #111499: PYTHONMALLOCSTATS=1 fails with fatal error at Python exit. Using mimalloc, Python is not affected :-)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
vstinner added 4 commits October 30, 2023 16:13
@vstinner
Reduce Makefile.pre.in indentation
99c4a24
@vstinner
obmalloc.c: indent
e2e36dc
@vstinner
Split _PyObject_DebugMallocStats() in sub-functions
62b7b83
@vstinner
Fix memory doc
ab829b3
mimalloc is not the default
@vstinner

vstinner commented Oct 30, 2023

Copy link
Copy Markdown
Member

@DinoV: I pushed a few cleanup changes. I fixed the outdated doc which still said that mimalloc is the default.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner enabled auto-merge (squash) October 30, 2023 15:25
@vstinner

vstinner commented Oct 30, 2023

Copy link
Copy Markdown
Member

I cleaned the commit message to only keep most important information.

The configuration logic added by @tiran is re-used

I added @tiran as a co-author in the commit message.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ericsnowcurrently

ericsnowcurrently commented Oct 30, 2023

Copy link
Copy Markdown
Member

Is there any chance of those changes getting upstreamed? There's advantages for cpython then too -- way less work to rebase the internal copy and no real risk of bugs being cpython introduced.

Any thoughts?

It's worth further discussion.

CC @daanx

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner merged commit 05f2f0a into python:main Oct 30, 2023
@bedevere-app bedevere-app Bot removed the awaiting review label Oct 30, 2023
@vstinner

vstinner commented Oct 30, 2023

Copy link
Copy Markdown
Member

Congrats @DinoV, I merged your PR :-) Do you want to propose a follow-up PR to enable it by default when --disable-gil is used?

DinoV reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ericsnowcurrently

ericsnowcurrently commented Oct 30, 2023

Copy link
Copy Markdown
Member

Looks like this broke the WASM buildbot: https://buildbot.python.org/all/#/builders/1046/builds/3371.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brettcannon

brettcannon commented Oct 30, 2023

Copy link
Copy Markdown
Member

Looks like this broke the WASM buildbot: https://buildbot.python.org/all/#/builders/1046/builds/3371.

Yep, looks like there's an implicit function definition:

In file included from ../../Objects/obmalloc.c:15:
In file included from ../../Objects/mimalloc/static.c:37:
In file included from ../../Objects/mimalloc/prim/prim.c:19:
../../Objects/mimalloc/prim/wasi/prim.c:44:15: error: implicit declaration of function 'sbrk' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    void* p = sbrk(size);
              ^
1 error generated.
make: *** [Makefile:2723: Objects/obmalloc.o] Error 1
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Oct 30, 2023

Copy link
Copy Markdown
Member

../../Objects/mimalloc/prim/wasi/prim.c:44:15: error: implicit declaration of function 'sbrk' is invalid in C99 [-Werror,-Wimplicit-function-declaration]

I wrote PR #111524 to fix WASI build.

DinoV reacted with thumbs up emoji brettcannon reacted with heart emoji
All reactions
  • 👍 1 reaction
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland erlend-aasland mentioned this pull request Oct 30, 2023
gh-90815: Add mimalloc memory allocator #31164
Closed
@vstinner

vstinner commented Oct 31, 2023

Copy link
Copy Markdown
Member

I wrote 4 follow-up PRs to fix different issues:

  • gh-90815: Fix _PyMem_MimallocEnabled() compiler warning #111522
  • gh-90815: Fix mimalloc build on WASI #111524
  • gh-90815: Fix mimalloc atomic.h on Windows arm64 #111527
  • gh-90815: Define WITH_MIMALLOC macro in PC/pyconfig.h #111528

While mimalloc C code is built on Windows, Python cannot currently use mimalloc on Windows: see PR #111528.

stonebig and Hnasar reacted with heart emoji
All reactions
  • ❤️ 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ayappanec ayappanec mentioned this pull request Oct 31, 2023
AIX build breaks due to syscall.h not found #111544
Closed
FullteaR pushed a commit to FullteaR/cpython that referenced this pull request Nov 3, 2023
@DinoV @colesbury
@tiran @vstinner @FullteaR
pythongh-90815: Add mimalloc memory allocator (python#109914)
37fa7b1
* Add mimalloc v2.12

Modified src/alloc.c to remove include of alloc-override.c and not
compile new handler.

Did not include the following files:

 - include/mimalloc-new-delete.h
 - include/mimalloc-override.h
 - src/alloc-override-osx.c
 - src/alloc-override.c
 - src/static.c
 - src/region.c

mimalloc is thread safe and shares a single heap across all runtimes,
therefore finalization and getting global allocated blocks across all
runtimes is different.

* mimalloc: minimal changes for use in Python:

 - remove debug spam for freeing large allocations
 - use same bytes (0xDD) for freed allocations in CPython and mimalloc
   This is important for the test_capi debug memory tests

* Don't export mimalloc symbol in libpython.
* Enable mimalloc as Python allocator option.
* Add mimalloc MIT license.
* Log mimalloc in Lib/test/pythoninfo.py.
* Document new mimalloc support.
* Use macro defs for exports as done in:
  python#31164

Co-authored-by: Sam Gross <[email protected]>
Co-authored-by: Christian Heimes <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
@DinoV @colesbury
@tiran @vstinner @aisk
pythongh-90815: Add mimalloc memory allocator (python#109914)
5f7475d
* Add mimalloc v2.12

Modified src/alloc.c to remove include of alloc-override.c and not
compile new handler.

Did not include the following files:

 - include/mimalloc-new-delete.h
 - include/mimalloc-override.h
 - src/alloc-override-osx.c
 - src/alloc-override.c
 - src/static.c
 - src/region.c

mimalloc is thread safe and shares a single heap across all runtimes,
therefore finalization and getting global allocated blocks across all
runtimes is different.

* mimalloc: minimal changes for use in Python:

 - remove debug spam for freeing large allocations
 - use same bytes (0xDD) for freed allocations in CPython and mimalloc
   This is important for the test_capi debug memory tests

* Don't export mimalloc symbol in libpython.
* Enable mimalloc as Python allocator option.
* Add mimalloc MIT license.
* Log mimalloc in Lib/test/pythoninfo.py.
* Document new mimalloc support.
* Use macro defs for exports as done in:
  python#31164

Co-authored-by: Sam Gross <[email protected]>
Co-authored-by: Christian Heimes <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
@oraluben oraluben mentioned this pull request Mar 19, 2024
Mimalloc header is not installed #116984
Closed
@freakboy3742 freakboy3742 mentioned this pull request Apr 18, 2024
Vendored mimalloc references macOS preprocessor symbols that no longer exist #118072
Open
@DinoV
DinoV deleted the nogil/mimalloc_212 branch May 31, 2024 18:23
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
@DinoV @colesbury
@tiran @vstinner @Glyphack
pythongh-90815: Add mimalloc memory allocator (python#109914)
554c420
* Add mimalloc v2.12

Modified src/alloc.c to remove include of alloc-override.c and not
compile new handler.

Did not include the following files:

 - include/mimalloc-new-delete.h
 - include/mimalloc-override.h
 - src/alloc-override-osx.c
 - src/alloc-override.c
 - src/static.c
 - src/region.c

mimalloc is thread safe and shares a single heap across all runtimes,
therefore finalization and getting global allocated blocks across all
runtimes is different.

* mimalloc: minimal changes for use in Python:

 - remove debug spam for freeing large allocations
 - use same bytes (0xDD) for freed allocations in CPython and mimalloc
   This is important for the test_capi debug memory tests

* Don't export mimalloc symbol in libpython.
* Enable mimalloc as Python allocator option.
* Add mimalloc MIT license.
* Log mimalloc in Lib/test/pythoninfo.py.
* Document new mimalloc support.
* Use macro defs for exports as done in:
  python#31164

Co-authored-by: Sam Gross <[email protected]>
Co-authored-by: Christian Heimes <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
@AA-Turner AA-Turner mentioned this pull request Aug 10, 2025
GH-92266: Remove embedded tabs from c-analyzer/cpython/_parser.py #137622
Merged
rokm added a commit to rokm/pyinstaller that referenced this pull request Oct 9, 2026
@rokm
bootloader: do not unload python shared library on NetBSD + python >=…
78235c1
… 3.13

Python 3.13 introduced mimalloc memory allocator [1], which registers
an `atexit()` handler [2].

On NetBSD 11, an `atexit()` handler registered in a shared library is
not executed upon the library unload (e.g., via `dlclose()`), but rather
on program exit. So unloading a library that registers an `atexit()`
handler results in segmentation fault in libc's `__cxa_finalize` when
the program exits, because the handler is no longer reachable...

The problem seems to be specific to NetBSD; on other platforms
(including OpenBSD and FreeBSD), the handler is executed upon the
library unload.

[1] python/cpython#109914
[2] https://github.com/python/cpython/blob/05f2f0ac92afa560315eb66fd6576683c7f69e2d/Objects/mimalloc/init.c#L530
rokm added a commit to pyinstaller/pyinstaller that referenced this pull request Oct 16, 2026
@rokm
bootloader: do not unload python shared library on NetBSD + python >=…
e87034a
… 3.13

Python 3.13 introduced mimalloc memory allocator [1], which registers
an `atexit()` handler [2].

On NetBSD 11, an `atexit()` handler registered in a shared library is
not executed upon the library unload (e.g., via `dlclose()`), but rather
on program exit. So unloading a library that registers an `atexit()`
handler results in segmentation fault in libc's `__cxa_finalize` when
the program exits, because the handler is no longer reachable...

The problem seems to be specific to NetBSD; on other platforms
(including OpenBSD and FreeBSD), the handler is executed upon the
library unload.

[1] python/cpython#109914
[2] https://github.com/python/cpython/blob/05f2f0ac92afa560315eb66fd6576683c7f69e2d/Objects/mimalloc/init.c#L530
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner left review comments
@corona10 corona10 corona10 left review comments
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@colesbury colesbury Awaiting requested review from colesbury
@erlend-aasland erlend-aasland Awaiting requested review from erlend-aasland erlend-aasland is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@DinoV @ericsnowcurrently @vstinner @itamaro @colesbury @thesamesam @brettcannon @corona10
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.