perf: cache WAL resume state on Pager to skip per-flush rescan (#640) - #642
Merged
Conversation
WalWriter::open_existing re-read and rescanned the entire -wal file on every commit (ADR-0026's accepted trade-off), measured at ~6-7ms per commit even against a near-empty WAL (#635's profiling). Cache a small WalResumeHint (header, offset, running checksum, expected file size) on Pager, refreshed after each flush and invalidated at the same three sites the existing wal_shm handle cache (#437) already resets at (switch_wal_to_journal, switch_journal_to_wal, recreate_wal_locked). open_existing trusts the hint only when the file's actual size and a cheap header re-read both still match, so a concurrent writer, mode switch, or torn file always falls back to the original full rescan. Supersedes ADR-0026's rejected caching alternative with ADR-0035, since 0026 is cited by this ticket. spend: matched estimate (~Medium) Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.
Summary
Follow-up from #635/#640.
WalWriter::open_existing(src/pager/wal.rs) re-read and rescanned the entire-walfile on every commit — ADR-0026's accepted trade-off, measured at ~6-7ms per commit even against a near-empty WAL file.Caches a small
WalResumeHint(header, offset, running checksum, expected file size) onPager, mirroring the existingwal_shmhandle cache (#437):WalWriter::sync.Noneat the same three siteswal_shmalready resets at:switch_wal_to_journal,switch_journal_to_wal,recreate_wal_locked.WalWriter::open_existingonly trusts the hint when the file's actual size and a cheap 32-byte header re-read both match — any mismatch (concurrent writer, mode switch, torn file from a crash) falls back to exactly the original full read + rescan, so correctness never depends on the cache being right, only commit latency does.Adds
.openspec/adr/0035-wal-resume-hint-cache-supersedes-0026.mdsuperseding ADR-0026's rejected caching alternative, since ADR-0026 predicted and is directly cited by this ticket.spend: matched estimate (~Medium, well under the 200-350k token budget)
Test plan
flush_wal_mode_second_commit_resumes_correctly_from_cached_hint— consecutive commits from the samePagerstay correct once the second resumes from the cache.flush_wal_mode_after_mode_round_trip_does_not_reuse_stale_hint— WAL→Legacy→WAL round trip invalidates the cache and still commits correctly.flush_wal_mode_falls_back_to_rescan_when_wal_grew_from_elsewhere— a secondPagerwriter's commit is not corrupted by the firstPager's stale cached hint.pager/walunit tests pass unchanged (80 tests).cargo testsuite passes.make lint(clippy + fmt) clean.Closes #640
🤖 Analysis by Claude