FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-90815: Add mimalloc memory allocator by DinoV · Pull Request #109914 · python/cpython · GitHub

/ cpython Public

gh-90815: Add mimalloc memory allocator - #109914

Merged
vstinner merged 24 commits into
python:mainfrom
DinoV:nogil/mimalloc_212
Oct 30, 2023
Merged

gh-90815: Add mimalloc memory allocator#109914
vstinner merged 24 commits into
python:mainfrom
DinoV:nogil/mimalloc_212

Conversation

DinoV commented Sep 26, 2023
edited
Loading

Copy link
Copy Markdown
Contributor

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.

DinoV force-pushed the nogil/mimalloc_212 branch 7 times, most recently from 8b3ec52 to 06e86d7 Compare September 27, 2023 00:25
DinoV changed the title Nogil/mimalloc 212 gh-90815: Add mimalloc memory allocator Sep 27, 2023
DinoV force-pushed the nogil/mimalloc_212 branch 4 times, most recently from e8c4f01 to 2dd8675 Compare September 27, 2023 15:59

Copy link
Copy Markdown
Member

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

CC @daanx

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

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 :)

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

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 force-pushed the nogil/mimalloc_212 branch 7 times, most recently from 7bda1e4 to 35d1ebd Compare September 28, 2023 19:52
DinoV marked this pull request as ready for review September 28, 2023 20:21
DinoV requested a review from rhettinger as a code owner September 28, 2023 20:21

DinoV commented Oct 23, 2023

Copy link
Copy Markdown
Contributor Author

Okay, the default is now pymalloc, for now PYTHONMALLOC=mimalloc ./python will pick up mimalloc. When we start to get nogil integration with it we'll need to make it the default/required in that config.

thesamesam commented Oct 30, 2023
edited
Loading

Copy link
Copy Markdown
Contributor

Is it possible for mimalloc?

That's not possible because we will have changes to mimalloc.

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? I think this got missed earlier in the review flurry. I've not seen any substantive discussion about why bundling is a good idea, which feels like it's worth proper consideration, given it's short-term easier but long-term expensive.

I understand upstreaming it initially is going to take some time, but it is going to be worthwhile for cpython upstream maintenance long-term too, as you can avoid:

  • having cpython being stuck on outdated versions in perpetuity, or
  • figuring out how to rebase (and include the local changes)
  • backporting fixes for various platforms/new compilers/etc to increasingly old versions of mimalloc

This also, of course, has the advantage of forcing one to document what the changes are which I think should be done either way.

Comment thread Lib/test/pythoninfo.py Outdated

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

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.

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

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.

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

vstinner merged commit 05f2f0a into python:main 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?

Copy link
Copy Markdown
Member

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

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

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.

Copy link
Copy Markdown
Member

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

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

FullteaR pushed a commit to FullteaR/cpython that referenced this pull request Nov 3, 2023
* 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 <colesbury@gmail.com>
Co-authored-by: Christian Heimes <christian@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
* 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 <colesbury@gmail.com>
Co-authored-by: Christian Heimes <christian@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
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
* 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 <colesbury@gmail.com>
Co-authored-by: Christian Heimes <christian@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants


Back | FazBrowse Home | New Git URL