Skip to content

Optimization round: huge-page zones, cache-aware reuse, faster C paths, benchmarks.json - #1

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

clabby merged 16 commits into
mainfrom
perf-round

Conversation

@clabby

@clabby clabby commented Nov 14, 2026

Copy link
Copy Markdown
Owner

An optimization round with the report and testing work that goes with it. Every figure is in BENCHMARKS.md and comes from benchmarks.json.

Allocator

  • Huge pages go to per-NUMA-part zones that big heaps and big blocks use, instead of THP over the whole region. Sparse slabs stay out of those zones.
  • Runs serve requests above 64 KiB. The heap's "big" flag counts the runs it holds and has hysteresis.
  • Fresh memory is no longer zeroed twice. The huge-page zone waits longer before purging, and huge frees are no longer counted twice.
  • A heap reuses another heap's blocks only for small, cache-line-sized classes. That keeps written lines on the core that wrote them, and roughly doubles rptest.
  • Size classes are 8 bytes apart up to 128 bytes, and bins of tiny classes can grow to 2048 blocks.
  • Fast paths are shorter. C malloc is 20 instructions and free is 27, and C realloc and malloc_usable_size now have fast paths.
  • Fix: a bin overran when it was shrunk while reading its inbox. This crashed or hung mstress.
  • An optional metrics feature that is cheap enough to leave on.

Benchmarks, tests and CI

  • crates/bench-data renders the tables and the overview figure from benchmarks.json. just bench-check runs in CI, and benchmark runs record into the file.
  • A coverage job uses cargo-llvm-cov and uploads to Codecov. It needs a CODECOV_TOKEN repository secret.
  • Unit tests for the C contracts, and zeroed allocation and unsized frees in the fuzz harness.
  • Import groups are merged into one block, and the docs and figures are rewritten.

Results against mimalloc 3.6.1

  • mimalloc-bench: time is a tie (0.999×) at 0.66× the peak memory. It is faster than snmalloc and jemalloc on the geometric mean.
  • Held-out Rust programs: 1.02× faster on macOS and 1.09× faster on Linux.
  • Development suite: 2.09× faster on macOS and 1.73× faster on Linux.

Where bcmalloc still loses on mimalloc-bench

  • Against mimalloc:
    • sh6bench and sh8bench, by about 1.2×. They free bursts of blocks larger than a bin holds.
    • alloc-test, glibc-simple, mstress and rocksdb, by 3 to 7%.
  • Against snmalloc: redis, glibc-thread, cfrac and espresso, by 1 to 6%.
  • xmalloc-test: jemalloc and snmalloc are both faster.

Checks

  • macOS: fmt, clippy, rustdoc, tests, doc tests, Miri (101) and loom.
  • Linux: clippy, tests (127) and loom.
  • CI-only checks: zepter and bench-check.
  • Fuzzing (earlier in the round): 60 s per target with no failures.

Backing the whole region with transparent huge pages made programs with
many threads hold a mostly empty 2 MiB page for each thread, about twice
mimalloc's peak memory on mimalloc-bench. The part of each NUMA node is
now split into a small-page zone and a huge-page zone. A heap moves to
the huge-page zone once its filled slabs hold 1 MiB, blocks of 2 MiB or
more always go there, and only that zone is purged in whole huge pages.
C's free has no size, so the C library asked for the block's layout and
then freed it, reading the chunk map twice and checking the layout on
the way. Bcmalloc::free_unsized reads the entry once and frees a slab's
block or a huge block from it, and Bcmalloc::malloc classifies sizes up
to 8 KiB straight from the table. cfrac runs 4.7% fewer instructions.
Shrinking an idle bin gives its oldest blocks back first. Sending another
heap's block may read the inbox, and the blocks filed from it can land
in the bin that is shrinking, so it could hold more than the capacity set
afterwards. Pushes then ran past the start of the bin into its neighbour,
which handed blocks out twice. mimalloc-bench's mstress crashed or hung in
about one run in five. The bin now shrinks only once it fits, and setting
a capacity below what a bin holds is checked in release builds too.
With the metrics feature, every heap counts its allocations and frees
per class on the bin's own cache line, and its slabs and slow-path
events, in counters only its thread writes, as TCMalloc and jemalloc
keep per-thread statistics. Region::metrics and Bcmalloc::metrics sum
them into a snapshot with live bytes, slab bytes and utilization per
class, and the C library prints it at exit with BCMALLOC_SHOW_METRICS.

The metrics showed why lean held 2.5 times mimalloc's memory: its heaps
kept a 4 MiB slab for each class from 64 KiB to 512 KiB with a block or
two in use, and in the huge page zone the first touch of each backed a
whole 2 MiB page. Only single-chunk slabs of big heaps and blocks of
2 MiB or more now use the huge page zone. lean's peak fell from 550 MB
to 267 MB.
…le chunks

Zeroed requests no longer clear memory that already reads as zeros, as
mimalloc and jemalloc skip zeroing pages they know are fresh. The chunk
bitmap knows a run is zero when no chunk in it was used since it was
fresh or purged on Linux, a slab remembers from which block on its
memory was never handed out, and a zeroed request with an empty bin
takes its block straight from the slab. GlobalAlloc::alloc_zeroed, the
Allocator trait and C's calloc all use it.

Blocks of whole chunks now always come from the huge page zone, since
programs fill buffers and arenas: rocksdb's 1 MiB memtable arena blocks
took 15 times the TLB misses of mimalloc's on small pages. Slabs of
several chunks go there once their class has filled one, so dense
classes get huge pages and lean's sparse ones do not.

Free chunks of the huge page zone wait ten times the purge delay before
they go back, so programs that free and allocate big blocks in turn do
not clear 2 MiB pages each time. malloc-large went from 1.55 s to 1.39 s.

A huge free counted itself twice in the metrics.
A heap kept and reused blocks of other heaps' slabs for every class whose
blocks share no cache line, up to 8 KiB. The owner wrote those blocks and
likely still holds their lines, so a thread on another core that reused
one pulled each line it wrote across, where the owner would have found it
at hand. Only blocks of up to 256 bytes, and messages, are now reused
where they are freed. rptest went from 4.8 to 10.4 million operations per
CPU second (mimalloc 7.3), and larson from 1.15 s to 1.07 s.
A slab of blocks from 64 KiB to 512 KiB was 4 MiB and held at most 56 of
them, so every heap kept a mostly empty one per class. Blocks above
64 KiB now get runs of whole chunks, as jemalloc gives large requests
extents of their own, and there are 80 size classes instead of 104.
mstress peaks at 607 MB instead of 700 MB.

Runs come from the huge page zone when they are a huge page or more, or
when the heap is big, which now counts the runs it holds as well as its
filled slabs. rocksdb's 1 MiB memtable arena blocks get huge pages that
way and match mimalloc, while alloc-test's single 256 KiB array per
thread stays on small pages.
A heap whose size swung around the threshold put half of its new slabs on
small pages. It now counts as big from 1 MiB until it holds less than
256 KiB.
- A thread's heap lookup returns an Option whose key check implies the
  heap, so the null check folded into it.
- A bin pop tells the compiler that bins hold no null pointers, which
  drops C malloc's check for an error on the fast path.
- Allocation finds its bin through a table of bin offsets by size, and a
  free through the bin offset now kept in the chunk map entry, instead of
  computing them from the class.

C malloc is 20 instructions and free 27. sh6bench runs 19% fewer
instructions and matches mimalloc's cycles, and cfrac is within 1% of
mimalloc's instruction count.
…s heap

Both asked the region for the block's layout, an atomic load and a range
check before the chunk map. Bcmalloc::usable_size and realloc_unsized read
the entry through the thread's heap, as free does. redis, which asks for
usable sizes on every allocation and free, gained 3%.
Dropping or building a subtree frees or allocates a few thousand small
blocks in a row, more than a bin of 512 held, so every one of them went
back to its slab and came out again. Classes of up to 64 bytes now grow
to 2048 blocks, within the same growth budget, and an overflowing bin
gives its blocks back a buffer at a time. The tree rewrite diagnostic went
from 92 to 103 million operations per second (mimalloc 101), and
sh8bench from 0.17 s to 0.15 s.
Classes were 16 bytes apart up to 128, so a value of three, five or seven
words took a block a word larger, a third more memory for a 24-byte node.
Classes are now 8 bytes apart from 16 to 128, as in TCMalloc and mimalloc,
with 87 classes in all. A block is aligned to the largest power of two
that divides its size, so a layout that asks for 16 bytes of alignment,
as C's malloc always does, is rounded up to a multiple of 16 first.
The binary trees with work diagnostic, whose nodes are 24 bytes, went
from 42.5 to 50.8 million operations per second (mimalloc 48.1).
…nal move chain

A layout that asks for 16 bytes of alignment indexes the table of bin
offsets with its size rounded up to 16, one that asks for 8 or less with
its size, on a branch that the alignment of each call site predicts. The
allocation fast path is 19 instructions.
…e block

The C library's exports are tested as functions in its unit tests. The fuzz
harness also drives zeroed allocation and unsized frees. Rustdoc corrections
from review, and rustfmt now merges import groups.
The bench-data crate renders the tables in BENCHMARKS.md and README.md and the
overview figure from benchmarks.json, and CI checks that they are current.
Complete benchmark runs and mimalloc-bench imports record into the file. CI
also gains a coverage job that uploads to Codecov.
BENCHMARKS.md holds the method and every table. DESIGN.md covers the huge page
zones, zeroed memory and metrics, with a new figure for the zones and
redrawn figures. The README carries the overview figure.
@clabby
clabby merged commit f9bff0d into main Nov 14, 2026
11 of 12 checks passed
@clabby
clabby deleted the perf-round 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