Repository navigation
perf(cache): protect a partial once per fill, not per range admit (#1790 item 5) - #1882
Merged
Merged
Conversation
item 5) `protect_partial` wrote the `decdn-partial-{hash}` GC-protection tag on every range admit. A single fill admits many ranges (streaming serve-miss, range pull-through), so the tag — which only needs to EXIST, under a deterministic per-hash name — was re-written on every admit: store-actor write amplification that steals time from concurrent serves and grows with fill size. Memoize protected hashes in a lock-free `DashMap` on the engine: the first admit writes the tag and records the hash; later admits short-circuit. Every tag removal routes through `drop_named_tags_for`, which now clears the memo before deleting, so a re-admit after an eviction re-protects. GC never removes a set tag, so a live memo entry always reflects a live tag; the memo starts empty each process, so the first admit after restart harmlessly re-sets the tag once. Co-Authored-By: Claude Opus 4.8 <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR optimizes the cache admit hot path by memoizing “partial blob GC protection” per hash, so decdn-partial-{hash} named tags are not redundantly written on every admitted range during a single fill.
Changes:
- Add an engine-level
DashMapmemo (partial_protected) to short-circuit repeatedprotect_partialtag writes. - Clear the memo when dropping named tags so a later re-admit re-protects.
- Add unit tests covering the memo skip behavior and memo invalidation on tag drop.
Suppressed comments (1)
crates/cache/src/engine.rs:2400
drop_named_tags_forclearspartial_protectedup front, but a concurrentprotect_partialcan still re-insert the memo while this function is deleting tags. If the tag delete then wins, the process can end up withpartial_protectedpresent while thedecdn-partial-*tag is gone, causing subsequent admits to skip re-protecting and allowing GC to reclaim a still-needed partial.
self.inner.partial_protected.remove(&hash);
let tags = self.inner.store.tags();
// Collect matching names before deleting so the (immutable) list
// stream is fully drained before any delete call — keeps the two
// store interactions sequential and easy to reason about.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…invariant Copilot review: the `partial_protected` memo's `contains_key`/`insert` and the `drop_named_tags_for` `remove` are not atomic against the store, so a `protect_partial` racing a concurrent evict can leave the memo set while the tag was deleted — the doc's "a live memo entry always reflects a live tag" overstated this. Reword both comments as a best-effort optimization and spell out why the race is benign: served bytes are hash-verified and operator takedowns block serving through the logical evicted set, not this tag, so the only cost is a partial that self-corrects on the next pull. No logic change. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Coverage reportTotal: 82.32% (was 82.30%, ▲ +0.02%) — 69459/84376 lines
History
|
This was referenced Aug 31, 2026
Merged
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.
Item 5 of #1790 (Group C serve-path hot-path audit), landed as its own PR — same per-item cadence as #1879 (item 7) and the #1792 siblings.
Problem
protect_partialwrites thedecdn-partial-{hash}GC-protection tag on every range admit — three call sites (range_pull_attempt,admit_bao,admit_bao_stream). A single fill admits many ranges (streaming serve-miss and range pull-through both admit chunk-group by chunk-group), so the tag is re-written on every admit. The tag only needs to exist, under a deterministic per-hash name, so every write after the first is redundant — store-actor write amplification that grows with fill size and steals actor time from concurrent serves (compounds #1789 item 5's contention).Fix
Memoize protected hashes in a lock-free
DashMapon the engine (partial_protected, same pattern asaccess_times— no mutex on the admit hot path):protect_partialshort-circuits when the hash is already memoized; otherwise it writes the tag and records the hash.decdn-partial-*tag removal routes throughdrop_named_tags_for(operatorevict+ the LRU eviction driver). It now clears the memo before deleting, so a re-admit after an eviction re-protects rather than trusting a stale entry.Correctness: GC never removes a set tag (it protects blobs reachable from a tag), so a live memo entry always reflects a live tag — the only removal is explicit eviction, which invalidates the memo. Concurrent first-admits at worst issue one redundant idempotent
set(same name/value). The memo starts empty each process, so the first admit after restart harmlessly re-sets the tag once.Tests
protect_partial_skips_store_write_when_memoized— deletes the tag directly at the store leaving the memo intact, then shows a memoizedprotect_partialdoes not re-create it (proves the store write is skipped).dropping_tags_reinvalidates_protect_memo—drop_named_tags_forclears the memo, and a subsequentprotect_partialre-creates the tag.Full cache suite (401) green;
clippy,fmt --check, and all pre-commit hooks clean.Remaining #1790 items
Items 1, 2, 3, 4, 6, 8, 9 remain open as separate follow-ups (item 7 shipped in #1879). Next natural candidate is item 3 (serve-side per-leaf presence from the held
PresentRangeWatch).🤖 Generated with Claude Code