Repository navigation
Let threads past max_heaps borrow spare heaps instead of failing - #9
Merged
Merged
Conversation
This was referenced Nov 14, 2026
A thread that found all max_heaps heaps taken got null from every allocation, so the program aborted at thread max_heaps + 1, 4097 by default. The global allocator's region now has spare heaps past max_heaps that Region::heap never hands out, four per online CPU and between 8 and 1024. A thread without a heap of its own borrows one for each allocation or resize: a compare-and-swap on the slot's claim, the operation as on a bound heap, and a store to release it. A spare keeps its bins and slabs from one borrower to the next. A thread asks for the spare it had last, or for its first borrow one hashed from its thread-local address, so threads that share the spares spread over them. A borrower that finds every spare taken spins for a moment and then yields. Frees from such a thread already went home through Region::dealloc without a heap, and still do. With one spare per CPU, 64 threads past the cap on 32 CPUs often found every spare held and yielded, and ran six times slower than with four per CPU. With four, they run at about a quarter of the speed of threads with a heap of their own, from 1 to 256 threads at once. Destructors that run after a thread's heap went back at exit, and allocations made while a thread binds its heap, used to claim a whole heap for one call and empty it afterwards. They borrow a spare now too. Region::heap counts the free slots and takes one before it looks for a slot, so a thread past the cap learns in one load that none is free, instead of a pass over every slot on every call, and it binds a heap of its own on its first call after one is given back. Binding writes the directory only once it has a heap, so threads that retry on every call do not keep writing the line that every free reads. trim empties the spares, and metrics count them. The fast paths are unchanged: only the binding, unbound and thread exit paths compile differently.
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The limit. A thread that found all
max_heapsheaps taken got null from every allocation. A program therefore aborted when it started threadmax_heaps + 1, which is 4097 by default.The fallback:
max_heapsthatRegion::heapnever hands out: four per online CPU, between 8 and 1024. A thread without a heap of its own borrows one for each allocation or resize: a compare-and-swap on the slot's claim, the operation exactly as on a bound heap, then a store to release it.Region::deallocwithout a heap, and still do.Region::heapkeeps a count of free slots and takes one before it looks for a slot.trimand metrics include the spares.trimtakes each slot without resetting it, so a spare's bins survive.Stats::heaps_in_usestill counts only heaps held by threads.Mapping::newhave no spares, so aRegionused directly behaves as before.Threads past the cap, on the EPYC box (32 CPUs, C library, 4100 threads holding heaps, workers doing malloc/free of 64 blocks):
Cost to everyone else: none measurable.
create,bind,alloc_unbound,realloc_unbound,claim/adopt,Region::heap,release_heap), checked against a symbol-size diff of the bench binary.Commit::Lockpins the region.bulk 32 B shuffled−2.4% (cold only). Bestpair 256 KiB+8%.large blocks−1.3%.alloc_generalwith identical instructions at a 16-byte different address costpair 16 KiB6%. That is why the aligned run is the one to trust.Docs: the README limits and configuration table, DESIGN.md sections 6, 12, 13 and 14, the
max_heapsdocs, and theglobalandtlsmodule docs.Tests:
global::threads_past_max_heaps_borrow_spares_until_a_heap_is_freeholds all 4 heaps on 4 threads.callocs,realloc_unsizeds and frees through the spares, and checks that it is unbound.trimgives back every chunk.heap::spare_heaps_serve_one_borrower_at_a_time_keep_their_slabs_and_are_trimmedchecks four things:heap()never hands out a spare, a held spare is passed over, a spare keeps its slab for the next borrower, andtrimempties it.a_claim_racing_a_release_finds_the_slot_or_leaves_it_freechecks that the free-slot count is never lost or double-counted.threads_without_a_heap_never_share_a_sparemakes two threads wait on one spare.trimskipping the spares, and a borrow that ignores the claim. Each break fails at least one of the new tests.Checks:
just lint,just lint-docs,just hack,just bench-check, the fuzz crate, the tests on macOS (139) and Linux,just loom, and the new and neighbouring heap tests under Miri.Stacked on #8.