Repository navigation
beatree: write new leaves to consecutive pages with vectored writes - #1000
Draft
citizen-stig wants to merge 4 commits into
Draft
citizen-stig wants to merge 4 commits into
citizen-stig wants to merge 4 commits into
Conversation
Each leaf worker takes 32 allocations at a time and merges leaves on consecutive pages into one vectored write (new IoKind::WriteRawV, up to 128 KiB). The free-list is rewritten each sync in address order, rotated to start after the last page used, so allocations sweep through the store. Free pages are reused only once they number twice the live pages; until then new pages are appended. The store grows with fallocate instead of set_len so direct writes don't allocate blocks one at a time. Writes no longer map one-to-one to leaves, so each leaf worker counts the writes it sends and the update waits for that many completions. Co-Authored-By: Claude Opus 5.5 <[email protected]>
fallocate is Linux-only; fall back to set_len elsewhere. Co-Authored-By: Claude Opus 5.5 <[email protected]>
FreeList::commit and its helpers were only kept alive for their own tests. Remove them with the pop/released_portions state, fold CleanFreeList into FreeList, and add tests for rewrite (layout, page conservation, crash safety, sweep order, fragmented input) and for the allocator's allocate_many/reuse path including a reload from disk. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The run now holds the leaves' Arcs rather than raw pages, so a pending page cannot be freed before its write is sent, and the last run is sent as soon as the worker stops producing leaves instead of after it waits on its left neighbor. Drop IoVecs::as_slice. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
What changed
SyncAllocator::allocate_many: each leaf worker takes 32 allocations at a time, andNewLeafHandlermerges leaves on consecutive pages into one write of up to 128 KiB. This uses a newIoKind::WriteRawV(io_uringWritev,pwritevon other Unix platforms).FreeList::rewriterewrites the free list each sync in address order, rotated to start after the last page used, so allocations sweep through the file. The on-disk free-list page format is unchanged.fallocateinstead ofset_len, so direct writes don't allocate blocks one at a time.Why it is faster
Leaves are copy-on-write anyway. Before, each commit reused the previous commit's freed pages, which are scattered at random, so every leaf was its own random 4 KiB write. On this NVMe, random 4 KiB writes cap at ~600k IOPS, while 128 KiB writes reach ~4 GB/s. The block layer doesn't merge adjacent direct writes, so the merging has to happen in NOMT. At steady state the cursor finds long runs of free pages, because most pages it passes have been freed since its last visit.
Measured
Stock benchtop (setup as below):
On a node-profile benchmark, disk writes per commit fell from 78.7k to 43.6k. On a database grown from empty with this allocator (steady-state sweeping), the gain held: 138 ms against 137 ms in append mode.
Trade-offs and risks
FREE_SLACK = 2gate applies to the branch store. There is no hysteresis: the store switches between reuse and append around the threshold sync by sync.FreeList::rewritesorts and re-encodes the entire free list on nearly every sync, and the gate lets the list grow to ~2× the live page count. Cost scales with database size, not with the commit: at 100M live pages that is ~200M entries, i.e. ~800 MB of free-list pages written and a 200M-element sort per sync. Not measured beyond the benchmark above.Arcs, so pending pages cannot be freed before their write is sent, and the last run is flushed as soon as the worker stops producing leaves rather than after it waits on its left neighbor. Once sent, writes rely on the same tracker/post-I/O lifetime as before.Tests
cargo test -p nomtpasses. The old incrementalFreeList::commitcode and its tests are removed (discardtoo). New tests:free_list::tests::rewrite_*: layout and encoding, no page lost or duplicated, new list pages never overwrite anything the committed state references, pops sweep upward with at most one wrap, and fragmented lists from older databases.allocator::tests::allocate_many_and_reuse: 40 syncs of churn throughallocate_many/allocate/finish, checking that no referenced page is handed out or overwritten, that reuse kicks in, and that the final free list reloads from disk identically.