turbo-persistence: pin referenced BlockCache entries to prevent eviction (#92361)
### What? Implements [`quick_cache::Lifecycle`](https://docs.rs/quick_cache/latest/quick_cache/#lifecycle-hooks) hooks for the `BlockCache` in turbo-persistence. A new `BlockCacheLifecycle` struct overrides `is_pinned` to check whether the `ArcBytes` value's backing `Arc` has a strong count > 1, indicating it is still referenced outside the cache. When `is_pinned` returns `true`, the cache's eviction algorithm (Clock-PRO / S3-FIFO) skips the entry instead of evicting it. ### Why? Without pinning, the cache can evict blocks that are still in use — the caller holds an `ArcBytes` clone (which shares the `Arc` backing), so the data isn't lost, but the cache slot is wasted. When the caller finishes and the entry is looked up again, it results in an unnecessary cache miss and re-read/decompress from disk. By checking `Arc::strong_count > 1` in `is_pinned`, we avoid evicting entries that are actively referenced, improving cache hit rates under concurrent access. ### How? **`ArcBytes::is_shared()`** (`arc_bytes.rs`): - Returns `true` when the backing `Arc<[u8]>` has `strong_count > 1` (i.e., someone outside the cache holds a clone). - Mmap-backed entries always return `false` — the mmap `Arc` is shared across all slices from the same file, so checking its `strong_count` would make them permanently unevictable. This is fine because #92390 already drops mmap blocks from the cache entirely. **`BlockCacheLifecycle`** (`static_sorted_file.rs`): - Implements `quick_cache::Lifecycle<(u32, u16), ArcBytes>` with `RequestState = ()`. - `is_pinned` delegates to `val.is_shared()`. - `begin_request` and `on_evict` are no-ops since we only need the pinning behavior. **`BlockCache` type alias** updated to include `BlockCacheLifecycle` as the 5th generic parameter. All call sites use `Default::default()` for construction, so no call-site changes were needed. **Test cleanup**: Removed the redundant `TestBlockCache` type alias (and its unused imports) in the builder tests in favor of the existing `BlockCache` alias. <!-- NEXT_JS_LLM_PR --> --------- Co-authored-by:Tobias Koppers <sokra@users.noreply.github.com> Co-authored-by:
Claude <noreply@anthropic.com>
Showing
Please
register
or
sign in
to comment