Skip to content

gh-158972: Drain each thread's page list during GC - #160150

Open
mpage wants to merge 2 commits into
python:mainfrom
mpage:gh-158972-drain-page-list
Open

mpage wants to merge 2 commits into
python:mainfrom
mpage:gh-158972-drain-page-list

Conversation

@mpage

@mpage mpage commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Free pages awaiting QSBR are added to each thread's mimalloc.page_list.
The list is only drained if the owning thread needs new pages, performs
an allocation larger than mimalloc's medium-object limit, or the thread
exits. This means that we don't free pages if a thread's allocation
pattern quiesces or falls into a pattern that can be serviced by
existing pages. This can be problematic for threads that allocate a
bunch of memory and then settle into the previous pattern.

It's worth noting that this may increase GC pause times, though it's not clear
by how much. There are a few alternatives if folks are concerned about overhead:

  • Only drain the page_list if over a certain size threshold.
  • Only drain the page_list on manual calls to gc.collect.

@python-cla-bot

python-cla-bot Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

mpage added 2 commits October 21, 2026 16:38
Free pages awaiting QSBR are added to each thread's `mimalloc.page_list`.
The list is only drained if the owning thread needs new pages, performs
an allocation larger than mimalloc's medium-object limit, or the thread
exits. This means that we don't free pages if a thread's allocation
pattern quiesces or falls into a pattern that can be serviced by
existing pages. This can be problematic for threads that allocate a
bunch of memory and then settle into the previous pattern.
Add _testinternalcapi.get_qsbr_page_list_length(), which counts the pages on
all threads' QSBR page lists, and use it to check that gc.collect() frees
empty pages parked by the calling thread and by an idle thread. Also check
that the GC skips thread states that have not initialized their mimalloc
state yet.
@mpage
mpage force-pushed the gh-158972-drain-page-list branch from d28e6a5 to d689f54 Compare October 21, 2026 23:40
@mpage
mpage requested review from DinoV, corona10 and nascheme October 22, 2026 17:16
@mpage
mpage marked this pull request as ready for review October 22, 2026 17:20

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

Hmm let me take a look also!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants