Skip to content

A write finds the cached read ranges it can touch from an index ordered by end - #634

Merged
matrixarkai merged 1 commit into
mainfrom
perf/a-write-skips-the-read-cache-ranges-it-cannot-touch
Nov 14, 2026
Merged

matrixarkai merged 1 commit into
mainfrom
perf/a-write-skips-the-read-cache-ranges-it-cannot-touch

Conversation

@matrixarkai

Copy link
Copy Markdown
Owner

What

Every write to a page ran invalidate_chunk_cache, which walked every read range cached for that page to find the ones the write intersects. The ranges a page has cached grow with the reads of it: an entry stays in the index until it is evicted, and after an append it is stale (a hit also needs the page's sequence and checksum to be the ones it was filled under) but still there. So the walk grew with the page's read history, on every write.

The per-page index is now ordered by the end of each cached range. The ranges a write can touch are the ones that end past its start, so the walk begins there instead of at the first range there is. An append starts at the end of the page, every cached range ends at or before it, and the walk finds nothing without visiting anything. The ranges returned are exactly those the intersection test accepts.

Measured

Door under callgrind (journal mode, the shared-store harness driving it), instructions per write at 300 and 1,200 writes:

door 300 writes 1,200 writes growth total at 1,200
before both changes 285.4K 435.3K +52.5% 522.3M
#633 (validation memo) alone 272.6K 302.4K +10.9% 362.9M
this change alone 280.7K 403.2K +43.6% 483.8M
both 264.9K 270.4K +2.1% 324.5M

Alone this change is small: about 32K instructions per write at 1,200 writes, which is what the profile attributed to the walk. It is the other half of the growth #633 left. With both, the cost per write is nearly flat across a four times larger run, and the run executes 37.9% fewer instructions.

Tests

  • A randomized comparison, over 200 pages of random cached ranges, of what a write leaves cached against the intersection written out separately in the test. Writes are aimed at the boundaries (starting exactly where a cached range ends, ending exactly where one starts, past the page, empty, one byte inside), and the test asserts every shape was exercised.
  • 40 appends leave every cached range of the page in the index.

Planted defects, each caught by the comparison on the boundary it names: widening the lower bound by one (a range that only touches the write is invalidated), and making the upper bound inclusive.

Checks run locally

The four named guards pass, cargo build --all-targets --locked passes with -D warnings (default and --all-features), and the six tests above pass on the tree rebased onto #633. The library suite is 691 passed, 1 failed: the_published_mutation_copy_figures_are_the_ones_this_tree_produces, which fails with identical numbers on unmodified main and which CI does not run. The network consensus jobs are left to CI.

🤖 Generated with Claude Code

…ed by end

Every write to a page ran `invalidate_chunk_cache`, which walked every read range cached for
that page to find the ones the write intersects. The ranges a page has cached grow with the
reads of it -- an entry stays in the index until it is evicted, and after an append it is
stale but still there -- so the walk grew with the page's read history, on every write.

The per-page index is now ordered by the END of each cached range. The ranges a write can
touch are the ones that end past its start, so the walk begins there instead of at the first
range there is. An append starts at the end of the page, every cached range ends at or before
it, and the walk finds nothing without visiting anything. The ranges returned are exactly
those the intersection test accepts; a randomized comparison against that definition,
written out separately, aims its writes at the places the boundary is.

Tests: a write against random sets of cached ranges leaves cached exactly the ranges it does
not intersect (starting where one ends, ending where one starts, past the page, empty, one
byte inside); and 40 appends leave every cached range of the page in the index.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@matrixarkai
matrixarkai merged commit ca5469e into main Nov 14, 2026
5 checks passed
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