Skip to content

Optimization round 2: spilled batches, remote runs, C++ operators, precise mimalloc-bench runs - #2

Merged
clabby merged 12 commits into
mainfrom
perf-round-2
Nov 14, 2026
Merged

clabby merged 12 commits into
mainfrom
perf-round-2

Conversation

@clabby

@clabby clabby commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

A second optimization round, aimed at mimalloc-bench and the held-out programs, with better measurement first. Every figure is in BENCHMARKS.md and comes from benchmarks.json. All runs were on the same two machines as before, with CPU boost on for the final runs.

Allocator

  • Spilled batches. A full bin at its largest capacity copies its oldest blocks (up to 510) into a 4 KiB batch, which the next refill of that class copies back. Each heap may spill up to 1 MiB. A burst bigger than that gives its batches back, so they don't pin a few blocks of every slab.
  • Remote frees in runs. Blocks a bin gives back to another heap go into its outbox as a run, with one copy per message. Messages record their class, so the owner files a one-class message into its bin with one copy. There are now 32 outboxes. A single remote free keeps its own short path.
  • Huge pages for big processes. Once 64 MiB of chunks are in use, every heap's filled classes get huge pages, not only big heaps'. Small programs keep their small footprint.
  • Purging when quiet. Allocations above 8 KiB also check the purge clock, so a program that goes quiet after a burst gives the memory back (phase churn 171.8 → 127.6 MiB on the M4). The whole-chunk path is now out of line, so its register saves don't slow that path down.

C library

  • Frameless fast paths. It builds with panic=abort, and malloc sets errno in its slow path. On x86-64, malloc is 19 instructions and free is 30, with no stack frame.
  • Map lookup without the heap. free and Rust dealloc find the chunk map through the Bcmalloc static. Each thread publishes it when it binds a heap.
  • New functions. free_sized, free_aligned_sized, reallocarray and cfree, and calloc has a fast path.
  • C++ new and delete. All forms are now defined. A throwing new that cannot allocate must run the new-handler and throw std::bad_alloc, and Rust frames can't carry C++ exceptions. So on x86-64 and AArch64 the four throwing forms are naked functions. They call the allocator, and if it fails they jump to the C++ runtime's own operator (found with dlsym(RTLD_NEXT)). That operator runs the handler and throws, with no frame of ours on the stack. A failing nothrow new hands over the same way.
    • just cabi-cxx checks this from a preloaded C++ program, and CI runs it on Linux.
    • The extra call costs alloc-test 1.2–1.6%. The earlier version aborted instead, which a review caught.
    • Known limitation: a program that replaces only some of the operators gets ours for the others, as with the mimalloc, jemalloc and tcmalloc preloads.

Measurement

  • scripts/mbrun.py (just mbrun) runs mimalloc-bench's own binaries with nanosecond timing. It rotates the allocators within every run, pads the environment randomly, and repeats sub-second programs within each sample. It reports 95% intervals and can wrap runs in perf stat. A stock bench.sh run of the final build gives the same geomeans.
  • The Linux test of mimalloc-bench works again. It had been building the suite's own tests for lack of kernel sources.
  • Held-out Rust programs. Added a fjall kv store, a hyper server with 256 clients, and a rayon pipeline. http many now retries connects and stops a pass after 300 s, because it hung twice on macOS.
  • Bench harness fix. It no longer lets LLVM remove the settle loop's allocations. "Burst retention" had been reporting 262 MiB the allocator never held.

Results against mimalloc 3.6.1 (main → this branch)

  • mimalloc-bench (Linux): speed 1.00 → 1.02 and memory 1.51 → 1.49. Both are the best of the four allocators: snmalloc is 0.94 / 1.40 and jemalloc 0.82 / 1.37. xmalloc-test went from last of three to first. mimalloc 2.5.2 also ran and came to 0.98.
  • Held-out Rust programs: Linux 1.09 → 1.13, best on 7 of 8. macOS 1.02 → 1.03, where snmalloc is 1.04 because it is 17% faster on rayon.
  • Development suite: macOS 2.09 → 2.09, Linux 1.73 → 1.82.

Where bcmalloc still loses on mimalloc-bench

  • Against mimalloc: sh6bench 12%, mstress 8%, glibc-simple 5%, sh8bench 4%, alloc-testN 2%, leanN and malloc-large 1%.
    • sh6bench catches up if every heap gets huge pages, but that costs small programs 3–10× their memory (glibc-thread 10 → 107 MB). I kept the 64 MiB rule instead. Whether to trade memory for sh6bench is a decision for you.
  • Against snmalloc: glibc-thread 6%, redis 3%, cfrac and espresso 1%.

Development-suite regressions against main (Linux)

  • channel 7–10%, bulk 32 B shuffled 3–5%, and linked list, medium blocks and churn skewed 3–6%.
  • Variants (no spill, no occupancy counter, 16 outboxes, Rust dealloc without the directory) account for at most 2–4% of the channel loss, and each would cost more elsewhere. The pair workloads gained 13–37%.

Checks

  • macOS: fmt, clippy (including the loom cfg), rustdoc, cargo hack feature powerset, tests (132), doc tests, Miri with strict provenance (104) and loom, all on the final code.

  • Linux: clippy, tests, just cabi-cxx, and fuzzing for 120 s per target with no failures.

  • Review: a second model (with sub-reviewers) checked the directory publication, spill, run sends, file_run and the region counter, and found no bugs there. It found the two C++ issues fixed above.

  • CI: every job passes. Under Miri, the spill tests now use 256-byte blocks (a 512-block bin) so they fit in CI's time limit; with 64-byte blocks each took over 20 minutes.

  • CI fuzz fix: the fuzz smoke test failed on main too. The prebuilt cargo-fuzz targets musl, where the address sanitizer cannot link, so just fuzz now passes the toolchain's host triple.

Nothing here changes a stored format.

…ap lookup

The C library now builds with panic=abort, so its extern functions need no unwind
paths, and malloc's failure sets errno inside the slow path. malloc and free then
have no stack frame and end in jumps to their slow paths.

A free finds its chunk map entry from base, span and map fields copied into the
Bcmalloc static, which every thread that binds a heap publishes, so the entry
load no longer waits for the thread-local load and the heap's fields first.

The library defines the C++ new and delete operators, as the preload libraries
of mimalloc, snmalloc and jemalloc do, plus free_sized, free_aligned_sized,
reallocarray and cfree, and calloc takes small blocks straight off a bin.

A throwing new that cannot allocate must run the new-handler and throw
std::bad_alloc, and no C++ exception may unwind through a Rust frame. On x86-64
and AArch64 the throwing forms are naked functions that call the allocator and,
when it fails, jump to the C++ runtime's operator (dlsym RTLD_NEXT), which runs
the handler and throws with no frame of the library on the stack; elsewhere the
runtime's operators stay in place. A nothrow new that fails hands over to the
runtime's nothrow form, which runs the handler. just cabi-cxx checks this from a
C++ program that preloads the library, and CI runs it on Linux. The call costs
alloc-test 1.2-1.6% against a frameless new that aborted.

alloc-test1 3.09 -> 2.93 s, alloc-testN 2.86 -> 2.70 s, larsonN 1.21 -> 1.16,
larsonN-sized 1.16 -> 1.13, cfrac 3.77 -> 3.74 (EPYC, boost off, 3-7 runs).
…kes back

A bin that has grown to its largest and overflows moves its oldest blocks into a
batch, a 4 KiB block of the heap's own that holds 510 pointers, kept per class.
The next refill of the class copies the newest batch back before it touches a
slab. A burst of frees bigger than the bin then costs a copy of pointers each
way, where giving the blocks back cost a bitmap write per block and the refill a
bitmap read per block.

Only a class that the heap also refills spills, messages never do, and the heap
holds at most 1 MiB of spilled blocks. A burst that outgrows that budget is not
one a refill takes back, and its batches would keep a few blocks of every slab it
freed in use, so the class's batches go back to their slabs and the class spills
again only after its next refill. Batches that no refill takes until the next
maintenance step go back too, and a flush or thread exit returns them all.

sh8benchN 0.199 -> 0.175 s (mi 0.183), user time 4.55 -> 3.58 s; glibc-thread
-3%; rptestN +2% and +27% peak memory (EPYC, boost off, 3 runs).
give_back finds the runs of blocks that belong to one other heap, using the slab
alignment to skip the chunk map within a slab, and send_run appends a whole run
to the outbox with one look at it per message instead of a call per block. A
single block from a free keeps its own short path, which a loop over runs made
longer (channel and ring workloads lost a quarter of their speed on the M4). The
runs are found out of line, in give_away, so that give_back's loop over the
heap's own blocks stays as short as it was: inlined, it cost bulk 32 B in order
a quarter and linked list a fifth on the M4.

A message records the class of its blocks, and the owner copies a message of one
class into the bin at once, giving what does not fit back to the slabs, where it
looked each block up and pushed it alone. A heap keeps 32 outboxes instead of
16, so more destinations fill messages side by side.

xmalloc-testN 0.203 -> 0.177 (sn 0.193), peak 101 -> 81 MiB; sh8benchN 0.172 ->
0.165; sh6benchN 0.0727 -> 0.0697; glibc-thread -3% (EPYC, boost off, 3 runs).
The memory workloads allocate and drop a few thousand blocks between their waits,
so that allocators which return memory only while they are used get to run. The
compiler removed those allocations, because nothing read the blocks, so whether
an allocator ran its purge clock depended on what else the process had done.
Each block now passes through black_box.
Every free of chunks writes the aging flag, which shared a line with the fields
that every claim of chunks reads, so each free took the line away from the cores
that claim. The clock's atomics now sit in a cache-line-aligned struct.
The purge clock ran only on refills, slab releases and huge frees. A program that
went quiet after a burst served its requests from bins, so nothing ran the clock
and the burst's memory stayed resident. Requests above 8 KiB, which take the
general path anyway, now check the clock's flag too, a load when nothing waits.
Phase churn on the M4 keeps 127.6 MiB instead of 171.8.

The allocation of whole chunks moves out of alloc_general into alloc_whole, so
the register saves it needs no longer sit on the path of blocks above 8 KiB.
A region counts the chunks in use, on a cache line of its own. Once a process
holds 64 MiB of chunks, a heap's slabs of any class it has filled go to the zone
of transparent huge pages, not only those of big heaps. Many small threads of a
big process no longer fault their slabs in page by page: sh6benchN -7%,
larsonN -3%, rptestN -4% with peak RSS unchanged or lower, while small
programs such as glibc-thread and espresso keep their footprint.

The count only steers placement. A block passed to another thread through a
relaxed store can be freed before its claim's addition lands, so the count
may wrap below zero for a moment, and is_big reads it as signed.
benchmarks.json has the final runs of both machines, with boost on: the
development suite and the Rust programs ten times each, and mimalloc-bench ten
times through scripts/mbrun.py, which BENCHMARKS.md now describes. A stock
bench.sh run of the same build is quoted for comparison.

DESIGN.md describes spilled batches, runs of remote frees and messages of one
class, the big-process rule for huge pages, the purge check of general
allocations, the static chunk-map lookup of a free, and the C++ operators.
cargo-fuzz defaults to the target it was built for. The prebuilt cargo-fuzz
that CI installs is a musl build, so the smoke test compiled for musl, where
the address sanitizer cannot link, and failed on main as well.
Their bursts are multiples of the largest bin, 2048 blocks of 64 bytes, and
under Miri each took over 20 minutes on CI, past the job's limit. Under Miri
they use 256-byte blocks, the largest class that is still reusable anywhere,
whose bin holds 512: about two and a half minutes each on an M4.
@clabby
clabby merged commit a15ba4a into main Nov 14, 2026
12 checks passed
@clabby
clabby deleted the perf-round-2 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