Skip to content

beatree: write new leaves to consecutive pages with vectored writes - #1000

Draft
citizen-stig wants to merge 4 commits into
thrumdev:masterfrom
citizen-stig:autoresearchresult-sov-commit-1s-03-consecutive-leaves
Draft

citizen-stig wants to merge 4 commits into
thrumdev:masterfrom
citizen-stig:autoresearchresult-sov-commit-1s-03-consecutive-leaves

Conversation

@citizen-stig

@citizen-stig citizen-stig commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What changed

  • SyncAllocator::allocate_many: each leaf worker takes 32 allocations at a time, and NewLeafHandler merges leaves on consecutive pages into one write of up to 128 KiB. This uses a new IoKind::WriteRawV (io_uring Writev, pwritev on other Unix platforms).
  • FreeList::rewrite rewrites 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.
  • 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.

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):

durable_commit
master 154 ms
this PR 142 ms (−7.8%)

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.

NOMT_DB_FOLDER=db benchtop init -b nomt \
    -w randw \
    -c 25 \
    --buckets 64000000 \
    --commit-concurrency 8 \
    --io-workers 3
NOMT_DB_FOLDER=db NOMT_BENCH_ROLLBACK=1 benchtop run -b nomt \
    -w randw \
    -c 25 \
    -s 39000 \
    --workload-fresh 33 \
    --distribution uniform \
    --commit-concurrency 8 \
    --io-workers 3 \
    --buckets 64000000 \
    --page-cache-size 256 \
    --leaf-cache-size 2048 \
    --page-cache-upper-levels 3 \
    --prepopulate-page-cache \
    --warm-up-ops 117000 \
    --op-limit 1170000

Trade-offs and risks

  • Disk space. The leaf file settles at ~3× the live leaf data (6.5 GB at 30M keys). The same FREE_SLACK = 2 gate applies to the branch store. There is no hysteresis: the store switches between reuse and append around the threshold sync by sync.
  • Whole-list rewrite every sync. FreeList::rewrite sorts 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.
  • Deferred leaf writes. Leaves wait in a per-worker run until it ends or reaches 32 pages. The run owns the leaves' 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 nomt passes. The old incremental FreeList::commit code and its tests are removed (discard too). 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 through allocate_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.

codex and others added 4 commits November 23, 2026 16:37
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]>
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.

2 participants