Skip to content

Let threads past max_heaps borrow spare heaps instead of failing - #9

Merged
clabby merged 1 commit into
aligned-runsfrom
spare-heaps
Nov 14, 2026
Merged

clabby merged 1 commit into
aligned-runsfrom
spare-heaps

Conversation

@clabby

@clabby clabby commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

The limit. A thread that found all max_heaps heaps taken got null from every allocation. A program therefore aborted when it started thread max_heaps + 1, which is 4097 by default.

The fallback:

  • Spare heaps. The global allocator's region has heap slots past max_heaps that Region::heap never 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.
  • A spare keeps its bins and slabs from one borrower to the next, so a borrow costs only the claim. Only the first borrower sets the slot up.
  • Spreading out. A thread asks first for the spare it had last. Before its first borrow, it asks for one hashed from its thread-local address. If every spare is held, a borrower spins for 16 passes and then yields.
  • Frees from such a thread already went home through Region::dealloc without a heap, and still do.
  • Rebinding. Region::heap keeps a count of free slots and takes one before it looks for a slot.
    • A thread past the cap learns in one load that none is free. Without the count it would pass over every slot (4096 cache misses) on every call.
    • The thread binds a heap of its own on its first call after one is given back.
  • Exiting threads. Two cases used to claim a whole heap for one call and empty it afterwards: destructors that run after a thread's heap went back at exit, and allocations made while a thread binds its heap. Both borrow a spare now.
  • trim and metrics include the spares. trim takes each slot without resetting it, so a spare's bins survive. Stats::heaps_in_use still counts only heaps held by threads.
  • Regions made with Mapping::new have no spares, so a Region used 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):

workers own heap (M pairs/s) spare heaps (M pairs/s) ratio
1 147 39 0.27
4 551 157 0.29
16 1760 453 0.26
64 3252 953 0.29
256 3965 1088 0.27
  • On main, the same program aborts with SIGABRT.
  • One spare per CPU was too few. 64 workers got 140 M/s and 256 workers 31 M/s, because the threads that ran at once often found every spare held and yielded. Four per CPU fixed that.

Cost to everyone else: none measurable.

  • Code: the stack changes no fast path. The functions that compile differently because of this PR are the binding, unbound and thread-exit paths (create, bind, alloc_unbound, realloc_unbound, claim/adopt, Region::heap, release_heap), checked against a symbol-size diff of the bench binary.
  • Binding publishes the directory only once it has a heap, so threads that retry on every call do not keep writing the cache line that every free reads.
  • Memory: each spare costs address space like any heap slot, about 290 KiB, and memory only once a thread uses it, unless Commit::Lock pins the region.
  • Whole stack (Serve alignments up to 4 MiB with aligned runs of whole chunks #8 and this PR) against main, full Rust dev suite on the EPYC box, three interleaved rounds:
    • Normal build: geometric mean +0.4% over 90 speed rows. Worst bulk 32 B shuffled −2.4% (cold only). Best pair 256 KiB +8%.
    • Every function and branch target aligned to 64 bytes: this takes code placement out of the comparison. Geometric mean +0.8%, worst large blocks −1.3%.
    • Noise: unaligned builds of identical hot code swing some workloads by ±6–8%. alloc_general with identical instructions at a 16-byte different address cost pair 16 KiB 6%. 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_heaps docs, and the global and tls module docs.

Tests:

  • global::threads_past_max_heaps_borrow_spares_until_a_heap_is_free holds all 4 heaps on 4 threads.
    • A fifth thread allocates, resizes, callocs, realloc_unsizeds and frees through the spares, and checks that it is unbound.
    • One holder then exits, and the fifth thread's next call binds a heap.
    • After every thread exits, trim gives back every chunk.
  • heap::spare_heaps_serve_one_borrower_at_a_time_keep_their_slabs_and_are_trimmed checks four things: heap() never hands out a spare, a held spare is passed over, a spare keeps its slab for the next borrower, and trim empties it.
  • loom:
    • a_claim_racing_a_release_finds_the_slot_or_leaves_it_free checks that the free-slot count is never lost or double-counted.
    • threads_without_a_heap_never_share_a_spare makes two threads wait on one spare.
  • Mutation checks: I broke each mechanism in turn: no count returned on release, trim skipping 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.

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.
@clabby
clabby merged commit 4d7b9c2 into main Nov 14, 2026
13 checks passed
@clabby
clabby deleted the spare-heaps branch November 14, 2026 21:03
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.

1 participant