Repository navigation
Optimization round: huge-page zones, cache-aware reuse, faster C paths, benchmarks.json - #1
Merged
Merged
Conversation
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.
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.
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
malloc_usable_sizenow have fast paths.metricsfeature that is cheap enough to leave on.Benchmarks, tests and CI
crates/bench-datarenders the tables and the overview figure frombenchmarks.json.just bench-checkruns in CI, and benchmark runs record into the file.CODECOV_TOKENrepository secret.Results against mimalloc 3.6.1
Where bcmalloc still loses on mimalloc-bench
Checks
bench-check.