Skip to content

Serve alignments up to 4 MiB with aligned runs of whole chunks - #8

Merged
clabby merged 1 commit into
mainfrom
aligned-runs
Nov 14, 2026
Merged

clabby merged 1 commit into
mainfrom
aligned-runs

Conversation

@clabby

@clabby clabby commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

The limit. An alignment above one chunk, 64 KiB, was refused. A Rust program that asked for a 2 MiB-aligned buffer aborted, and C's posix_memalign and aligned_alloc returned ENOMEM.

The change:

  • MAX_ALIGN is now 4 MiB. A request aligned to more than a chunk gets a run of whole chunks that starts on a multiple of its alignment. The chunk bitmap already claims runs at alignments up to one word of 64 chunks, so the run comes from the existing claim with align passed through.
  • Every huge block holds more than MAX_SMALL. A small request aligned past a chunk takes at least two chunks, so the paths that know only a block's size (C's realloc, usable_size, block_layout) still tell a huge block from a slab block. One-chunk huge blocks broke those paths, and random_traffic caught it.
  • Freed-run cache: the heap's cache of freed runs only hands out a run whose first chunk is aligned as asked.
  • Huge blocks in the chunk map. A huge block's entry is now marked by the top bit of the owner rather than of the whole word. Read as a slab entry, it looks like a block of the smallest class owned by a heap that does not exist.
    • A Rust free still decides by size alone, so a small size aligned past a chunk takes the slab-block path. The fast free treats the block as another heap's block of a class that goes home, and calls send.
    • send recognises the block by the owner bit and frees it as a huge block.
    • So the fast free needs no check of its own. Checking the alignment there instead cost the churn workloads 2 to 3%.

Cost: none that I can measure.

  • Instructions on x86-64: __rust_alloc, __rust_dealloc and C's malloc, calloc and free compile to the same instruction sequences as on main. I diffed the disassembly of the bench binary and the C library. C's free tests %ecx where it tested %rcx, and its huge branch gains one and.
  • classify: it tests the alignment first, as it did against the old limit, and handles anything aligned past a chunk out of line.
  • Freed-run cache: it checks a run's alignment with a mask. A division there cost the 256 KiB and 1 MiB pair workloads about 5%.
  • Benchmarks: the whole stack measured against main on the EPYC box is in Let threads past max_heaps borrow spare heaps instead of failing #9.

Docs: the README limits, DESIGN.md sections 5 and 14, the MAX_ALIGN and size_class docs, and the Entry docs.

Tests:

  • heap::blocks_aligned_past_a_chunk_are_runs_that_start_on_their_alignment allocates, resizes and zeroes at 128 KiB, 2 MiB and 4 MiB alignment while a misaligned run sits in the heap's cache. It checks every block's alignment and contents, and frees small over-aligned blocks with their layout, which exercises the route through send. It reads a sample of bytes, not every byte, so Miri stays fast.
  • region::a_huge_blocks_entry_reads_as_a_block_of_no_heap_that_goes_home pins down the entry encoding.
  • size_class::alignment_beyond_a_chunk_takes_an_aligned_run_up_to_the_limit covers classification up to the limit and the refusal past it.
  • random_traffic and the fuzz interpreter now draw alignments up to 8 MiB, past the limit, and free across three heaps.
  • tests/global.rs allocates and frees 100 bytes through std::alloc at 128 KiB, 2 MiB and 4 MiB alignment.
  • cabi: posix_memalign at 2 MiB succeeds and survives a realloc and a free. At 8 MiB it returns ENOMEM.

Checks:

  • just lint, just lint-docs and just bench-check.
  • The tests on macOS (137) and Linux.
  • Under Miri, the size-class, alignment, entry, free and random_traffic tests.

An alignment above a chunk, 64 KiB, was refused, so a Rust program that asked for
a 2 MiB-aligned buffer aborted and C's posix_memalign returned ENOMEM. Such a
request is now a run of whole chunks that starts on a multiple of its alignment,
which the chunk bitmap already claims at an alignment of up to one word of 64
chunks, so MAX_ALIGN is 4 MiB. A small request aligned that far takes two
chunks, so that every huge block still holds more than MAX_SMALL bytes and the
paths that know only a block's size, C's free and realloc among them, still
tell it from a block of a slab. The heap's cache of freed runs only hands out
a run that is aligned as asked.

The fast paths are unchanged. A free with a layout still looks at the size
alone, which leaves a small size aligned past a chunk on the path of slab
blocks. The chunk map entry of a huge block now marks it with the top bit of
the owner rather than of the word, and below that bit it reads as a block of
the smallest class owned by a heap that does not exist. The fast free takes
it for another heap's block of a class that goes home and calls send, which
tells it by the owner and frees it as a huge block. Checking the alignment
in the free instead cost the churn workloads 2 to 3%. classify tests the
alignment first, as it did against the old limit, and goes out of line past
a chunk. The cache of freed runs checks a run's alignment with a mask,
because a division there costs the 256 KiB and 1 MiB pair workloads about
5%. __rust_alloc, __rust_dealloc and C's malloc, calloc and free compile to
the same instruction sequences on x86-64 as before.
@clabby
clabby merged commit 302d9c0 into main Nov 14, 2026
13 checks passed
@clabby
clabby deleted the aligned-runs 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