Skip to content

merkle: elide the third page level and keep costly elided pages in memory - #1002

Draft
citizen-stig wants to merge 2 commits into
thrumdev:masterfrom
citizen-stig:autoresearchresult-sov-commit-1s-05-elided-depth3
Draft

citizen-stig wants to merge 2 commits into
thrumdev:masterfrom
citizen-stig:autoresearchresult-sov-commit-1s-05-elided-depth3

Conversation

@citizen-stig

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

Copy link
Copy Markdown
Contributor

What changed

  • PAGE_ELISION_THRESHOLD goes from 20 to 255, so pages three levels below the root (~114 leaves each at 30M keys) are elided instead of stored in the hash table.
  • PageCache keeps elided pages with ≥ 20 leaves in an in-memory map, reflecting the last commit.
  • The page walker reports the cacheable elided pages it changes, and removals, through conclude_with_elided_pages. The merkle Output and the overlay carry them, and Nomt applies them after the store commit, still under the access write lock.
  • Seek uses a cached page instead of fetching leaves, and caches the pages it reconstructs. It does both only when the session has no overlays below it, since the cache holds committed state only.
  • A debug_assert checks every cache hit against its parent node.

Why it is faster

Per commit at 30M keys, ~90% of the hash-table writes were these third-level pages, each with ~6 changed nodes, on a drive limited by random 4 KiB write IOPS. They are derived data (hashes over leaves that are durable in the beatree), so they need no durable copy. After a restart they are recalculated from the leaves on first use.

Measured

Stock benchtop (setup as below):

durable_commit finish time per block
master, warm (40 blocks) 152 ms 94 ms 253 ms
this PR, warm 113 ms (−26%) 97 ms 217 ms (−14%)
master, cold (3 blocks) 154 ms 89.5 ms 252.5 ms
this PR, cold 117 ms 138 ms 263 ms

Same final roots. Peak RSS +0.4 GB.

The database must be built with this branch, because it changes which pages are stored.

Limitations

  • Cold start: after a restart the cache is empty, and finish rebuilds pages from leaves for ~20 blocks. A follow-up could rebuild the cache when the database opens.
  • New databases only: stored pages carry no leaf counts and are never elided again, so only databases grown with this version benefit.
  • Scale: at 255 the third level stays elided up to ~60M keys, after which those pages are gradually stored again. 255 is the most the existing tests' as u8 cast allows.

Tests

cargo test --workspace passes. New nomt/tests/elided_cache.rs runs 40 commits over clustered keys in one process and checks the root against a fresh database. It fails if the walker's last elided pages aren't reported, a stale-cache bug found during development.

codex and others added 2 commits November 23, 2026 16:55
…mory

About 90% of the hash-table writes of a commit were third-level pages (bits
18-24), each with ~6 changed nodes: at 30M leaves each holds ~114 leaves,
above the elision threshold of 20. Raise PAGE_ELISION_THRESHOLD to 255 so
they are elided, and keep elided pages with at least 20 leaves in an
in-memory map in the page cache, as of the last commit, so finish does not
reconstruct them from leaves:

- the page walker reports every elided page it changes, and pages that
  stop being elided; the merkle output carries them, an overlay keeps them,
  and the page cache applies them after the store commit succeeds;
- seek uses a cached elided page instead of fetching leaves, and caches
  pages it reconstructs, only when there are no overlays below the session
  (the cache reflects committed state only).

Stored pages carry no leaf counts and are never elided, so this applies to
databases grown with the new threshold.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Forty commits over clustered keys in one process keep pages two levels below
the root elided with 20-255 leaves, so they are served from the page cache's
elided-page map between commits. The root is checked against a fresh
database holding the same state. Fails if the walker's last elided pages are
not reported (the cached page goes stale).

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