Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental memory allocator with vendored mimalloc #29123

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

Projects
None yet
4 participants
@tiran
Copy link
Member

@tiran tiran commented Oct 21, 2021

EXPERIMENTAL This is an experimental pull request and a proof of concept. mimalloc integration is in a very early stage.

Vendor and hook up mimalloc. The PR only replaces raw memory allocator and Python's internal obmalloc with mimalloc. It does not use any of the advanced features of mimalloc such as heaps.

  • Includes mimalloc v1.7.1 and additional fixes
  • --with-mimalloc=yes by default (for testing)
  • --with-pymalloc=no by default
  • --with-freelists=yes

mimalloc requires C11 <stdatomic.h> on POSIX platforms to provide atomic operations such as CAS. GCC on RHEL 7 / CentOS 7 is too old. GCC 4.9 and clang 3.6 seem to be minimum versions. Users need install Red Hat Developer Toolset 10.0 from Software Collections. The CentOS 7-based manylinux2014 container image comes with GCC 10.2 pre-installed.

related mimalloc tickets

Initial benchmarks

Initial benchmarks should be taken with a big grain of salt. It seems like mimalloc is in the same ballpark as libc malloc + obmalloc with slightly better performance in some cases. I haven't measured memory consumption yet.

with mimalloc, without pymalloc, without freelists

In general about same performance, with some benchmarks 7% faster to 12% slower, https://gist.github.com/pablogsal/33fce1cbe5f908c1d85cae5d678f50a6

with mimalloc, without pymalloc, with freelists

In general 2% speedup, with some benchmarks 9% faster to 6% slower, https://gist.github.com/pablogsal/8027937b71cd30f17aaaa5ef7c885d3e

debug output

>>> sys._debugmallocstats()
mimalloc (version: 171)
heap stats:     peak      total      freed    current       unit      count  
  reserved:   256.0 mb   256.0 mb       0 b    256.0 mb       1 b              not all freed!
 committed:    20.0 mb    20.0 mb       0 b     20.0 mb       1 b              not all freed!
     reset:     1.0 mb     1.0 mb       0 b      1.0 mb       1 b              not all freed!
   touched:       0 b        0 b    932.3 kb  -932.3 kb       1 b              ok
  segments:       5          5          1          4                           not all freed!
-abandoned:       0          0          0          0                           ok
   -cached:       0          0          0          0                           ok
     pages:      94        123         29         94                           not all freed!
-abandoned:       0          0          0          0                           ok
 -extended:       0   
 -noretire:       0   
     mmaps:       0   
   commits:      69   
   threads:       0          0          0          0                           ok
  searches:     0.0 avg
numa nodes:       1
   elapsed:       5.572 s
   process: user: 0.073 s, system: 0.007 s, faults: 0, rss: 11.9 mb, commit: 20.0 mb
...
@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Oct 24, 2021

🤖 New build scheduled with the buildbot fleet by @tiran for commit a4e7165 🤖

If you want to schedule another build, you need to add the "🔨 test-with-buildbots" label again.

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Oct 25, 2021

🤖 New build scheduled with the buildbot fleet by @tiran for commit b6f5965 🤖

If you want to schedule another build, you need to add the "🔨 test-with-buildbots" label again.

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Oct 25, 2021

🤖 New build scheduled with the buildbot fleet by @tiran for commit 11efb79 🤖

If you want to schedule another build, you need to add the "🔨 test-with-buildbots" label again.

tiran and others added 8 commits Oct 25, 2021
Signed-off-by: Christian Heimes <christian@python.org>
Signed-off-by: Christian Heimes <christian@python.org>
Co-authored-by: Dong-hee Na <donghee.na@python.org>
Signed-off-by: Christian Heimes <christian@python.org>
Signed-off-by: Christian Heimes <christian@python.org>
ifeq is not portable. Original patch by Neil.

Co-authored-by: Neil Schemenauer <nas@arctrix.com>
Signed-off-by: Christian Heimes <christian@python.org>
tiran added 13 commits Oct 25, 2021
See: microsoft/mimalloc#426
Signed-off-by: Christian Heimes <christian@python.org>
Use mimalloc's ``static.c`` to include mimalloc library into obmalloc.
This allows us to prefix all symbols with ``_Py_`` prefix.

Signed-off-by: Christian Heimes <christian@python.org>
HAVE_FINITE and HAVE_*GAMMA are not used any more. This fixes a build
issue. Configure doesn't detect finite and gamma on macOS.

Signed-off-by: Christian Heimes <christian@python.org>
@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Oct 25, 2021

🤖 New build scheduled with the buildbot fleet by @tiran for commit 8d7373a 🤖

If you want to schedule another build, you need to add the "🔨 test-with-buildbots" label again.

@insilications
Copy link

@insilications insilications commented Oct 25, 2021

Saw your PR in microsoft/mimalloc#475. This is great. I'm using mimalloc as a drop-in replacement with the ENV variables:

PYTHONMALLOC=malloc MIMALLOC_PAGE_RESET=0 MIMALLOC_LARGE_OS_PAGES=1 LD_PRELOAD='/usr/lib64/libmimalloc.so'

I expected that this upstream integration would bring other benefits, but it would not be able to use mimalloc's allocation of large OS pages?

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Oct 26, 2021

🤖 New build scheduled with the buildbot fleet by @tiran for commit 8d7373a 🤖

If you want to schedule another build, you need to add the "🔨 test-with-buildbots" label again.

Signed-off-by: Christian Heimes <christian@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment