Skip to content

perf(cache): protect a partial once per fill, not per range admit (#1790 item 5) - #1882

Merged
alpergundogdu merged 2 commits into
mainfrom
claude/1790-item5-protect-partial-once
Aug 31, 2026
Merged

alpergundogdu merged 2 commits into
mainfrom
claude/1790-item5-protect-partial-once

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

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_partial writes the decdn-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 DashMap on the engine (partial_protected, same pattern as access_times — no mutex on the admit hot path):

  • protect_partial short-circuits when the hash is already memoized; otherwise it writes the tag and records the hash.
  • All decdn-partial-* tag removal routes through drop_named_tags_for (operator evict + 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 memoized protect_partial does not re-create it (proves the store write is skipped).
  • dropping_tags_reinvalidates_protect_memo — drop_named_tags_for clears the memo, and a subsequent protect_partial re-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

 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]>
Copilot AI lite review requested due to automatic review settings August 31, 2026 14:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 DashMap memo (partial_protected) to short-circuit repeated protect_partial tag 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_for clears partial_protected up front, but a concurrent protect_partial can still re-insert the memo while this function is deleting tags. If the tag delete then wins, the process can end up with partial_protected present while the decdn-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.

Comment thread crates/cache/src/engine.rs Resolved
…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]>
@github-actions

Copy link
Copy Markdown

Coverage report

Total: 82.32% (was 82.30%, ▲ +0.02%) — 69459/84376 lines

File Coverage Lines Δ
crates/cache/src/engine.rs 92.38% 4570/4947 ▲ +0.07%
History
Commit Total Δ vs base
45da7dc (HEAD) 82.32% ▲ +0.02%

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