Skip to content

gh-161420: Process mimalloc's delayed frees on every generic malloc again - #161533

Merged
corona10 merged 2 commits into
python:mainfrom
maikschulze:mimalloc-delayed-free
Nov 7, 2026
Merged

corona10 merged 2 commits into
python:mainfrom
maikschulze:mimalloc-delayed-free

Conversation

@maikschulze

@maikschulze maikschulze commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Since mimalloc 2.5.2 (gh-159111), _mi_malloc_generic() processes delayed frees from other threads only every 100th call, so blocks over 64 KiB that other threads freed can pile up, up to about 100 per heap. This processes them on every generic malloc again, as before.

Peak RSS of the example from the issue, free-threaded release builds:

Peak RSS main this PR
macOS 5,900–8,000 MiB 410–500 MiB
Linux 9,500–11,000 MiB 420–540 MiB
Windows 8,000–8,200 MiB 430–450 MiB

No measurable difference for small objects (pyperf, macOS and Linux).

I used an AI assistant to investigate this and draft the patch; I reviewed and tested the changes myself.

…lloc again

Since the update to mimalloc 2.5.2 (pythongh-159111), _mi_malloc_generic() processes the heap's delayed
frees only every 100 calls. A block over MI_MEDIUM_OBJ_SIZE_MAX has a page of its own, so a free of
it from another thread is a delayed free, and every allocation of such a block is a generic malloc:
each thread could keep up to about 100 large blocks that other threads had freed. Process the
delayed frees on every generic malloc again, as mimalloc 2.1 did; the deferred-free callback and
the periodic collection stay batched.
Comment thread Objects/mimalloc/page.c Resolved

@corona10 corona10 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.

Please write descption to be concisely as possible.

@bedevere-app

bedevere-app Bot commented Nov 7, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Co-authored-by: Donghee Na <donghee.na92@gmail.com>
@python-cla-bot

python-cla-bot Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@maikschulze

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again

@bedevere-app

bedevere-app Bot commented Nov 7, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@corona10: please review the changes made to this pull request.

@bedevere-app
bedevere-app Bot requested a review from corona10 November 7, 2026 07:01

@corona10 corona10 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.

lgtm!

@corona10
corona10 enabled auto-merge (squash) November 7, 2026 07:12
@corona10
corona10 merged commit b8dd06f into python:main Nov 7, 2026
61 of 64 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @maikschulze for the PR, and @corona10 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Nov 7, 2026

Copy link
Copy Markdown

GH-161537 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Nov 7, 2026
@bedevere-app

bedevere-app Bot commented Nov 7, 2026

Copy link
Copy Markdown

GH-161538 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Nov 7, 2026
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.

2 participants