Repository navigation
Serve alignments up to 4 MiB with aligned runs of whole chunks - #8
Merged
Merged
Conversation
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.
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.
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_memalignandaligned_allocreturnedENOMEM.The change:
MAX_ALIGNis 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 withalignpassed through.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'srealloc,usable_size,block_layout) still tell a huge block from a slab block. One-chunk huge blocks broke those paths, andrandom_trafficcaught it.send.sendrecognises the block by the owner bit and frees it as a huge block.Cost: none that I can measure.
__rust_alloc,__rust_deallocand C'smalloc,callocandfreecompile to the same instruction sequences as on main. I diffed the disassembly of the bench binary and the C library. C'sfreetests%ecxwhere it tested%rcx, and its huge branch gains oneand.classify: it tests the alignment first, as it did against the old limit, and handles anything aligned past a chunk out of line.Docs: the README limits, DESIGN.md sections 5 and 14, the
MAX_ALIGNandsize_classdocs, and theEntrydocs.Tests:
heap::blocks_aligned_past_a_chunk_are_runs_that_start_on_their_alignmentallocates, 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 throughsend. 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_homepins down the entry encoding.size_class::alignment_beyond_a_chunk_takes_an_aligned_run_up_to_the_limitcovers classification up to the limit and the refusal past it.random_trafficand the fuzz interpreter now draw alignments up to 8 MiB, past the limit, and free across three heaps.tests/global.rsallocates and frees 100 bytes throughstd::allocat 128 KiB, 2 MiB and 4 MiB alignment.posix_memalignat 2 MiB succeeds and survives areallocand afree. At 8 MiB it returnsENOMEM.Checks:
just lint,just lint-docsandjust bench-check.random_traffictests.