Repository navigation
Optimization round 2: spilled batches, remote runs, C++ operators, precise mimalloc-bench runs - #2
Merged
Merged
Conversation
…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.
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.
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
C library
panic=abort, andmallocsets errno in its slow path. On x86-64,mallocis 19 instructions andfreeis 30, with no stack frame.freeand Rustdeallocfind the chunk map through theBcmallocstatic. Each thread publishes it when it binds a heap.free_sized,free_aligned_sized,reallocarrayandcfree, andcallochas a fast path.newanddelete. All forms are now defined. A throwingnewthat cannot allocate must run the new-handler and throwstd::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 withdlsym(RTLD_NEXT)). That operator runs the handler and throws, with no frame of ours on the stack. A failingnothrownewhands over the same way.just cabi-cxxchecks this from a preloaded C++ program, and CI runs it on Linux.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 inperf stat. A stockbench.shrun of the final build gives the same geomeans.http manynow retries connects and stops a pass after 300 s, because it hung twice on macOS.Results against mimalloc 3.6.1 (main → this branch)
Where bcmalloc still loses on mimalloc-bench
Development-suite regressions against main (Linux)
Checks
macOS: fmt, clippy (including the loom cfg), rustdoc,
cargo hackfeature 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_runand 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 fuzznow passes the toolchain's host triple.Nothing here changes a stored format.