Skip to content

turbo-persistence: pin referenced BlockCache entries to prevent eviction#92361

Merged
sokra merged 3 commits intocanaryfrom
sokra/cache-pinned
Apr 7, 2026
Merged

turbo-persistence: pin referenced BlockCache entries to prevent eviction#92361
sokra merged 3 commits intocanaryfrom
sokra/cache-pinned

Conversation

@sokra
Copy link
Copy Markdown
Member

@sokra sokra commented Apr 4, 2026

What?

Implements quick_cache::Lifecycle 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 turbo-persistence: skip BlockCache for uncompressed (mmap-backed) blocks #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.

@nextjs-bot nextjs-bot added created-by: Turbopack team PRs by the Turbopack team. Turbopack Related to Turbopack with Next.js. labels Apr 4, 2026
@sokra sokra marked this pull request as ready for review April 4, 2026 21:40
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 4, 2026

Merging this PR will degrade performance by 3.5%

❌ 1 regressed benchmark
✅ 16 untouched benchmarks
⏩ 3 skipped benchmarks1

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation packages-bundle.js[full] 988.9 ms 1,024.7 ms -3.5%

Comparing sokra/cache-pinned (6e7aa36) with canary (b9a1476)

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@nextjs-bot
Copy link
Copy Markdown
Collaborator

nextjs-bot commented Apr 4, 2026

Stats from current PR

✅ No significant changes detected

📊 All Metrics
📖 Metrics Glossary

Dev Server Metrics:

  • Listen = TCP port starts accepting connections
  • First Request = HTTP server returns successful response
  • Cold = Fresh build (no cache)
  • Warm = With cached build artifacts

Build Metrics:

  • Fresh = Clean build (no .next directory)
  • Cached = With existing .next directory

Change Thresholds:

  • Time: Changes < 50ms AND < 10%, OR < 2% are insignificant
  • Size: Changes < 1KB AND < 1% are insignificant
  • All other changes are flagged to catch regressions

⚡ Dev Server

Metric Canary PR Change Trend
Cold (Listen) 456ms 455ms ▁█▅▅▅
Cold (Ready in log) 444ms 444ms ▁▂▅▇▄
Cold (First Request) 1.119s 1.123s ▆▁▁▂▂
Warm (Listen) 458ms 457ms █▁██▁
Warm (Ready in log) 444ms 445ms ▂▂▇█▁
Warm (First Request) 340ms 342ms ▃▄▇█▄
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 455ms 456ms ▁▁▅▁▅
Cold (Ready in log) 434ms 434ms ▁▃▆▂▂
Cold (First Request) 1.849s 1.848s ▇▇▇▆▁
Warm (Listen) 456ms 455ms ▁▁▁▁▁
Warm (Ready in log) 434ms 434ms ▁▂▅▁▂
Warm (First Request) 1.883s 1.861s ▆▇█▆▁

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 3.853s 3.921s ▇▆██▁
Cached Build 3.823s 3.836s ▄███▄
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 14.296s 14.310s ▁▂▅▂▂
Cached Build 14.460s 14.465s ▁▂▇▃▄
node_modules Size 488 MB 488 MB █████
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
02fkg8wfh0iju.js gzip 9.19 kB N/A -
050zwt5xh_0tx.js gzip 10.4 kB N/A -
0803-3r6mifdx.js gzip 157 B N/A -
087fzjd-gvlzv.js gzip 450 B N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB
0d0pcwea0l749.js gzip 151 B N/A -
0d34flh8j9r6u.js gzip 156 B N/A -
0p0khj57rq_v_.js gzip 162 B N/A -
0ppxcl_z43mad.js gzip 8.52 kB N/A -
13mnpc17btogu.js gzip 157 B N/A -
19oha6-znmkcv.js gzip 8.55 kB N/A -
1elt1qium-r2m.css gzip 115 B 115 B
1ppe_gkx_dsny.js gzip 159 B N/A -
2_5rjb7lqxntf.js gzip 221 B 221 B
219prxwxgaalc.js gzip 7.61 kB N/A -
26elcgxnn9zjd.js gzip 8.52 kB N/A -
28s7tbll9vbjr.js gzip 156 B N/A -
2900hudr6gvm0.js gzip 2.28 kB N/A -
2bbl2qamyvimj.js gzip 65.7 kB N/A -
2lv2js3kmdeho.js gzip 8.48 kB N/A -
2rehygrd36hqv.js gzip 8.58 kB N/A -
2scbv16he964r.js gzip 158 B N/A -
2srwswih0m9_h.js gzip 13.3 kB N/A -
3-jz00s4w-r6h.js gzip 13 kB N/A -
3-p9p9mheqhzx.js gzip 8.55 kB N/A -
31030bryqpolg.js gzip 8.53 kB N/A -
31dx5nmrzzuy7.js gzip 225 B N/A -
37r23u64aoktk.js gzip 155 B N/A -
3925v09gtu-5k.js gzip 49 kB N/A -
39x4zj5mjb4d_.js gzip 9.77 kB N/A -
3at2ovgizp8r6.js gzip 158 B N/A -
3bknr2e7m9s7z.js gzip 155 B N/A -
3k-48b78ys_vy.js gzip 10.1 kB N/A -
3m7-5rfj0avoz.js gzip 12.9 kB N/A -
3t39n05ky9z08.js gzip 70.8 kB N/A -
3uqce_6sa526g.js gzip 8.47 kB N/A -
3yurjqk-sjs3y.js gzip 1.46 kB N/A -
3znov3m90-kab.js gzip 168 B N/A -
40ybjx9c192n0.js gzip 13.8 kB N/A -
421vzwdt9j1b_.js gzip 5.62 kB N/A -
44jj5q-kk1jan.js gzip 157 B N/A -
turbopack-03..e7c0.js gzip 4.18 kB N/A -
turbopack-0k..9h2a.js gzip 4.18 kB N/A -
turbopack-0m..6r79.js gzip 4.18 kB N/A -
turbopack-0v..v8st.js gzip 4.18 kB N/A -
turbopack-1s..tsd2.js gzip 4.16 kB N/A -
turbopack-3-..bo-6.js gzip 4.18 kB N/A -
turbopack-31..l4gh.js gzip 4.18 kB N/A -
turbopack-36..rum2.js gzip 4.18 kB N/A -
turbopack-3i..3636.js gzip 4.18 kB N/A -
turbopack-3v..wfxq.js gzip 4.18 kB N/A -
turbopack-3v..1qwv.js gzip 4.19 kB N/A -
turbopack-3z..kcbv.js gzip 4.18 kB N/A -
turbopack-40..j2ay.js gzip 4.18 kB N/A -
turbopack-42..qz47.js gzip 4.17 kB N/A -
03dgzoo-qf3sm.js gzip N/A 9.19 kB -
03i0taczqebbx.js gzip N/A 70.8 kB -
05tx5f25dlivn.js gzip N/A 8.53 kB -
0c7ez6p2qc57f.js gzip N/A 5.62 kB -
0duvj3qk5pvgn.js gzip N/A 13.8 kB -
0ifxao1ktkgwg.js gzip N/A 156 B -
0m-34rm9w_wpm.js gzip N/A 7.6 kB -
0qnwuk92m8i7o.js gzip N/A 10.4 kB -
0r4wrn6n0ue2m.js gzip N/A 8.55 kB -
0rp0fodtbt_6m.js gzip N/A 8.52 kB -
0sfck-km4dl1k.js gzip N/A 8.47 kB -
0x0xuhmxzwkp8.js gzip N/A 8.47 kB -
1-wdvgxnzicj7.js gzip N/A 1.46 kB -
11u6nxujb2eg4.js gzip N/A 450 B -
19uunh8umr1a1.js gzip N/A 157 B -
1el9fuakpgh8m.js gzip N/A 155 B -
1jv-o1_s-zmua.js gzip N/A 49 kB -
1mifo-hcc4vf6.js gzip N/A 154 B -
1o5x2xlfw7x62.js gzip N/A 156 B -
1sk7rrnby7fjt.js gzip N/A 157 B -
2-j7jrt35v955.js gzip N/A 160 B -
27kwgyklbqvcl.js gzip N/A 152 B -
2e2z-03lx4fjc.js gzip N/A 13 kB -
2irxuxkr23i0g.js gzip N/A 160 B -
2k9ax08cjl2id.js gzip N/A 12.9 kB -
2lms6k76q5-6m.js gzip N/A 13.3 kB -
2qx4twi9i3xus.js gzip N/A 2.28 kB -
2srnqic6tvxxd.js gzip N/A 8.52 kB -
2zkc9u4375pyw.js gzip N/A 157 B -
30l7m4nayp73a.js gzip N/A 8.55 kB -
34v1uamxoz09s.js gzip N/A 170 B -
34wde90lr4zme.js gzip N/A 157 B -
3h_ecpiaatwgc.js gzip N/A 10.1 kB -
3hxw-cpxtvy_3.js gzip N/A 156 B -
3ity0aahajapd.js gzip N/A 225 B -
3wrhpuc-j1aw9.js gzip N/A 9.77 kB -
3xlti3rufjlyg.js gzip N/A 65.7 kB -
43mlw9dy_8f02.js gzip N/A 8.58 kB -
turbopack-02..6_tq.js gzip N/A 4.18 kB -
turbopack-0h..r50b.js gzip N/A 4.18 kB -
turbopack-17..z-3u.js gzip N/A 4.19 kB -
turbopack-18..evlj.js gzip N/A 4.17 kB -
turbopack-1c..a07c.js gzip N/A 4.18 kB -
turbopack-1h..a606.js gzip N/A 4.18 kB -
turbopack-1o.._bpf.js gzip N/A 4.18 kB -
turbopack-1w..e9r6.js gzip N/A 4.18 kB -
turbopack-22..wdmr.js gzip N/A 4.18 kB -
turbopack-2c..zde7.js gzip N/A 4.18 kB -
turbopack-31..4lzd.js gzip N/A 4.18 kB -
turbopack-3g..9wtz.js gzip N/A 4.18 kB -
turbopack-3l..q89n.js gzip N/A 4.18 kB -
turbopack-40..aa11.js gzip N/A 4.16 kB -
Total 464 kB 464 kB ✅ -22 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 722 B 714 B 🟢 8 B (-1%)
Total 722 B 714 B ✅ -8 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 434 B 435 B
Total 434 B 435 B ⚠️ +1 B

📦 Webpack

Client

Main Bundles
Canary PR Change
5528-HASH.js gzip 5.54 kB N/A -
6280-HASH.js gzip 60.7 kB N/A -
6335.HASH.js gzip 169 B N/A -
912-HASH.js gzip 4.59 kB N/A -
e8aec2e4-HASH.js gzip 62.8 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB
main-app-HASH.js gzip 256 B 253 B 🟢 3 B (-1%)
main-HASH.js gzip 39.4 kB 39.3 kB
webpack-HASH.js gzip 1.68 kB 1.68 kB
262-HASH.js gzip N/A 4.59 kB -
2889.HASH.js gzip N/A 169 B -
5602-HASH.js gzip N/A 5.55 kB -
6948ada0-HASH.js gzip N/A 62.8 kB -
9544-HASH.js gzip N/A 61.4 kB -
Total 235 kB 235 kB ⚠️ +578 B
Polyfills
Canary PR Change
polyfills-HASH.js gzip 39.4 kB 39.4 kB
Total 39.4 kB 39.4 kB
Pages
Canary PR Change
_app-HASH.js gzip 194 B 194 B
_error-HASH.js gzip 183 B 180 B 🟢 3 B (-2%)
css-HASH.js gzip 331 B 330 B
dynamic-HASH.js gzip 1.81 kB 1.81 kB
edge-ssr-HASH.js gzip 256 B 256 B
head-HASH.js gzip 351 B 352 B
hooks-HASH.js gzip 384 B 383 B
image-HASH.js gzip 580 B 581 B
index-HASH.js gzip 260 B 260 B
link-HASH.js gzip 2.51 kB 2.51 kB
routerDirect..HASH.js gzip 320 B 319 B
script-HASH.js gzip 386 B 386 B
withRouter-HASH.js gzip 315 B 315 B
1afbb74e6ecf..834.css gzip 106 B 106 B
Total 7.98 kB 7.98 kB ✅ -1 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 125 kB 126 kB
page.js gzip 273 kB 273 kB
Total 398 kB 398 kB ⚠️ +174 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 615 B 615 B
middleware-r..fest.js gzip 156 B 155 B
middleware.js gzip 44.3 kB 44.1 kB
edge-runtime..pack.js gzip 842 B 842 B
Total 45.9 kB 45.7 kB ✅ -244 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 715 B 718 B
Total 715 B 718 B ⚠️ +3 B
Build Cache
Canary PR Change
0.pack gzip 4.38 MB 4.37 MB 🟢 5.52 kB (0%)
index.pack gzip 113 kB 116 kB 🔴 +2.65 kB (+2%)
index.pack.old gzip 113 kB 115 kB 🔴 +1.5 kB (+1%)
Total 4.61 MB 4.61 MB ✅ -1.37 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 342 kB 342 kB
app-page-exp..prod.js gzip 189 kB 189 kB
app-page-tur...dev.js gzip 341 kB 341 kB
app-page-tur..prod.js gzip 189 kB 189 kB
app-page-tur...dev.js gzip 338 kB 338 kB
app-page-tur..prod.js gzip 187 kB 187 kB
app-page.run...dev.js gzip 338 kB 338 kB
app-page.run..prod.js gzip 187 kB 187 kB
app-route-ex...dev.js gzip 76.6 kB 76.6 kB
app-route-ex..prod.js gzip 52.2 kB 52.2 kB
app-route-tu...dev.js gzip 76.6 kB 76.6 kB
app-route-tu..prod.js gzip 52.2 kB 52.2 kB
app-route-tu...dev.js gzip 76.2 kB 76.2 kB
app-route-tu..prod.js gzip 52 kB 52 kB
app-route.ru...dev.js gzip 76.2 kB 76.2 kB
app-route.ru..prod.js gzip 52 kB 52 kB
dist_client_...dev.js gzip 324 B 324 B
dist_client_...dev.js gzip 326 B 326 B
dist_client_...dev.js gzip 318 B 318 B
dist_client_...dev.js gzip 317 B 317 B
pages-api-tu...dev.js gzip 43.8 kB 43.8 kB
pages-api-tu..prod.js gzip 33.4 kB 33.4 kB
pages-api.ru...dev.js gzip 43.8 kB 43.8 kB
pages-api.ru..prod.js gzip 33.4 kB 33.4 kB
pages-turbo....dev.js gzip 53.2 kB 53.2 kB
pages-turbo...prod.js gzip 39 kB 39 kB
pages.runtim...dev.js gzip 53.2 kB 53.2 kB
pages.runtim..prod.js gzip 39 kB 39 kB
server.runti..prod.js gzip 62.8 kB 62.8 kB
Total 3.03 MB 3.03 MB ✅ -1 B
📎 Tarball URL
https://vercel-packages.vercel.app/next/commits/6e7aa36c11a5ebcfb0f5eb7545ed7444d8a3001f/next

@sokra sokra requested a review from lukesandberg April 5, 2026 11:18
sokra and others added 2 commits April 7, 2026 05:22
…d entries

Implement `quick_cache::Lifecycle` for `BlockCacheLifecycle` with `is_pinned`
that checks whether the `ArcBytes` backing `Arc` has strong_count > 1, preventing
eviction of cache entries still referenced outside the cache.

Co-Authored-By: Claude <noreply@anthropic.com>
…lias in tests

Drop the Option-based eviction stash since we only need is_pinned. Remove the
redundant TestBlockCache type alias in favor of the existing BlockCache alias.

Co-Authored-By: Claude <noreply@anthropic.com>
@sokra sokra force-pushed the sokra/cache-pinned branch from 6ce5dc5 to c58552b Compare April 7, 2026 05:23
Co-Authored-By: Claude <noreply@anthropic.com>
@sokra sokra merged commit 287fd7e into canary Apr 7, 2026
173 of 174 checks passed
@sokra sokra deleted the sokra/cache-pinned branch April 7, 2026 12:14
eps1lon pushed a commit that referenced this pull request Apr 7, 2026
…ion (#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>
eps1lon pushed a commit that referenced this pull request Apr 7, 2026
…ion (#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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

created-by: Turbopack team PRs by the Turbopack team. Turbopack Related to Turbopack with Next.js.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants