From f3ab9a75cbc9b2616cd3c910e5c16fd3ca75b88c Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 19:38:02 -0800 Subject: [PATCH] An append resumes the hash of the partial page instead of rehashing it When an append carries the page's validation memo forward, the page that held the old end was partial, and its hash was recomputed from the start of that page. A hash of a page is a left fold over its bytes, so the hash of the page as it is now is the hash it had carried on over the bytes appended to it; the page checksum already relies on that identity in `write_chunk`. The memo's partial-page hash is now resumed over the appended bytes, and only pages past it are hashed whole. Rehashing the partial page cost a page of hashing, per replica, to add a few bytes. No new test: the existing comparison of the carried memo against a cold build, over appends whose widths straddle the page boundary, is the oracle for exactly this, and each planted defect below is caught by it. Co-Authored-By: Claude Sonnet 5.5 --- .../cache_reclaim_snapshot/read_cache.inc.rs | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/services/chunk/cache_reclaim_snapshot/read_cache.inc.rs b/src/services/chunk/cache_reclaim_snapshot/read_cache.inc.rs index 990fbd2b..78a85ba0 100644 --- a/src/services/chunk/cache_reclaim_snapshot/read_cache.inc.rs +++ b/src/services/chunk/cache_reclaim_snapshot/read_cache.inc.rs @@ -215,10 +215,11 @@ impl MatrixReplicaService { /// /// An append leaves every byte before the old end untouched, so the hash of each page that was /// FULL at the old end is still the hash of that page. Those are kept. The page that held the - /// old end was partial and its hash is stale, and anything past it is new, so those are hashed - /// from the current bytes. The memo comes out describing the page exactly as a cold build of the - /// new payload would -- a test builds one and compares them -- so the integrity a read checks - /// is the same: a damaged byte fails its page. + /// old end was partial: its hash is the hash of its bytes so far, so it is carried on over the + /// bytes appended to it rather than recomputed from the page's start. Anything past that page + /// is new and is hashed from the current bytes. The memo comes out describing the page exactly + /// as a cold build of the new payload would -- a test builds one and compares them -- so the + /// integrity a read checks is the same: a damaged byte fails its page. /// /// It carries only a memo that described the page as it was just before this write: /// `prior_sequence` and `prior_checksum` are that page's, captured before the write changed @@ -241,9 +242,22 @@ impl MatrixReplicaService { if hashes.len() != bytes_before_write.div_ceil(page) || chunk.data.len() < bytes_before_write { return; } - let kept_pages = bytes_before_write / page; - hashes.truncate(kept_pages); - Self::extend_page_hashes(&chunk.data, kept_pages * page, &mut hashes); + let full_pages = bytes_before_write / page; + let mut next_page_start = full_pages * page; + if bytes_before_write > next_page_start { + // THE PAGE THAT HELD THE OLD END WAS PARTIAL, AND ITS HASH IS A LEFT FOLD OVER ITS + // BYTES, so the hash of the page as it is now is that hash carried on over the bytes + // appended to it -- the identity the page checksum already relies on in `write_chunk`. + // Rehashing the page from its start made every append cost a page of hashing to add a + // few bytes. The bytes past this page's end are new and are hashed whole below. + let partial_end = (next_page_start + page).min(chunk.data.len()); + hashes[full_pages] = fnv1a64_resumed( + hashes[full_pages], + &chunk.data[bytes_before_write..partial_end], + ); + next_page_start += page; + } + Self::extend_page_hashes(&chunk.data, next_page_start, &mut hashes); self.page_validation .borrow_mut() .insert(page_id.clone(), (chunk.write_sequence, chunk.checksum(), hashes));