Skip to content

gh-113141: Upgrade mimalloc to upstream v3 - #159718

Draft
DinoV wants to merge 28 commits into
python:mainfrom
DinoV:mimallocv3
Draft

DinoV wants to merge 28 commits into
python:mainfrom
DinoV:mimallocv3

Conversation

@DinoV

@DinoV DinoV commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

This upgrades mimalloc to v3 - the initial work here was done by @daanx and this pushes it over the edge to getting all CI passing and conforming we're at parity with the existing implementation. v3 brings with it baked in qsbr support and it has a number of the CPython specific changes upstreamed now.

To maintain parity with v2 on the pyperformance benchmarks we set several options back to their v2 levels in pycore_mimalloc.h.

There's a new Github CI job added which took 15 seconds to run on this job that verifies we have now modified mimalloc. It is modeled after the expat refresh script / CI job. We sync to a specific mimalloc tag and have the hash recorded. It downloads and verifies that the mimalloc vendored code (which lives in Objects/mimalloc and Include/internal/mimalloc) is unchanged.

@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@read-the-docs-community

read-the-docs-community Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35312361 | 📁 Comparing cff014b against main (415ebff)

  🔍 Preview build  

56 files changed · + 1 added · ± 55 modified

+ Added

± Modified

Comment thread Objects/mimalloc/util/libc.c Fixed
@StanFromIreland

Copy link
Copy Markdown
Member

@daanx can you please sign the CLA?

@daanx

daanx commented Oct 19, 2026

Copy link
Copy Markdown

@daanx can you please sign the CLA?

Ah yes, but not really for the current PR as that includes the full mimalloc v3 vendored sources and that is a separate project under the MIT license.

I can of course sign off for the changes I made on the CPython side. The current PR contains both, maybe we can split it? I think commit cedd47b is the CPython part only, while the other commits are just importing mimalloc v3 as a vendored library.

@daanx daanx left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Using MI_WIN_INIT_USE_FLS is not really encouraged for mimalloc. If we can use the default that would be best.

@DinoV

DinoV commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Using MI_WIN_INIT_USE_FLS is not really encouraged for mimalloc. If we can use the default that would be best.

There's a crash on the Windows builds w/o it currently (and it seems to show up basically at startup but the freeze programs make it through it). I was planning on digging into it more later but this is what Claude came up as a fix - I just don't have immediate access to a Windows machine to see where the actual crash is coming from. But that's why it's there for now.

Right now I'm just trying to get things to green CI and then I'll clean up the final result.

@DinoV

DinoV commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

@corona10 Hey here it is!

@corona10

Copy link
Copy Markdown
Member

For TSAN fail see: 2599016

IIRC, it was already happened at

else {
// if not aligned, free it, overallocate, and unmap around it
// NOTE(sgross): this warning causes issues in Python tests
// _mi_warning_message("unable to allocate aligned OS memory directly, fall back to over-allocation (size: 0x%zx bytes, address: %p, alignment: 0x%zx, commit: %d)\n", size, p, alignment, commit);
mi_os_prim_free(p, size, commit, stats);
if (size >= (SIZE_MAX - alignment)) return NULL; // overflow
const size_t over_size = size + alignment;
if (mi_os_mem_config.must_free_whole) { // win32 virtualAlloc cannot free parts of an allocate block
// over-allocate uncommitted (virtual) memory
p = mi_os_prim_alloc(over_size, 1 /*alignment*/, false /* commit? */, false /* allow_large */, is_large, is_zero, stats);
if (p == NULL) return NULL;
// set p to the aligned part in the full region
// note: this is dangerous on Windows as VirtualFree needs the actual base pointer
// this is handled though by having the `base` field in the memid's
*base = p; // remember the base
p = mi_align_up_ptr(p, alignment);
// explicitly commit only the aligned part
if (commit) {
_mi_os_commit(p, size, NULL, stats);
}
}

@corona10

Copy link
Copy Markdown
Member

About guardrail for memory pressure

any changes expected to increase memory above 15% (with a hard limit of 20%) should be discussed with the SC in advance.

@DinoV

DinoV commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

@DinoV

DinoV commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

@daanx can you please sign the CLA?

Ah yes, but not really for the current PR as that includes the full mimalloc v3 vendored sources and that is a separate project under the MIT license.

I can of course sign off for the changes I made on the CPython side. The current PR contains both, maybe we can split it? I think commit cedd47b is the CPython part only, while the other commits are just importing mimalloc v3 as a vendored library.

FWIW the docs due already call out mimalloc license: https://docs.python.org/3/license.html#mimalloc although we should probably update the date range w/ the v3 update.

@corona10

Copy link
Copy Markdown
Member

perf run is here waiting for results: https://github.com/facebookexperimental/free-threading-benchmarking/actions/runs/39346123598

2x to 1.14x but still higher than mimalloc v2

Comment thread Objects/obmalloc.c Outdated
}
}
// Hooks can run under allocator locks, so only consult cached progress.

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.

Suggested change
// Hooks can run under allocator locks, so only consult cached progress.
if (qsbr != NULL) {
// Scan thread sequences like the v2 integration did.
return !_Py_qsbr_poll(qsbr, page->goal);
}
// Hooks can run under allocator locks, so only consult cached progress.

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.

Similar logic with

if (!_Py_qsbr_poll(tstate->qsbr, page->qsbr_goal)) {

@DinoV

DinoV commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

New benchmark run: https://github.com/facebookexperimental/free-threading-benchmarking/actions/runs/39373144391

@DinoV

DinoV commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

New benchmark run: https://github.com/facebookexperimental/free-threading-benchmarking/actions/runs/39373144391

That was still a 6% regression on memory (but things are headed in the right direction, THP tweak about it from 2x to 1.15, and then the _Py_qsbr_poll fix got us here), here's another run https://github.com/facebookexperimental/free-threading-benchmarking/actions/runs/39476892845 which has commit 3c8be4f that sets mi_option_purge_delay and mi_option_arena_purge_mult to their v2 values.

@DinoV

DinoV commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

@DinoV

DinoV commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Another run with new options for mi_option_arena_purge_min_size, and mi_option_page_full_retain and a tweaked mi_option_arena_purge_mult: https://github.com/facebookexperimental/free-threading-benchmarking/actions/runs/39497633255

@DinoV DinoV changed the title Mimallocv3 gh-113141: Mimallocv3 Oct 20, 2026
@DinoV

DinoV commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

Down to 3%

@DinoV

DinoV commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

This is now neutral: https://github.com/facebookexperimental/free-threading-benchmarking/tree/main/results/bm-20261020-3.16.0a1+dev-a36efb6-NOGIL

It includes one more upstream fix which is a36efb6 that re-enables the mi_arenas_try_purge call which tries to purge expired decommits. This was commented out but not removed in the 3x branch.

@daanx Thanks for merging all of the other upstream PRs I sent. I can send another one to re-enable this but I can also throw it under another ifdef if you're concerned about having it on in the common case. But I think this basically brings us to parity where we could merge this. I'd still want to bring back the latest commits you have upstream but my previous run had that as neutral. I also am assuming your dev3 branch isn't yet released?

@DinoV

DinoV commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

Another benchmark run w/ the latest and greatest mimalloc/dev3 branch merged in: https://github.com/facebookexperimental/free-threading-benchmarking/actions/runs/39651451425

We're now down to 4 vendored in changes which I've opened upstream PRs for (the last being the most questionable I think):
microsoft/mimalloc#1445
microsoft/mimalloc#1446
microsoft/mimalloc#1447
microsoft/mimalloc#1448

@DinoV

DinoV commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor Author

This should also resolve:
#118072
#134070
#113141
#159111
#134070
#133571

@DinoV DinoV changed the title gh-113141: Mimallocv3 gh-113141: Upgrade mimalloc to upstream v3 Oct 21, 2026
@DinoV DinoV added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Oct 21, 2026
@DinoV

DinoV commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

One more run after merging against and updating the define for 3.6 #160360

https://github.com/facebookexperimental/free-threading-benchmarking/actions/runs/41690003577

@DinoV

DinoV commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

It looks like the slice shift tweak hits assertions, doing another perf run w/o it and with the base having it: https://github.com/facebookexperimental/free-threading-benchmarking/actions/runs/41693317724

@maikschulze

Copy link
Copy Markdown
Contributor

While looking into #161420, I tried this PR. In #161420, worker threads allocate 16 MiB bytes objects that the main thread frees, a few at a time. That needs about 150 MiB, but mimalloc 2.5.2 holds back the blocks other threads freed, up to about 10 GB.

The v3 code in this PR would fix that, but one of the option defaults the PR sets for CPython turns the fix off.

Why v3 fixes it. With its default options, v3 abandons a page that becomes full: the page no longer belongs to the allocating thread (_mi_page_to_full() calls _mi_page_abandon()). When another thread frees a block on an abandoned page, that free collects the page right away (mi_free_try_collect_mt()), so the memory can be reused at once.

Why it comes back here. This PR sets page_full_retain to -1 in set_mimalloc_defaults(), and that turns abandoning off (allow_page_abandon = (page_full_retain >= 0)). Full pages then stay with the allocating thread, and frees from other threads are only collected during that thread's housekeeping, every 1,000 generic allocations.

Peak memory, with about 150 MiB needed:

v3 default options page_full_retain = -1
upstream mimalloc v3, a C version of the example 146 MiB 11.9 GB
this PR's build, the script from #161420 1.1 GB (MIMALLOC_PAGE_FULL_RETAIN=2) 10.7–17.2 GB (as configured)

Could abandoning stay enabled, at least for pages of large blocks? The C program and all numbers are in this comment on microsoft/mimalloc#1117.

daanx added a commit to microsoft/mimalloc that referenced this pull request Nov 8, 2026
daanx added a commit to microsoft/mimalloc that referenced this pull request Nov 8, 2026
@daanx

daanx commented Nov 9, 2026 •

Copy link
Copy Markdown

Hi @maikschulze , @DinoV : I agree that using page_full_retain = -1 is not a good setting in general; however, I just pushed fixes to the mimalloc dev3 branch that collect the large allocations more aggressively (and a small extra fix for the mi_thread_id_of to clear it on thread exit). That should fix the issue above; let me know.

However, we should try again also with the default page_full_retain (2). I think there is a mistake in the current PR:

op->ob_tid = mi_page_is_abandoned(page) ? 0 : mi_page_thread_id(page);
if (op->ob_tid == 0) merge_refcount(op, 0);

this should become:

op->ob_tid = mi_thread_id_of(op);   // this gets a CPython thread id (not a mimalloc abandoned page tid) (todo: make this more efficient by avoiding overwriting the tid in the object header (and avoiding this call)?
if (op->ob_tid == 0) merge_refcount(op, 0);

edit: actually, it would be even better to call the internal mi_page_qsbr_thread_id(page) as we have the page anyways (instead of mi_thread_id_of(op)) -- but not sure if that is visible there?

I think with this change we don't need the page_full_retain and can use abandoned pages (which should improve matters). Fingers crossed :-)

(not sure if you can easily pull from the latest dev3 ? but nice to try if we are getting to the point of being a drop-in update. I will make a versioned release later this week).

@maikschulze

Copy link
Copy Markdown
Contributor

Thank you, @daanx! I tried the C version of the example (8 threads, 16 MiB blocks freed by the main thread, about 144 MiB in flight) on the latest dev3: with page_full_retain = -1 ("-1" below), the peak now stays at what the blocks in flight need, and a run is as fast as with the default options again. Before the fix, it spent most of its CPU time in the kernel.

dev3, macOS arm64, median of 5 runs peak RSS, default peak RSS, -1 time, default time, -1
36e29b80a4, before the fix 146 MiB 9,396 MiB 0.12 s 1.97 s
c0ee872fbf 146 MiB 154 MiB 0.12 s 0.11 s
90d604b8d5 (latest) 146 MiB 158 MiB 0.11 s 0.11 s

@daanx

daanx commented Nov 10, 2026 •

Copy link
Copy Markdown

Good see that @maikschulze; @DinoV , I tested the latest mimalloc dev3 with the fix I proposed:

--- a/Python/gc_free_threading.c
+++ b/Python/gc_free_threading.c
@@ -311,8 +311,8 @@ gc_restore_tid(PyObject *op)
         op->ob_tid = 0;
     }
     else {
-        // Reclaimed pages may have a new owner; abandoned pages have none.
-        op->ob_tid = mi_page_is_abandoned(page) ? 0 : mi_page_thread_id(page);
+        // Full-page abandonment preserves the QSBR thread ownership.
+        op->ob_tid = mi_page_qsbr_thread_id(page);
         if (op->ob_tid == 0) {
             merge_refcount(op, 0);
         }

and it all seems to work now with default mimalloc settings, i.e. page_full_retain=2 and page_reclaim_on_free=0.

I used the following (agent generated) test script that tries to probe threads creating abandoned pages that get picked up by other threads and gc'd:
import argparse
from collections import Counter
from concurrent.futures import ThreadPoolExecutor
import ctypes
import faulthandler
import gc
import threading
import sys
import weakref

parser = argparse.ArgumentParser()
parser.add_argument("--reclaim", action="store_true")
args = parser.parse_args()

if sys.implementation.name != "cpython" or sys._is_gil_enabled():
    raise SystemExit("Requires free-threaded CPython with the GIL disabled")

TIMEOUT = 30
COUNT = 20_000

faulthandler.dump_traceback_later(TIMEOUT, exit=True)
gc.disable()


def object_thread_id(obj):
    # ob_tid is the first word of a free-threaded CPython object.
    return ctypes.c_size_t.from_address(id(obj)).value


class Node:
    __slots__ = ("value", "link", "__weakref__")

    def __init__(self, value):
        self.value = value
        self.link = None


state = {}
ready = threading.Event()
reclaimed = threading.Event()
finish = threading.Event()


def allocate():
    objects = [Node(i) for i in range(COUNT)]
    state["owner"] = object_thread_id(objects[0])
    state["freed"] = {id(objects[i]) for i in range(0, COUNT, 2)}
    state["garbage"] = [
        weakref.ref(objects[i]) for i in range(0, COUNT, 2)
    ]

    # Half become unreachable cycles, to be freed by GC on another thread.
    for i in range(0, COUNT, 2):
        objects[i].link = objects[i]
        objects[i] = None

    state["objects"] = objects
    ready.set()
    assert finish.wait(TIMEOUT)
    return sum(obj.value for obj in objects if obj is not None)


def reclaim_pages():
    objects = [Node(i) for i in range(COUNT)]
    state["reclaimer"] = object_thread_id(objects[0])
    state["reused"] = sum(
        id(obj) in state["freed"] for obj in objects
    )
    state["new"] = objects
    reclaimed.set()
    assert finish.wait(TIMEOUT)
    return sum(
        obj.value for obj in state["objects"] if obj is not None
    )


expected_sum = sum(range(1, COUNT, 2))

with ThreadPoolExecutor(max_workers=2) as pool:
    owner = pool.submit(allocate)
    other = None
    try:
        assert ready.wait(TIMEOUT)
        objects = state["objects"]
        refs = [
            weakref.ref(obj) for obj in objects if obj is not None
        ]
        assert {
            object_thread_id(obj)
            for obj in objects if obj is not None
        } == {state["owner"]}

        # Free cycles outside their allocating thread, avoiding owner reclaim.
        gc.collect()
        assert all(ref() is None for ref in state["garbage"])

        expected_owners = {state["owner"]}
        if args.reclaim:
            other = pool.submit(reclaim_pages)
            assert reclaimed.wait(TIMEOUT)
            assert state["reused"] > 0, "No freed slots were reused"
            expected_owners.add(state["reclaimer"])

        for _ in range(2):
            gc.collect()
            owners = Counter(
                object_thread_id(obj)
                for obj in objects if obj is not None
            )
            assert owners.keys() <= expected_owners, owners
            if args.reclaim:
                assert owners[state["reclaimer"]] > 0, owners
            assert sum(
                obj.value for obj in objects if obj is not None
            ) == expected_sum
    finally:
        finish.set()

    assert owner.result() == expected_sum
    if other is not None:
        assert other.result() == expected_sum

del objects
state.clear()
gc.collect()
assert all(ref() is None for ref in refs)

faulthandler.cancel_dump_traceback_later()
print("PASS:", "reclaimed pages" if args.reclaim else "abandoned pages")

To run both scenarios with your free-threaded interpreter:

for retain in 0 2; do
    MIMALLOC_PAGE_FULL_RETAIN=$retain MIMALLOC_PAGE_RECLAIM_ON_FREE=0 \
        ./python -X gil=0 qsbr_gc_test.py

    MIMALLOC_PAGE_FULL_RETAIN=$retain MIMALLOC_PAGE_RECLAIM_ON_FREE=0 \
        ./python -X gil=0 qsbr_gc_test.py --reclaim
done

@maikschulze

Copy link
Copy Markdown
Contributor

Thanks, I reran the #161420 pattern against this PR, in both directions, in case it helps with the page_full_retain choice. In free-threaded builds of main (e8c0584877), large blocks that one thread makes and another frees are much cheaper with this PR's mimalloc v3 (48883a281d merged in, with dev3 6a7e041 vendored instead of v3.6.1) than with mimalloc 2.5.2. On memory, with -1 both directions stay close to main's level; only when worker threads exit does about one block per thread wait until the next garbage collection. With 2, the blocks seem to wait in the thread that frees them, and when that is a pool worker, a large part of what was processed stays in memory. I'm not sure what's best here: I only looked at large blocks passed between threads, which is a small part of the picture.

On speed: 2.5.2 makes an madvise call on every free of a block above 4 MiB from another thread; v3 doesn't. Workers make a bytearray(n) and the main thread frees it, 8 in flight, with the PR's -1 (fastest of 5 rounds of 1 GiB, median of 3 processes, PGO + LTO builds, macOS arm64), GiB/s:

main v3
4 MiB 74 185
8 MiB 75 158
16 MiB 76 145

On memory, the pattern: 16 MiB bytes objects, 8 in flight, 10 GiB in total (10 rounds of 64). Either the workers make them and the main thread frees them, as in #161420, or the main thread reads them from a file and the workers checksum and free them; with one ThreadPoolExecutor kept, or a new one per round, so that the workers exit. "With collections" means gc.collect() after every round; on its own, the pattern triggers 4 or 5 collections. Release builds without PGO, macOS arm64 and Linux arm64 (Docker on the same machine); page_full_retain set through MIMALLOC_PAGE_FULL_RETAIN, the other options as the PR sets them (page_reclaim_on_free is 0). Peak RSS in MiB, medians of 5:

main v3, -1 v3, 2 v3, -1, with collections v3, 2, with collections
macOS, workers make, main frees, one pool kept 426 421 1,016 421 867
macOS, main reads, workers free, one pool kept 84 103 4,851 103 5,493
macOS, workers make, main frees, new pool per round 785 1,452 975 406 790
Linux, workers make, main frees, one pool kept 431 415 1,127 419 861
Linux, main reads, workers free, one pool kept 184 241 11,205 241 11,059
Linux, workers make, main frees, new pool per round 607 1,631 1,026 432 811
  • With -1 and one pool kept, both directions stay within 60 MiB of main. When the workers exit, about one block per exited worker waits (1.4, 2.6 and 5.1 GiB after 10, 20 and 40 rounds on macOS, against 0.7 to 0.8 GiB on main; 2 is lower in this case) until a collection, where _PyMem_ProcessDelayedNoDealloc() collects the abandoned pages. As far as I can tell from counters in an instrumented build, the worker's last page is empty but still delayed when the thread exits; _mi_page_abandon() unlinks it from the thread's QSBR list, and _mi_arenas_page_abandon() leaves a singleton on no list that an allocation or a free would look at (71 to 77 pages, 1.2 to 1.3 GiB, for 80 exited workers).
  • With 2, as far as I can tell, each full singleton page is abandoned right away, and the thread that frees the block reclaims the empty, delayed page into its own QSBR list. When the main thread frees, up to 0.4 GiB waits in its list. When the workers free, about 525 of the 640 pages go to their lists, which they collect only 9 to 18 times in the run, so up to 3.8 GiB waits there, and it grows with the amount processed (5.2, 9.7 and 13.7 GiB for 10, 20 and 40 GiB on macOS, medians of 3). A collection on the main thread doesn't release it, since it only collects its own thread's lists and the abandoned pages. With -1, the main thread keeps these pages and collects its list at every large allocation.
  • How much waits with 2 depends on timing (macOS, single runs): if the main thread copies the blocks in memory instead of reading them from a file, the second row stays at 0.1 GiB; if the workers sleep for 3 ms instead of checksumming, it reaches 1.2 GiB with the copy and 7.9 GiB with the file.
  • Your gc_restore_tid change gives about the same numbers for 2 (in the table's row order: 731, 4,659, 1,036, 945, 11,205 and 785 MiB).
Reproducer (standard library only)
# Large blocks made on one thread and freed on another, 8 in flight: 10 rounds of 64 blocks of 16 MiB.
# usage: python pool_repro.py produce|consume new|keep [gc]
#   produce: workers make the blocks, the main thread frees them (as in gh-161420)
#   consume: the main thread reads the blocks from a file, workers checksum and free them
#   new: a new ThreadPoolExecutor per round (the workers exit); keep: one pool for all rounds
#   gc: call gc.collect() after every round
import gc, resource, sys, zlib
from collections import deque
from concurrent.futures import ThreadPoolExecutor

direction, mode, collect = sys.argv[1], sys.argv[2], sys.argv[3:] == ["gc"]
BLOCK, COUNT, THREADS, ROUNDS = 16 << 20, 64, 8, 10
pattern = bytes(range(256)) * (BLOCK // 256)
source = open("/dev/zero", "rb", buffering=0)

def make():
    return memoryview(pattern).tobytes()  # a new bytes object, as decompress() returns

kept = ThreadPoolExecutor(THREADS) if mode == "keep" else None
for _ in range(ROUNDS):
    pool = kept or ThreadPoolExecutor(THREADS)
    pending = deque()
    for _ in range(COUNT):
        if direction == "produce":
            pending.append(pool.submit(make))  # the block is freed on the main thread
        else:
            pending.append(pool.submit(zlib.crc32, source.read(BLOCK)))  # freed on the worker
        if len(pending) >= THREADS:
            pending.popleft().result()
    while pending:
        pending.popleft().result()
    if kept is None:
        pool.shutdown()  # the workers exit
    if collect:
        gc.collect()

peak = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
peak = peak >> 20 if sys.platform == "darwin" else peak >> 10
print(f"{direction} {mode}{' gc' if collect else ''}: peak RSS {peak} MiB")
for retain in -1 2; do
  for args in "produce keep" "consume keep" "produce new" "produce new gc" "consume keep gc"; do
    MIMALLOC_PAGE_FULL_RETAIN=$retain ./python pool_repro.py $args
  done
done

A probe that suggests the waiting pages are the whole difference (for tracing only, since it lets memory be reused while lock-free readers may still look at it): returning false at the top of _mi_page_qsbr_should_delay() keeps every row of the table at 0.1 to 0.3 GiB, with either setting.

   if (!mi_heap_qsbr_is_enabled(heap)) return false;
+  return false;  // PROBE ONLY: never delay empty pages for QSBR (unsafe in general)
   const bool delayed = mi_page_qsbr_should_delay(&page->qsbr, &heap->qsbr, page->capacity * mi_page_block_size(page));

The counters are a small patch I can share if useful.

Separately: in v3, a block above 31 MiB seems to be purged on every free. As far as I can see, _mi_os_good_alloc_size() rounds such a request up to at least 32 MiB, which reaches the default arena_purge_immediate_size (microsoft/mimalloc#1422, raised to 32 MiB in bbe184ae). When each thread allocates and frees its own block, v3 runs at 54 GiB/s at 31 MiB against 229 with 2.5.2 on macOS (PGO + LTO, medians of 5), and at 105 against 207 on Linux (release builds, medians of 3); with MIMALLOC_ARENA_PURGE_IMMEDIATE_SIZE=1GiB it is back at 188 and 180, and at 30 MiB all of them run at about 195 to 230. A bytearray that grows to 30 MiB reaches it too, as its last step asks for 33.75 MiB.

I used an AI assistant to run these measurements and draft this comment; I reviewed the results myself.

daanx added a commit to microsoft/mimalloc that referenced this pull request Nov 11, 2026
daanx added a commit to microsoft/mimalloc that referenced this pull request Nov 11, 2026
@daanx

daanx commented Nov 11, 2026

Copy link
Copy Markdown

Thanks @maikschulze -- I pushed an update to the latest dev3 branch; I think the main issue is/was that foreign threads should not reclaim abandoned empty qsbr pages (as that can lead to them being not collected for a while). Hope that fixes the differences seen in your experiment. (of course, note that the benchmark is synthetic and tests a worst-case scenario with pure allocators and free'ers).

@maikschulze

Copy link
Copy Markdown
Contributor

Thanks, that was quick! With dev3 at 80899607d2 vendored (otherwise the same builds as above), 2 now comes out where -1 does, in both directions. Peak RSS in MiB, medians of 5, old and new build run side by side:

main 6a7e041, 2 80899607d2, 2 80899607d2, -1
macOS, workers make, main frees, one pool kept 409 1,007 410 422
macOS, main reads, workers free, one pool kept 84 4,547 102 104
Linux, workers make, main frees, one pool kept 499 1,024 440 420
Linux, main reads, workers free, one pool kept 179 9,153 242 242

You're right that the benchmark is synthetic, so, with an AI assistant's help, I put the same builds under a real program: a package manager that extracts .tar.zst archives on 8 threads, one decompress() call per frame, a new pool per archive. With a single extraction, 2 is now level with -1 there too (an archive holding 14 GB: 553 MiB, 829 before). Repeated extractions in one process hit the case that remains, where workers exit and their pages wait for the next collection. That's where the garbage collector's role became clear to me: in my script I had treated it as a side note, but in the real program it decides the outcome. 20 extractions of an archive holding 1.2 GB in one process, no gc.collect(), release builds, macOS arm64, medians of 3:

GIL, 2.5.2 GIL, v3 free-threaded, 2.5.2 free-threaded, v3, -1 free-threaded, v3, 2
peak RSS in MiB 460 468 609 3,294 3,277
collections during the run 21 21 0 0 0

(2.5.2 is main, v3 is 80899607d2; GIL builds with PYTHONMALLOC=mimalloc. With frames in 1 MiB pieces, the program's default on free-threaded builds, the picture is the same: 339 against about 2,000 MiB.) So v3 seems to reveal a gap that reaches user code: on the free-threaded build, this program would need its own gc.collect() calls, which it didn't with 2.5.2. As far as I can tell from counters in instrumented builds and from the code:

  • With v3, about two pages per exited worker end up empty but still delayed by QSBR, most of them already when the thread exits (about 350 singletons, 3.4 GiB, by the end of the run). _mi_arenas_page_abandon() keeps them out of the abandoned maps, so no allocation picks them up; only _mi_heap_collect_abandoned() frees them, and CPython calls that only during a collection.
  • With 2.5.2, allocations that need a new segment free them in mi_segment_try_reclaim(): 3.3 GiB over the same run, no collection needed.
  • And the free-threaded GC never runs here: gc_should_collect() waits until new objects reach a quarter of the long-lived ones (about 8,700 here), and extracting mostly makes bytes.

As an experiment only, I let gc_should_collect() return true once exiting threads had abandoned 64 MiB of such pages (just scheduling a collection wasn't enough, the re-check drops it). The run above then peaks at 663 MiB with 20 collections of about 2 ms each, and my reproducer with a new pool per round (pool_repro.py produce new) at about 400 MiB instead of about 1,500.

@daanx

daanx commented Nov 13, 2026

Copy link
Copy Markdown

Thanks @maikschulze ; The latter issue you raised turned out to be more tricky with the increased RSS. I think I found a reasonable solution though where abandoned empty qsbr-delayed pages from threads that have exited are now retained in a heap qsbr_delayed list. I think this will fix the issue -- can you try it ? It is in the latest dev3. (I made quite a few code changes for this so it might give trouble but let's see).

@maikschulze

Copy link
Copy Markdown
Contributor

Thanks, that does it here, also with v3.7.0 (released 2026-11-17). In the package manager where this started, the peak is back at the level of 2.5.2 (631 against 601 MiB on macOS, with 9548089b04), and pool_repro.py from my comment of 2026-11-11 above gives (peak RSS in MiB, Linux):

pool_repro.py produce new, Linux in use at most main (2.5.2) 80899607d2 (2026-11-12) 9548089b04 (2026-11-13) v3.7.0
peak RSS 195 634 1,693 435 443

The v3 builds are this PR merged into main with the mimalloc version in question; Linux arm64 (Debian 13, GCC 14.2), free-threaded release builds unless noted; the build steps are in the last fold.

It also got faster where workers exit. Blocks made by 8 workers and freed by the main thread, a new pool per round, GiB/s (release builds, fastest of 5 rounds of 1 GiB, median of 5 processes; measured before v3.7.0):

main (2.5.2) 80899607d2 (2026-11-12) 9548089b04 (2026-11-13)
macOS, 4 MiB 60 153 205
macOS, 8 MiB 62 139 209
macOS, 16 MiB 53 142 182
Linux, 4 MiB 40 171 180
Linux, 8 MiB 75 192 202
Linux, 16 MiB 77 165 211

Where each worker frees its own block, the two dev3 builds run the same (within 2% on macOS, 5% on Linux), so I see no cost from the check before each huge allocation.

Beyond that, I compared v3.7.0 with main (2.5.2) more widely: the full test suite on a release build, test_threading on a debug build, the thread sanitizer, a fork loop and a few memory scripts. Five things came up where v3.7.0 behaves differently from main. 1 looks like the CPython side of this PR, 2 and 4 like the mimalloc side; 3 could be either; 5 is for the record.

1. A forked child can hang

I noticed this through test_threading: now and then its fork test leaves a child process that never gets back from os.fork() (under load, 6 of 90 runs with 9548089b04, none of 90 with 2.5.2). In a free-threaded build, CPython pauses all threads that are running Python code before os.fork() (a stop-the-world), and the child then cleans up the threads that don't exist there, including mi_theap_quiescent_abandon() for their theaps. A thread that is just starting or ending does some mimalloc work outside Python code (mi_theap_attach_to_heap() at the start, mimalloc's thread-exit handler at the end), so the pause doesn't wait for it. If it holds heap->theaps_lock at the fork, the child inherits the lock locked and waits for it forever: in mi_theap_quiescent_abandon(), or in mi_theap_attach_to_heap() if it starts a thread.

So it takes os.fork() (or the fork start method of multiprocessing) while another thread starts or ends; subprocess without preexec_fn isn't affected. I see it with v3.6.1 as in this PR and with v3.7.0, not with 2.5.2. It is rare in normal use; the loop below makes it likely.

The fork notes give CPython's stop-the-world as an example of all other threads being at a safe point outside mimalloc. As far as I can tell it doesn't cover these starting and ending threads, so this may need something on the CPython side of this PR, and perhaps a word in the notes. Letting mi_theap_quiescent_abandon() skip threads from before the fork didn't help on its own when I tried it on v3.6.1, since the lock stays locked in the child. On the CPython side, a lock that a thread takes around those two places and that os.fork() holds across the fork made the loop pass on v3.7.0 (20 of 20 copies hung without it, none with it; the sketch is in the fold); thread_run() then has to check for shutdown and bind under that lock in one go, or a starting thread hits the window of #162109 more often.

The loop, and the sketch I used

fork_hang.py ROUNDS [thread], 400 rounds, 20 copies at a time (a shorter version of the script I ran, which also prints the stuck child's backtrace):

import os, sys, threading, time

def do_fork(pids):
    pid = os.fork()
    if pid == 0:
        if sys.argv[2:] == ["thread"]:
            t = threading.Thread(target=lambda: None)
            t.start()
            t.join()
        os._exit(50)
    pids.append(pid)

for r in range(int(sys.argv[1])):
    pids = []
    threads = [threading.Thread(target=do_fork, args=(pids,)) for _ in range(16)]
    for t in threads:
        t.start()
    for t in threads:
        t.join()
    deadline = time.monotonic() + 20
    for pid in pids:
        while os.waitpid(pid, os.WNOHANG)[0] == 0:
            if time.monotonic() > deadline:
                sys.exit(f"child {pid} of round {r} is still running")
            time.sleep(0.01)
print("no hang")

The sketch, against this PR at 48883a2 merged into main at e8c0584:

--- a/Include/internal/pycore_pystate.h
+++ b/Include/internal/pycore_pystate.h
@@ -242,6 +242,7 @@
     PyInterpreterState *interp,
     int whence);
 extern void _PyThreadState_Bind(PyThreadState *tstate);
+extern int _PyThreadState_BindUnlessMustExit(PyThreadState *tstate);
 PyAPI_FUNC(PyThreadState *) _PyThreadState_NewBound(
     PyInterpreterState *interp,
     int whence);
@@ -249,6 +250,16 @@
 extern void _PyThreadState_DeleteList(PyThreadState *list, int is_after_fork);
 extern void _PyThreadState_ClearMimallocHeaps(PyThreadState *tstate);
 
+#ifdef Py_GIL_DISABLED
+// Held by a thread while it runs code before it is attached or after its
+// thread state is gone that takes locks a stop-the-world cannot see
+// (mimalloc, the table of biased reference counting), and by os.fork()
+// across the fork, so that the child does not inherit them half done.
+extern void _PyThreadState_UnattachedLock(void);
+extern void _PyThreadState_UnattachedUnlock(void);
+extern void _PyThreadState_UnattachedReInit(void);
+#endif
+
 // Export for '_testinternalcapi' shared extension
 PyAPI_FUNC(PyObject*) _PyThreadState_GetDict(PyThreadState *tstate);
 
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -9,6 +9,7 @@
 #include "pycore_moduleobject.h"  // _PyModule_GetState()
 #include "pycore_object_deferred.h" // _PyObject_SetDeferredRefcount()
 #include "pycore_pylifecycle.h"
+#include "pycore_mimalloc.h"      // mi_thread_done()
 #include "pycore_pystate.h"       // _PyThreadState_SetCurrent()
 #include "pycore_time.h"          // _PyTime_FromSeconds()
 #include "pycore_tuple.h"         // _PyTuple_FromPairSteal
@@ -371,7 +372,7 @@
     // _PyRuntimeState_SetFinalizing() is called. At this point, all Python
     // threads must exit, except of the thread calling Py_Finalize() which
     // holds the GIL and must not exit.
-    if (_PyThreadState_MustExit(tstate)) {
+    if (!_PyThreadState_BindUnlessMustExit(tstate)) {
         // Don't call PyThreadState_Clear() nor _PyThreadState_DeleteCurrent().
         // These functions are called on tstate indirectly by Py_Finalize()
         // which calls _PyInterpreterState_Clear().
@@ -382,7 +383,6 @@
         goto exit;
     }
 
-    _PyThreadState_Bind(tstate);
     PyEval_AcquireThread(tstate);
     _Py_atomic_add_ssize(&tstate->interp->threads.count, 1);
 
@@ -406,6 +406,15 @@
     PyThreadState_Clear(tstate);
     _PyThreadState_DeleteCurrent(tstate);
 
+#if defined(Py_GIL_DISABLED) && defined(WITH_MIMALLOC)
+    // Let mimalloc give up this thread's heaps here, under the lock that
+    // os.fork() holds across the fork, and not only in its thread-exit
+    // handler, which runs when no stop-the-world can wait for this thread.
+    _PyThreadState_UnattachedLock();
+    mi_thread_done();
+    _PyThreadState_UnattachedUnlock();
+#endif
+
 exit:
     // Don't need to wait for this thread anymore
     remove_from_shutdown_handles(handle);
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -715,6 +715,9 @@
 
     _PyImport_AcquireLock(interp);
     _PyEval_StopTheWorldAll(&_PyRuntime);
+#ifdef Py_GIL_DISABLED
+    _PyThreadState_UnattachedLock();
+#endif
     HEAD_LOCK(&_PyRuntime);
     _PyWarnings_BeforeFork(interp);
 }
@@ -723,6 +726,9 @@
 PyOS_AfterFork_Parent(void)
 {
     HEAD_UNLOCK(&_PyRuntime);
+#ifdef Py_GIL_DISABLED
+    _PyThreadState_UnattachedUnlock();
+#endif
     _PyEval_StartTheWorldAll(&_PyRuntime);
 
     PyInterpreterState *interp = _PyInterpreterState_GET();
@@ -753,6 +759,9 @@
     PyStatus status;
     _PyRuntimeState *runtime = &_PyRuntime;
 
+#ifdef Py_GIL_DISABLED
+    _PyThreadState_UnattachedReInit();
+#endif
 #ifdef WITH_MIMALLOC
     _mi_fork_child();
 #endif
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -163,9 +163,40 @@
 
 static void tstate_mimalloc_bind(PyThreadState *);
 
+#ifdef Py_GIL_DISABLED
+static PyMutex unattached_mutex;
+
+void
+_PyThreadState_UnattachedLock(void)
+{
+    PyMutex_LockFlags(&unattached_mutex, _Py_LOCK_DONT_DETACH);
+}
+
+void
+_PyThreadState_UnattachedUnlock(void)
+{
+    PyMutex_Unlock(&unattached_mutex);
+}
+
+void
+_PyThreadState_UnattachedReInit(void)
+{
+    unattached_mutex = (PyMutex){0};
+}
+#endif
+
+static void
+bind_tstate_impl(PyThreadState *tstate, int lock_held);
+
 static void
 bind_tstate(PyThreadState *tstate)
 {
+    bind_tstate_impl(tstate, 0);
+}
+
+static void
+bind_tstate_impl(PyThreadState *tstate, int lock_held)
+{
     assert(tstate != NULL);
     assert(tstate_is_alive(tstate) && !tstate->_status.bound);
     assert(!tstate->_status.unbound);  // just in case
@@ -184,6 +215,12 @@
 #endif
 
 #ifdef Py_GIL_DISABLED
+    // This thread is not attached yet, so a stop-the-world does not wait for
+    // it; os.fork() holds this lock across the fork.
+    if (!lock_held) {
+        _PyThreadState_UnattachedLock();
+    }
+
     // Initialize biased reference counting inter-thread queue. Note that this
     // needs to be initialized from the active thread.
     _Py_brc_init_thread(tstate);
@@ -192,6 +229,14 @@
     // mimalloc state needs to be initialized from the active thread.
     tstate_mimalloc_bind(tstate);
 
+#ifdef Py_GIL_DISABLED
+    if (!lock_held) {
+        _PyThreadState_UnattachedUnlock();
+    }
+#else
+    (void)lock_held;
+#endif
+
     tstate->_status.bound = 1;
 }
 
@@ -2826,6 +2871,30 @@
     }
 }
 
+// For a new thread: bind its thread state unless the interpreter is shutting
+// down. Returns 0 if the thread must exit. The check and the bind are done
+// under the lock that os.fork() holds across a fork, so that waiting for it
+// cannot come between the two.
+int
+_PyThreadState_BindUnlessMustExit(PyThreadState *tstate)
+{
+#ifdef Py_GIL_DISABLED
+    _PyThreadState_UnattachedLock();
+#endif
+    int must_exit = _PyThreadState_MustExit(tstate);
+    if (!must_exit) {
+        assert(_PyThreadState_CheckConsistency(tstate));
+        bind_tstate_impl(tstate, 1);
+        if (gilstate_get() == NULL) {
+            bind_gilstate_tstate(tstate);
+        }
+    }
+#ifdef Py_GIL_DISABLED
+    _PyThreadState_UnattachedUnlock();
+#endif
+    return !must_exit;
+}
+
 #if defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API)
 uintptr_t
 _Py_GetThreadLocal_Addr(void)

2. A debug-build assertion

With --with-pydebug, a mimalloc assertion fires when the garbage collector visits the heaps (test_threading aborts on it):

mimalloc: assertion failed: at "Objects/mimalloc/heap/theap.c":769, _mi_theap_area_visit_blocks
  assertion: "free_list_corrupted || page->capacity == (free_count + mi_page_used(page))"

A thread exits and leaves a partly used page, another thread frees some of its objects, then a collection visits the page:

import gc, threading

class C:
    pass

keep = []
def worker():
    keep.append([C() for _ in range(100)])

t = threading.Thread(target=worker); t.start(); t.join()
del keep[0][::2]   # the main thread frees half of the worker's objects
gc.collect()       # the assertion fires here

The blocks freed by the other thread are still on local_free when the visitor builds its free map from page->free. mi_heap_visit_page() calls _mi_page_free_collect(page, true) only for pages that aren't abandoned, and _mi_abandoned_page_unown() collects without force, so local_free isn't appended to a non-empty free. On 9548089b04 (2026-11-13), the former condition (if (!abandoned || claimed)) made it go away, and 80899607d2 (2026-11-12) doesn't hit it, so perhaps the visit could collect a claimed abandoned page with force again, as before 9108cd8ac9. In release builds I found no effect: within a collection the only heap visit before the abandoned pages are collected is the debug-only validate_refcounts.

3. A warning at exit in debug builds

$ ./python -c "import threading, time; threading.Thread(target=time.sleep, args=(100,), daemon=True).start(); time.sleep(0.2)"
mimalloc: warning: thread 0xFFFFA4CDD740: deleting heap 0x020000150180 while 1 thread(s) did not release it first (see mi_thread_heap_release)

CPython deletes its heaps at interpreter exit without calling mi_thread_heap_release() (the daemon threads are still alive, so it can't release on their behalf), so with daemon threads alive this goes to stderr in debug builds, and test cases that expect an empty stderr fail (ten with 9548089b04, six of them in test_threading). Release builds don't print it.

4. The data race of #129748 is back

Freeing a block writes the free-list link into the block's first word, which is the object's ob_tid, while another thread may be trying to take a reference to that object (_Py_TryIncrefFast(), which PEP 703 allows). Main carries #134238 for this in its 2.5.2 copy: the link is stored with mi_atomic_store_relaxed(). v3.7.0 still has a plain load and store. Reports of this race per 5 s run of the script in the fold (./configure --disable-gil --with-thread-sanitizer, TSAN_OPTIONS=halt_on_error=0; each of the v3.7.0 reports involves mi_block_set_next() or mi_block_link()):

run 1 run 2 run 3
main (2.5.2) 0 0 0
this PR (v3.6.1) 10 9 11
v3.7.0 8 9 11
9548089b04 (2026-11-13) with the change below 0 0 0

The TSan job here builds with --with-pydebug, where the debug allocator's extra words in front of each block keep the link away from ob_tid, so it can't show this. I tested the change on v3.6.1 and 9548089b04; v3.7.0 has the same two lines. Since the vendored copy has to match the upstream release, I take it this would be an upstream change.

The change, and the script
--- a/include/mimalloc/alloc/page.h
+++ b/include/mimalloc/alloc/page.h
@@ -816,7 +816,7 @@
 
 static inline mi_encoded_t mi_block_link(const mi_block_t* block) {
   mi_track_mem_defined(block,sizeof(mi_block_t));
-  const mi_encoded_t x = block->next;
+  const mi_encoded_t x = mi_atomic_load_relaxed((_Atomic(mi_encoded_t)*)&block->next);
   mi_track_mem_noaccess(block,sizeof(mi_block_t));
   return x;
 }
@@ -833,7 +833,7 @@
 
 static inline void mi_block_set_next(const mi_page_t* page, mi_block_t* block, const mi_block_t* next) {
   mi_track_mem_undefined(block,sizeof(mi_block_t));
-  block->next = mi_block_encode(page, block, next);
+  mi_atomic_store_relaxed((_Atomic(mi_encoded_t)*)&block->next, mi_block_encode(page, block, next));
   mi_track_mem_noaccess(block,sizeof(mi_block_t));
 }
 

Four readers load a slot while one writer replaces its value:

import sys, threading, time

class C:
    __slots__ = ("x",)

seconds = float(sys.argv[1]) if len(sys.argv) > 1 else 5.0
c = C()
c.x = object()
stop = []

def reader():
    while not stop:
        for _ in range(1000):
            c.x

def writer():
    while not stop:
        for _ in range(1000):
            c.x = object()          # frees the previous value

threads = [threading.Thread(target=reader) for _ in range(4)] + [threading.Thread(target=writer)]
for t in threads:
    t.start()
time.sleep(seconds)
stop.append(1)
for t in threads:
    t.join()

5. Two memory properties

  • Empty pages that no thread owns. Workers of a new pool per phase make small objects, the main thread frees them after the workers have exited, and (counters of a diagnostic build on 9548089b04) the emptied pages are left abandoned with their QSBR goal; only an allocation of the same size class or a collection looks at them again. Since 7f615b54 (2026-11-11) the freeing thread no longer takes such a page into its own heap. Ten phases of one size each (size_classes.py new, in the fold), peak RSS in MiB on Linux: 805 with 2.5.2, 3,632 with v3.6.1, 6,054 with v3.7.0, and 841 with v3.7.0 and a gc.collect() after each phase.
  • Growth with a kept pool, Free threading memory leak #145615 again. Its workload (a kept pool, the main thread frees and collects every round; gh145615_kinds.py 100 bytearray, in the fold) grows with v3.7.0: resident set after 100 rounds 123 MiB with 2.5.2, 133 with v3.6.1, 746 with v3.7.0. The owner looks at its full pages in mi_theap_collect_special_pages(), at most 8 per call and right before it takes a fresh page, so the all-free pages further back in the queue aren't reached. On 9548089b04, searching the size-class queue once more before the fresh page kept it level (132 MiB; the probe is in the fold).
The scripts, and the probe

size_classes.py new|keep [gc]:

# Phases with small bytes objects of one size each. Per phase, 8 workers make 512 MiB of them, all
# kept; then the main thread frees them. At most 512 MiB (plus the lists) is alive.
# usage: python size_classes.py new|keep [gc]
#   new: a new ThreadPoolExecutor per phase (the workers exit before the frees); keep: one pool for all phases
import gc, resource, sys
from concurrent.futures import ThreadPoolExecutor

mode, collect = sys.argv[1], sys.argv[2:] == ["gc"]
SIZES = [64, 100, 160, 250, 400, 640, 1000, 1600, 2500, 4000]    # payload bytes: ten size classes
TOTAL, THREADS = 512 << 20, 8

def make(job):
    size, n = job
    return [b"x" * size for _ in range(n)]

kept = ThreadPoolExecutor(THREADS) if mode == "keep" else None
for size in SIZES:
    pool = kept or ThreadPoolExecutor(THREADS)
    parts = list(pool.map(make, [(size, TOTAL // (size + 33) // THREADS)] * THREADS))
    if kept is None:
        pool.shutdown()              # the workers exit
    while parts:
        parts.pop()                  # freed on the main thread
    if collect:
        gc.collect()

peak = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print("size_classes", *sys.argv[1:], "peak RSS", peak >> 20 if sys.platform == "darwin" else peak >> 10, "MiB")

gh145615_kinds.py ITERATIONS KIND:

# The workload of https://github.com/python/cpython/issues/145615 with the kind of object as a parameter, and a
# memory reading that also works on macOS (physical footprint there, resident set on Linux).
#   python gh145615_kinds.py [ITERATIONS] [bytearray|bytes|list|dict|object] [nogc] [census]
# nogc: without the two gc.collect() calls per iteration; census: sys._debugmallocstats() at the end (diagnostic build).
# bytearray is the issue's own case (a small object plus a 64 byte buffer from PyMem_Malloc).
import concurrent.futures, gc, os, sys

if sys.platform == "darwin":
    import ctypes
    _buf = (ctypes.c_uint64 * 64)()
    _lib = ctypes.CDLL(None)
    def mem_mib():
        _lib.proc_pid_rusage(os.getpid(), 4, ctypes.byref(_buf))
        return _buf[2:][7] >> 20                     # physical footprint
else:
    def mem_mib():
        with open("/proc/self/statm") as f:
            return int(f.read().split()[1]) * os.sysconf("SC_PAGE_SIZE") >> 20

class C:
    pass

MAKE = {
    "bytearray": lambda: bytearray(64),
    "bytes": lambda: bytes(64),
    "list": lambda: [None] * 8,
    "dict": lambda: {"a": 1},
    "object": lambda: C(),
}

iterations = int(sys.argv[1]) if len(sys.argv) > 1 else 100
kind = sys.argv[2] if len(sys.argv) > 2 else "bytearray"
make = MAKE[kind]
use_gc = "nogc" not in sys.argv

def work(_):
    return [make() for _ in range(4000)]

pool = concurrent.futures.ThreadPoolExecutor(max_workers=4)
readings = []
for i in range(iterations):
    futures = [pool.submit(work, j) for j in range(100)]
    results = [f.result() for f in futures]
    del futures, results  # free worker-allocated objects on main thread
    if use_gc:
        gc.collect()
        gc.collect()
    if i == 0 or (i + 1) % (iterations // 4) == 0:
        readings.append(mem_mib())
if "census" in sys.argv:
    sys._debugmallocstats()
pool.shutdown()
print(f"{kind}{'' if use_gc else ', no gc.collect()'}, {iterations} iterations: memory in MiB at iteration 1 and after each quarter: {', '.join(map(str, readings))}")

The probe, on 9548089b04:

--- a/src/alloc/alloc-generic.c
+++ b/src/alloc/alloc-generic.c
@@ -177,6 +177,10 @@
 
   if (page == NULL) {
     _mi_theap_collect_retired(theap, false); // perhaps make a page available
+    if (first_try && pq->last != NULL && mi_page_immediate_available(pq->last)) {   // PROBE: use what the scan made available
+      page = mi_page_queue_find_free_ex(theap, pq, false);
+      if (page != NULL) { return page; }
+    }
     page = _mi_page_fresh(theap, pq);
     mi_assert_internal(page == NULL || mi_page_immediate_available(page));
     if (page == NULL && first_try) {
How the builds were made

This PR merged into main, with a mimalloc release copied over the vendored files, as Objects/mimalloc/refresh.sh does. The merge conflicts only inside Objects/mimalloc/, which is replaced anyway. The CPython side of this PR still reads theap->qsbr_first, which is qsbr_delayed since 9108cd8ac9 (2026-11-13), so the build needs the sed line.

git checkout e8c0584877e0b0a82ee8e0707336d8df4d68ccb4
git merge 48883a281d72773b18c11b3ad0eddbb455f50f70          # conflicts in Objects/mimalloc/ only
curl -L https://github.com/microsoft/mimalloc/archive/refs/tags/v3.7.0.tar.gz | tar xz -C /tmp
find Objects/mimalloc -type f ! -name refresh.sh -delete
rm -rf Include/internal/mimalloc && mkdir Include/internal/mimalloc
cp -R /tmp/mimalloc-3.7.0/src/. Objects/mimalloc/
cp -R /tmp/mimalloc-3.7.0/include/. Include/internal/mimalloc/
cp /tmp/mimalloc-3.7.0/LICENSE Objects/mimalloc/
sed -i.bak 's/theap->qsbr_first != NULL/theap->qsbr_delayed != NULL/' Objects/obmalloc.c
./configure --disable-gil && make -j8   # --with-pydebug for items 2 and 3, --with-thread-sanitizer for item 4

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