Skip to content

Proof of concept: task eviction after snapshot for turbo-tasks-backend#91790

Open
lukesandberg wants to merge 15 commits intocanaryfrom
eviction_semantics_for_real_this_time
Open

Proof of concept: task eviction after snapshot for turbo-tasks-backend#91790
lukesandberg wants to merge 15 commits intocanaryfrom
eviction_semantics_for_real_this_time

Conversation

@lukesandberg
Copy link
Copy Markdown
Contributor

@lukesandberg lukesandberg commented Mar 22, 2026

Note: This is a proof of concept implementation. It is not yet ready for production use.

Summary

Implements memory eviction for the turbo-tasks engine. After a persistence snapshot completes, tasks that are safe to remove are evicted from in-memory storage and transparently restored from disk on next access.

Eviction levels

  • Full eviction: Entire task removed from the in-memory map (restored from disk on access). Only possible when the task has no meaningful transient state (and other state is already on disk)
  • DataAndMeta eviction: Both data and meta categories cleared, but the task stays in the map to preserve transient state (e.g. current_session_clean, aggregated session-clean counts).
  • DataOnly eviction: Only data-category fields cleared; meta (graph structure, output, dirty state) stays in memory.
  • MetaOnly eviction: Only meta-category fields cleared; data stays in memory.

Data and meta evictability are computed independently — if one category is modified but the other is clean, the clean category can still be dropped.

Eviction is gated behind BackendOptions::evict_after_snapshot (off by default), and can be enabled in Next.js via the TURBO_ENGINE_EVICT_AFTER_SNAPSHOT=1 env var for testing.

Key changes

  • Orthogonal eviction decision tree (storage_schema.rs): Data and meta evictability are computed independently. Full eviction additionally requires no meaningful transient state (session-clean flags, aggregated session-clean counts). Replaces the previous sequential bail-out approach which was too aggressive on full eviction (losing transient session state on leaf tasks) and not aggressive enough on partial eviction (blocking all eviction when only one category was modified).

  • drop_partial() codegen (task_storage_macro.rs): New generated methods to drop data

  • restore_from_*() codegen changes (task_storage_macro.rs): New semantics for merging persistent data from the backend with transient data stored in memory.

  • task_cache moved into Storage (storage.rs): The CachedTaskType → TaskId deduplication map was previously a separate field on TurboTasksBackendInner. It is now owned by Storage so eviction can remove entries when a task is fully evicted. Because task_cache is a pure performance cache (entries are re-populated by task_by_type() on miss once the task type is persisted to backing storage), evicting entries is safe. After bulk eviction the map is shrunk when it is less than half full.

  • Parallel shard eviction (storage.rs): Eviction iterates all storage shards in parallel after snapshot, applying the appropriate eviction level per task. Each shard is shrunk after bulk eviction to reclaim slack capacity.

    • In principle this is O(N) work to scan, but because each pass drops >98% of tasks there isn't wasted work and the logic is fast, taking <100ms for even the largest applications.

Design notes

  • SessionDependent tasks: SessionDependent tasks can still be evicted but if current_session_clean is set we prevent full eviction to avoid rechecking. Within a session the file-watchers are responsible for invalidations after setting current_session_clean.

Known limitations (proof of concept)

  • No LRU or access-frequency tracking — all eligible tasks are evicted on every snapshot cycle
  • No memory pressure feedback — eviction runs on a timer, not in response to actual memory pressure
  • Only runs after snapshotting which tends to be a high point in memory
    • Future work will explore interleaving this logic with snapshotting to trim the peak

@nextjs-bot nextjs-bot added created-by: Turbopack team PRs by the Turbopack team. Turbopack Related to Turbopack with Next.js. labels Mar 22, 2026
Copy link
Copy Markdown
Contributor Author

lukesandberg commented Mar 22, 2026

@nextjs-bot
Copy link
Copy Markdown
Contributor

nextjs-bot commented Mar 22, 2026

Tests Passed

@nextjs-bot
Copy link
Copy Markdown
Contributor

nextjs-bot commented Mar 22, 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) 455ms 455ms ▅▁▁▅▅
Cold (Ready in log) 440ms 440ms █▇▂▅▅
Cold (First Request) 817ms 822ms ▁▁█▁█
Warm (Listen) 456ms 456ms ▅▅▅▅█
Warm (Ready in log) 440ms 440ms ▃▃▃▁▄
Warm (First Request) 338ms 339ms ▅▅▇▂▇
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 455ms 455ms ▅▁▅▅▅
Cold (Ready in log) 438ms 438ms ▂▇▁▃▅
Cold (First Request) 1.857s 1.895s ▆█▆▆█
Warm (Listen) 456ms 456ms ▅█▁▅▅
Warm (Ready in log) 438ms 438ms ▄▆▂▃▅
Warm (First Request) 1.871s 1.876s ▅▇▆▆█

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 3.979s 3.951s ▄█▃▅▂
Cached Build 3.980s 4.009s ▃█▂▄▁
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 14.452s 14.439s ██▂▃█
Cached Build 14.540s 14.589s ▂█▄▁▆
node_modules Size 494 MB 494 MB █████
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
04gfaeiu1v7qm.js gzip 151 B N/A -
07rxhp_1_g4mu.js gzip 13.1 kB N/A -
07vz_1vlust95.js gzip 65.5 kB N/A -
08avva-dy02e7.js gzip 10.4 kB N/A -
09x_ycos2214h.js gzip 151 B N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB
0fli3_wppnim5.js gzip 12.9 kB N/A -
0k09jwjeb-tki.js gzip 13.8 kB N/A -
0kb7_ep3r1z0_.js gzip 10.1 kB N/A -
0kw8xgqdrilf6.js gzip 8.56 kB N/A -
0lgndkr1hands.js gzip 49.6 kB N/A -
0ojkk2e654xsc.js gzip 8.59 kB N/A -
0wfoal20dkqj1.js gzip 155 B N/A -
0wxpyd8r-vipl.js gzip 1.47 kB N/A -
0xy2fhla48_rd.js gzip 9.24 kB N/A -
0zx3fp91iid52.js gzip 159 B N/A -
10wqsvi2mgfmi.js gzip 9.82 kB N/A -
16lhqjoqbznyg.js gzip 220 B 220 B
16vepdkipri3r.js gzip 8.51 kB N/A -
17n96uu6y1pxq.js gzip 8.6 kB N/A -
18y4_8-9or0mn.js gzip 8.51 kB N/A -
19410kg_2bl_2.js gzip 156 B N/A -
1elt1qium-r2m.css gzip 115 B 115 B
1gq145j3kps-h.js gzip 8.62 kB N/A -
1li2wvnrlll1x.js gzip 155 B N/A -
1nsh-mbn0e-se.js gzip 8.56 kB N/A -
1tsrrp1tdngti.js gzip 13.3 kB N/A -
2__-e_ym8n788.js gzip 450 B N/A -
22o6xd9_ywdu6.js gzip 233 B N/A -
2587zu9zz3ch0.js gzip 154 B N/A -
25ykkt57gkfew.js gzip 154 B N/A -
2kb7581t3yg4r.js gzip 153 B N/A -
2kvj8yrfznmwx.js gzip 5.69 kB N/A -
2qv7m7xjnokgr.js gzip 8.58 kB N/A -
342ijzvrpe53h.js gzip 2.29 kB N/A -
37pyv-119nldk.js gzip 152 B N/A -
3geqw_3h7k33y.js gzip 156 B N/A -
3ok6wj7wwsorf.js gzip 70.8 kB N/A -
3sfdvnlx2-d2i.js gzip 152 B N/A -
3u-chifslwdvs.js gzip 166 B N/A -
44un3--wmqiyh.js gzip 7.61 kB N/A -
turbopack-09..02ja.js gzip 4.2 kB N/A -
turbopack-0c.._q68.js gzip 4.18 kB N/A -
turbopack-0s..yucr.js gzip 4.19 kB N/A -
turbopack-0x..vpta.js gzip 4.19 kB N/A -
turbopack-10..-d_w.js gzip 4.19 kB N/A -
turbopack-11..dlm2.js gzip 4.18 kB N/A -
turbopack-1k..j62f.js gzip 4.19 kB N/A -
turbopack-27..b9wz.js gzip 4.17 kB N/A -
turbopack-2u..oxs7.js gzip 4.18 kB N/A -
turbopack-37..th14.js gzip 4.18 kB N/A -
turbopack-39..mfcn.js gzip 4.18 kB N/A -
turbopack-3h..4bnr.js gzip 4.18 kB N/A -
turbopack-3i..gib8.js gzip 4.19 kB N/A -
turbopack-3m..bo_z.js gzip 4.19 kB N/A -
07ao55cvtbxdj.js gzip N/A 153 B -
09t9i6o8nvir7.js gzip N/A 49.6 kB -
0arkbdqpxc37i.js gzip N/A 8.6 kB -
0bz-xifewa17d.js gzip N/A 8.63 kB -
0tp80kb9upsmd.js gzip N/A 153 B -
0tsy2vtfmen7f.js gzip N/A 155 B -
0tv67up09u6yz.js gzip N/A 168 B -
0tvekitj587fh.js gzip N/A 8.51 kB -
0y-5s9e_qen41.js gzip N/A 160 B -
0yvk6-wi8e9wh.js gzip N/A 13.3 kB -
0z83a1om5rvtt.js gzip N/A 7.61 kB -
1-jqyfc89tixo.js gzip N/A 1.46 kB -
14t1kneseb8th.js gzip N/A 2.3 kB -
15sb1-dsqfk_j.js gzip N/A 8.59 kB -
1ab2xruymo-oj.js gzip N/A 449 B -
1gt0tm9ba-fkj.js gzip N/A 70.8 kB -
1p-256e9l_n93.js gzip N/A 156 B -
1rexplishteym.js gzip N/A 156 B -
1tu25qtsmfhar.js gzip N/A 9.82 kB -
1vein_gnv3mwr.js gzip N/A 8.56 kB -
1wtjzfc14dkv9.js gzip N/A 154 B -
1wzrm0xjjbzn5.js gzip N/A 10.1 kB -
1xvan__507d2k.js gzip N/A 155 B -
1z3g0uaqtv9_3.js gzip N/A 8.56 kB -
25a1yz7zua29z.js gzip N/A 13.8 kB -
2bi5hx402juv-.js gzip N/A 8.58 kB -
2hy56297fog9u.js gzip N/A 8.52 kB -
2u_rpxq3tzytl.js gzip N/A 233 B -
31pzx_2wlpu4e.js gzip N/A 156 B -
368lim5wq0o0r.js gzip N/A 12.9 kB -
3drqjohogojbw.js gzip N/A 5.69 kB -
3fs484yi1tj7v.js gzip N/A 156 B -
3g8l1m2-o-ewi.js gzip N/A 13.1 kB -
3jmkxsnxg0nrh.js gzip N/A 10.4 kB -
3qacr5lkwv7qz.js gzip N/A 65.5 kB -
3qi06md02f2cn.js gzip N/A 159 B -
3socpyg9nr02a.js gzip N/A 157 B -
3wpp8nvyoj121.js gzip N/A 9.24 kB -
turbopack-0n..mrvy.js gzip N/A 4.17 kB -
turbopack-0p..kynv.js gzip N/A 4.19 kB -
turbopack-0s..7ud3.js gzip N/A 4.18 kB -
turbopack-17..4gpi.js gzip N/A 4.19 kB -
turbopack-1c..9gyb.js gzip N/A 4.19 kB -
turbopack-1e..ccp_.js gzip N/A 4.19 kB -
turbopack-1s..h27x.js gzip N/A 4.19 kB -
turbopack-1t..njk8.js gzip N/A 4.2 kB -
turbopack-29..e4gm.js gzip N/A 4.19 kB -
turbopack-2a..5zpv.js gzip N/A 4.19 kB -
turbopack-2c..tpfn.js gzip N/A 4.19 kB -
turbopack-2k..hjo6.js gzip N/A 4.19 kB -
turbopack-32..3ekq.js gzip N/A 4.19 kB -
turbopack-42..fj8z.js gzip N/A 4.19 kB -
Total 465 kB 465 kB ⚠️ +103 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 712 B 715 B
Total 712 B 715 B ⚠️ +3 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 431 B 433 B
Total 431 B 433 B ⚠️ +2 B

📦 Webpack

Client

Main Bundles
Canary PR Change
2637-HASH.js gzip 4.63 kB N/A -
7724.HASH.js gzip 169 B N/A -
8274-HASH.js gzip 61.4 kB N/A -
8817-HASH.js gzip 5.59 kB N/A -
c3500254-HASH.js gzip 62.8 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB
main-app-HASH.js gzip 254 B 255 B
main-HASH.js gzip 39.4 kB 39.3 kB
webpack-HASH.js gzip 1.68 kB 1.68 kB
5887-HASH.js gzip N/A 5.61 kB -
6522-HASH.js gzip N/A 60.8 kB -
6779-HASH.js gzip N/A 4.63 kB -
8854.HASH.js gzip N/A 169 B -
eab920f9-HASH.js gzip N/A 62.8 kB -
Total 236 kB 235 kB ✅ -640 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 193 B 193 B
_error-HASH.js gzip 182 B 182 B
css-HASH.js gzip 333 B 334 B
dynamic-HASH.js gzip 1.81 kB 1.8 kB
edge-ssr-HASH.js gzip 255 B 255 B
head-HASH.js gzip 353 B 349 B 🟢 4 B (-1%)
hooks-HASH.js gzip 384 B 382 B
image-HASH.js gzip 581 B 581 B
index-HASH.js gzip 260 B 259 B
link-HASH.js gzip 2.51 kB 2.51 kB
routerDirect..HASH.js gzip 316 B 318 B
script-HASH.js gzip 386 B 386 B
withRouter-HASH.js gzip 313 B 314 B
1afbb74e6ecf..834.css gzip 106 B 106 B
Total 7.98 kB 7.97 kB ✅ -10 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 126 kB 126 kB
page.js gzip 274 kB 273 kB
Total 399 kB 399 kB ✅ -384 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 620 B 618 B
middleware-r..fest.js gzip 156 B 156 B
middleware.js gzip 43.9 kB 44.3 kB
edge-runtime..pack.js gzip 842 B 842 B
Total 45.5 kB 45.9 kB ⚠️ +390 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 721 B 720 B
Total 721 B 720 B ✅ -1 B
Build Cache
Canary PR Change
0.pack gzip 4.39 MB 4.38 MB
index.pack gzip 115 kB 115 kB
index.pack.old gzip 114 kB 114 kB
Total 4.62 MB 4.61 MB ✅ -3.78 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 347 kB 347 kB
app-page-exp..prod.js gzip 192 kB 192 kB
app-page-tur...dev.js gzip 347 kB 347 kB
app-page-tur..prod.js gzip 192 kB 192 kB
app-page-tur...dev.js gzip 343 kB 343 kB
app-page-tur..prod.js gzip 190 kB 190 kB
app-page.run...dev.js gzip 344 kB 344 kB
app-page.run..prod.js gzip 190 kB 190 kB
app-route-ex...dev.js gzip 77 kB 77 kB
app-route-ex..prod.js gzip 52.5 kB 52.5 kB
app-route-tu...dev.js gzip 77.1 kB 77.1 kB
app-route-tu..prod.js gzip 52.6 kB 52.6 kB
app-route-tu...dev.js gzip 76.7 kB 76.7 kB
app-route-tu..prod.js gzip 52.3 kB 52.3 kB
app-route.ru...dev.js gzip 76.6 kB 76.6 kB
app-route.ru..prod.js gzip 52.3 kB 52.3 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.9 kB 43.9 kB
pages-api-tu..prod.js gzip 33.5 kB 33.5 kB
pages-api.ru...dev.js gzip 43.9 kB 43.9 kB
pages-api.ru..prod.js gzip 33.5 kB 33.5 kB
pages-turbo....dev.js gzip 53.3 kB 53.3 kB
pages-turbo...prod.js gzip 39.1 kB 39.1 kB
pages.runtim...dev.js gzip 53.3 kB 53.3 kB
pages.runtim..prod.js gzip 39.1 kB 39.1 kB
server.runti..prod.js gzip 63 kB 63 kB
Total 3.07 MB 3.07 MB ✅ -1 B
📎 Tarball URL
https://vercel-packages.vercel.app/next/commits/77631e6dc9d11936dca57fa75b1321c9d4d77db5/next

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Mar 22, 2026

Merging this PR will not alter performance

✅ 17 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing eviction_semantics_for_real_this_time (8b16ef4) with canary (0b08e2f)2

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.

  2. No successful run was found on racy_snapshot_encoding (e98a934) during the generation of this report, so canary (0b08e2f) was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@lukesandberg lukesandberg force-pushed the eviction_semantics_for_real_this_time branch from c194c61 to fa50df9 Compare March 23, 2026 02:19
@lukesandberg lukesandberg force-pushed the eviction_semantics_for_real_this_time branch from fa50df9 to 056be10 Compare March 23, 2026 22:03
@lukesandberg lukesandberg changed the title eviction Proof of concept: task eviction after snapshot for turbo-tasks-backend Mar 23, 2026
Comment thread test/e2e/filesystem-cache/evict-after-snapshot.test.ts
Comment thread turbopack/crates/turbo-tasks-backend/src/backend/mod.rs Outdated
Comment thread turbopack/crates/turbo-tasks-backend/src/backend/mod.rs Outdated
Comment thread turbopack/crates/turbo-tasks-backend/src/backend/mod.rs Outdated
Comment thread turbopack/crates/turbo-tasks-backend/src/backend/mod.rs Outdated
Comment thread turbopack/crates/turbo-tasks-backend/src/backend/mod.rs Outdated
Comment thread turbopack/crates/turbo-tasks-backend/src/backend/mod.rs
Comment thread turbopack/crates/turbo-tasks-backend/src/backend/storage.rs Outdated
Comment thread turbopack/crates/turbo-tasks-backend/src/backend/storage_schema.rs Outdated
Comment thread turbopack/crates/turbo-tasks/src/vc/traits.rs Outdated
@lukesandberg lukesandberg marked this pull request as ready for review March 24, 2026 22:30
@lukesandberg lukesandberg force-pushed the eviction_semantics_for_real_this_time branch 2 times, most recently from 116d63d to ae6878d Compare April 7, 2026 18:36
@lukesandberg lukesandberg marked this pull request as draft April 7, 2026 20:44
@lukesandberg lukesandberg changed the base branch from canary to graphite-base/91790 April 11, 2026 14:14
@lukesandberg lukesandberg force-pushed the eviction_semantics_for_real_this_time branch from 35da0fa to 290c8d7 Compare April 11, 2026 14:14
@lukesandberg lukesandberg changed the base branch from graphite-base/91790 to racy_snapshot_encoding April 11, 2026 14:14
@lukesandberg lukesandberg force-pushed the eviction_semantics_for_real_this_time branch 2 times, most recently from 890a1ba to da87628 Compare April 13, 2026 23:47
@lukesandberg lukesandberg force-pushed the eviction_semantics_for_real_this_time branch from 1e85dc1 to 7a4059f Compare April 30, 2026 22:54
@lukesandberg lukesandberg requested a review from sokra April 30, 2026 22:55
@lukesandberg lukesandberg force-pushed the eviction_semantics_for_real_this_time branch from 7a4059f to 1f9c55d Compare May 1, 2026 18:52
@lukesandberg lukesandberg changed the base branch from canary to graphite-base/91790 May 1, 2026 22:44
@lukesandberg lukesandberg force-pushed the eviction_semantics_for_real_this_time branch from 1f9c55d to 5a1ac64 Compare May 1, 2026 22:44
@lukesandberg lukesandberg changed the base branch from graphite-base/91790 to slightly_more_crash_safety May 1, 2026 22:44
@lukesandberg lukesandberg force-pushed the eviction_semantics_for_real_this_time branch 2 times, most recently from 07114f2 to c5e00ef Compare May 2, 2026 05:43
@lukesandberg lukesandberg force-pushed the slightly_more_crash_safety branch 2 times, most recently from 4378987 to d2bde2d Compare May 4, 2026 20:08
@lukesandberg lukesandberg force-pushed the eviction_semantics_for_real_this_time branch from c5e00ef to 327ffb1 Compare May 4, 2026 20:08
Base automatically changed from slightly_more_crash_safety to canary May 4, 2026 22:23
@graphite-app
Copy link
Copy Markdown
Contributor

graphite-app Bot commented May 4, 2026

Merge activity

  • May 4, 10:28 PM UTC: Graphite disabled "merge when ready" on this PR due to: unsigned commits detected.

lukesandberg and others added 12 commits May 4, 2026 15:41
The macro emitted `if !drop_partial() { reset; }` for filter_transient
inline fields, which destroyed transient residue (HasResidue branch
unconditionally cleared the field). After eviction the upper-edge to a
transient HMR root was wiped, so dirty propagation from a file change
never reached an active root and HMR didn't fire.

Replace the bool return with `DropPartialOutcome { Empty, HasResidue }`,
make `DropPartial` a pub(crate) trait that CellData also implements
(was duck-typed before), and have `TaskStorage::drop_partial` return
the outcome authoritatively — the eviction caller no longer needs a
separate `is_empty()` query. Add per-category `is_empty_data` /
`is_empty_meta` / `is_empty_transient` helpers so the final
emptiness check skips the categories the drop just cleared.
@lukesandberg lukesandberg force-pushed the eviction_semantics_for_real_this_time branch from 327ffb1 to 94bd15c Compare May 4, 2026 22:42
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. tests Turbopack Related to Turbopack with Next.js.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants