Skip to content

turbopack: cache TransformPlugin in narrow-scoped turbo-tasks functions#92842

Merged
sokra merged 3 commits into
canaryfrom
sokra/cache-transform-plugin
Apr 16, 2026
Merged

turbopack: cache TransformPlugin in narrow-scoped turbo-tasks functions#92842
sokra merged 3 commits into
canaryfrom
sokra/cache-transform-plugin

Conversation

@sokra
Copy link
Copy Markdown
Member

@sokra sokra commented Apr 15, 2026

What?

Refactor all usages of EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(...) as _)) across next-core and turbopack-tests so that TransformPlugin cells are created inside dedicated #[turbo_tasks::function] functions rather than inline at call sites.

Additionally:

  • Introduce a JsonValue newtype wrapping serde_json::Value that implements TaskInput, enabling the SWC wasm plugin list to be passed through a turbo-tasks function boundary and properly cached.
  • Replace the bool roundtrip in next_strip_page_exports with an ExportFilterInput enum that derives TaskInput, mirroring ExportFilter exhaustively (so a new upstream variant is a compile error, not a silent fallback).
  • Derive TaskInput on ActionsTransform and pass it directly to the cached function instead of converting to is_server: bool at the call site.
  • Replace all .expect("... config must exist") panics in option-gated plugin functions (emotion, styled_components, react_remove_properties, remove_console, relay) with .context(...)? for proper error propagation.
  • Add // TODO: use get_ecma_transform_rule instead comments to the ~10 transform functions that manually inline the same ModuleRule::new + ExtendEcmascriptTransforms pattern that get_ecma_transform_rule abstracts.

Why?

TransformPlugin is not serializable and not comparable. When a TransformPlugin is celled inline (i.e. ResolvedVc::cell(Box::new(...) as _)) inside a turbo-tasks function, a new cell is created on every invocation of the enclosing function, because the framework has no way to detect that the value is the same as before. This causes every task that depends on the TransformPlugin cell to be invalidated unnecessarily.

By moving the Vc::cell(...) call into its own narrow-scoped #[turbo_tasks::function], turbo-tasks can cache the cell by the function's inputs. If the inputs haven't changed, the function won't re-run, the existing cell is reused, and downstream tasks are not invalidated.

How?

Each inline ResolvedVc::cell(Box::new(SomeTransformer { ... }) as _) is replaced with:

some_transform_plugin(args).to_resolved().await?

#[turbo_tasks::function]
fn some_transform_plugin(args: ...) -> Vc<TransformPlugin> {
    Vc::cell(Box::new(SomeTransformer { ... }) as Box<dyn CustomTransformer + Send + Sync>)
}

Where a cached function needs to store a ResolvedVc in the resulting transformer struct, the parameter is declared as ResolvedVc<T> directly in the #[turbo_tasks::function] signature. The turbo_tasks macro rewrites ResolvedVc<T>Vc<T> in the external call-site signature, and the call site passes a dereferenced *resolved_vc. This avoids a redundant .to_resolved().await? inside the function body.

For the SWC wasm plugin case, serde_json::Value (used for per-plugin config) doesn't implement Hash or TaskInput. A JsonValue newtype is introduced with:

  • #[bincode(with = "turbo_bincode::serde_self_describing")] for serialization
  • Manual Hash impl that hashes the JSON string representation
  • Manual TaskInput impl (is_transient = false since the type contains no Vcs)

sokra and others added 2 commits April 15, 2026 16:32
…nvalidation

TransformPlugin is not serializable and not comparable. When celled inline
via ResolvedVc::cell(Box::new(...)), it causes invalidation of all dependent
tasks. Move each TransformPlugin creation into its own #[turbo_tasks::function]
so that Vc<TransformPlugin> is cached and reused across task invocations.

Co-Authored-By: Claude <noreply@anthropic.com>
- Introduce `JsonValue` newtype wrapping `serde_json::Value` with manual
  `Hash` + `TaskInput` impls so `serde_json::Value` can be used as a
  turbo-tasks function argument; use it to properly cache the SWC wasm
  plugin transform via a `#[turbo_tasks::function]`
- Replace the `bool` roundtrip in `next_strip_page_exports` with
  `ExportFilterInput` enum (derives `TaskInput`) mirroring `ExportFilter`,
  eliminating the silent fallback risk when a new variant is added
- Derive `TaskInput` on `ActionsTransform` and pass it directly to the
  turbo-tasks function instead of converting through `is_server: bool`
- Replace all `.expect("... config must exist")` panics in option-gated
  plugin functions with `.context(...)?` for proper error propagation
- Restore blank line before `Ok(())` in `modularize_imports`
- Add `// TODO: use get_ecma_transform_rule instead` comments to the 10
  transform functions that manually inline the same `ModuleRule::new` +
  `ExtendEcmascriptTransforms` pattern

Co-Authored-By: Claude <noreply@anthropic.com>
@nextjs-bot nextjs-bot added created-by: Turbopack team PRs by the Turbopack team. Font (next/font) Related to Next.js Font Optimization. Turbopack Related to Turbopack with Next.js. labels Apr 15, 2026
@nextjs-bot
Copy link
Copy Markdown
Contributor

nextjs-bot commented Apr 15, 2026

Tests Passed

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 15, 2026

Merging this PR will improve performance by 3.91%

⚡ 1 improved benchmark
✅ 16 untouched benchmarks
⏩ 3 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation app-page-turbo.runtime.prod.js[full] 639.4 ms 615.3 ms +3.91%

Comparing sokra/cache-transform-plugin (eb559c3) with canary (92b0789)

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
Contributor

nextjs-bot commented Apr 15, 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 456ms █▁▁█▁
Cold (Ready in log) 440ms 439ms █▃█▁█
Cold (First Request) 847ms 812ms ▁█▁█▁
Warm (Listen) 456ms 456ms ▁▅▁▅▅
Warm (Ready in log) 438ms 438ms ▃▃▅▅▄
Warm (First Request) 338ms 334ms ▅▄█▄▇
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 457ms 456ms ▅▅▅▅▅
Cold (Ready in log) 442ms 441ms ▃▂▂▃▂
Cold (First Request) 1.952s 1.958s ▂▅▃▅▃
Warm (Listen) 456ms 456ms ▅▅█▁▅
Warm (Ready in log) 440ms 441ms ▃▂▂▃▁
Warm (First Request) 1.949s 1.957s ▁▅▃▅▄

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 4.131s 4.167s ▃▁▅▃▅
Cached Build 4.176s 4.152s ▃▁▅▁▅
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 14.587s 14.649s ▄▃▂▃▁
Cached Build 14.741s 14.762s ▂▃▁▃▂
node_modules Size 493 MB 493 MB █████
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
053b9v5j96uu_.js gzip 8.56 kB N/A -
07rxhp_1_g4mu.js gzip 13.1 kB N/A -
096aa2uod0_wa.js gzip 9.82 kB N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB
0ea8vo2o6kp_s.js gzip 13.3 kB N/A -
0egovw6-5i1e3.js gzip 8.57 kB N/A -
0fli3_wppnim5.js gzip 12.9 kB N/A -
0gkovryjv05n3.js gzip 154 B N/A -
0ho78l23ixdy7.js gzip 155 B N/A -
0jrg8yoqn4b8e.js gzip 155 B N/A -
0k09jwjeb-tki.js gzip 13.8 kB N/A -
0kb7_ep3r1z0_.js gzip 10.1 kB N/A -
0kmgih16pb2zy.js gzip 9.24 kB N/A -
0lbv96yr1uj-x.js gzip 168 B N/A -
0wxpyd8r-vipl.js gzip 1.47 kB N/A -
1-z_xm7pzpbeh.js gzip 49.2 kB N/A -
11qa6mv6j4f0n.js gzip 160 B 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 -
1d9ms1y2nit5w.js gzip 65.5 kB N/A -
1elt1qium-r2m.css gzip 115 B 115 B
1odlz7_bqi4gv.js gzip 160 B N/A -
1s-lssnhdqk31.js gzip 156 B N/A -
1xz7fhgdco-x_.js gzip 8.59 kB N/A -
2__-e_ym8n788.js gzip 450 B N/A -
22o6xd9_ywdu6.js gzip 233 B N/A -
2avcdi72oetam.js gzip 156 B N/A -
2h9_xc-4i5_oy.js gzip 152 B N/A -
2k1q_gre6hp0-.js gzip 8.56 kB N/A -
2kvj8yrfznmwx.js gzip 5.69 kB N/A -
2sgqisp4604si.js gzip 8.51 kB N/A -
2u7io78pjtekk.js gzip 156 B N/A -
3_lh2q04233ur.js gzip 70.8 kB N/A -
342ijzvrpe53h.js gzip 2.29 kB N/A -
3p575fh88tejl.js gzip 155 B N/A -
3v9s-fz24f8oo.js gzip 10.4 kB N/A -
3zdh_rd16s0i8.js gzip 157 B N/A -
420pn_b9x-3ir.js gzip 8.62 kB N/A -
43l229j-6khv5.js gzip 156 B N/A -
44un3--wmqiyh.js gzip 7.61 kB N/A -
turbopack-0x..2c1o.js gzip 4.19 kB N/A -
turbopack-0y..rjc9.js gzip 4.2 kB N/A -
turbopack-1k..rbj5.js gzip 4.19 kB N/A -
turbopack-1q.._m7g.js gzip 4.19 kB N/A -
turbopack-2-..4mlc.js gzip 4.17 kB N/A -
turbopack-22..h7oj.js gzip 4.19 kB N/A -
turbopack-25..lf2b.js gzip 4.19 kB N/A -
turbopack-2b..nby5.js gzip 4.19 kB N/A -
turbopack-2m..wm_6.js gzip 4.19 kB N/A -
turbopack-2n.._pfr.js gzip 4.19 kB N/A -
turbopack-3b..ydgx.js gzip 4.19 kB N/A -
turbopack-3e..0ajm.js gzip 4.19 kB N/A -
turbopack-3l..dvr5.js gzip 4.19 kB N/A -
turbopack-3m..qy5_.js gzip 4.19 kB N/A -
03_szppy5nakp.js gzip N/A 49.2 kB -
0arkbdqpxc37i.js gzip N/A 8.6 kB -
0iuamvde1mkye.js gzip N/A 166 B -
0mz-rbiau93_g.js gzip N/A 161 B -
0tvekitj587fh.js gzip N/A 8.51 kB -
0vcsfpshdbpoi.js gzip N/A 157 B -
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 -
16bh0aqv641xv.js gzip N/A 157 B -
192q2suy7rwb_.js gzip N/A 8.56 kB -
1ab2xruymo-oj.js gzip N/A 449 B -
1im7xphq6dxob.js gzip N/A 159 B -
1ng_99sotnhzq.js gzip N/A 159 B -
1qui_nsqw5190.js gzip N/A 157 B -
1wzrm0xjjbzn5.js gzip N/A 10.1 kB -
25a1yz7zua29z.js gzip N/A 13.8 kB -
2f2bp_kexa37v.js gzip N/A 13.3 kB -
2fpb7l1pbce6e.js gzip N/A 65.5 kB -
2i5v1taq97gql.js gzip N/A 8.59 kB -
2jrvadyrjp0yr.js gzip N/A 155 B -
2n3vh0w0tnb_2.js gzip N/A 8.52 kB -
2u_rpxq3tzytl.js gzip N/A 233 B -
2wfbykz1avdns.js gzip N/A 8.56 kB -
32sifemopndov.js gzip N/A 10.4 kB -
368lim5wq0o0r.js gzip N/A 12.9 kB -
37ba9t8u6k_9f.js gzip N/A 156 B -
3aouhp_o4j3ta.js gzip N/A 151 B -
3de283pazbu99.js gzip N/A 9.81 kB -
3drqjohogojbw.js gzip N/A 5.69 kB -
3g8l1m2-o-ewi.js gzip N/A 13.1 kB -
3gp2o_v7jcw6s.js gzip N/A 8.57 kB -
3kab57ds4zwc9.js gzip N/A 157 B -
3qu7g1q2l_x2n.js gzip N/A 9.24 kB -
3vucw_kkxf-6j.js gzip N/A 155 B -
3zycucbo0ee-y.js gzip N/A 70.8 kB -
41p1znhcs5rk8.js gzip N/A 155 B -
43j6fjudq0a3_.js gzip N/A 8.62 kB -
turbopack-02..ejcf.js gzip N/A 4.19 kB -
turbopack-04..36zk.js gzip N/A 4.19 kB -
turbopack-05..ed1i.js gzip N/A 4.19 kB -
turbopack-08..ymqg.js gzip N/A 4.19 kB -
turbopack-1e..75yv.js gzip N/A 4.17 kB -
turbopack-1e..lwrg.js gzip N/A 4.19 kB -
turbopack-1s..5095.js gzip N/A 4.19 kB -
turbopack-1w..leed.js gzip N/A 4.19 kB -
turbopack-1x..h7hw.js gzip N/A 4.19 kB -
turbopack-29..nv35.js gzip N/A 4.19 kB -
turbopack-2d..42om.js gzip N/A 4.2 kB -
turbopack-3f..ewzo.js gzip N/A 4.19 kB -
turbopack-3m..3yye.js gzip N/A 4.19 kB -
turbopack-3z..lv_i.js gzip N/A 4.19 kB -
Total 465 kB 465 kB ⚠️ +39 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 715 B 718 B
Total 715 B 718 B ⚠️ +3 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 434 B 429 B 🟢 5 B (-1%)
Total 434 B 429 B ✅ -5 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.3 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 255 B 255 B
main-HASH.js gzip 39.4 kB 39.4 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.7 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 235 kB 235 kB ✅ -620 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 273 kB 273 kB
Total 399 kB 398 kB ✅ -442 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 617 B 618 B
middleware-r..fest.js gzip 156 B 156 B
middleware.js gzip 43.9 kB 44.2 kB
edge-runtime..pack.js gzip 842 B 842 B
Total 45.5 kB 45.8 kB ⚠️ +311 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.38 MB 4.38 MB
index.pack gzip 112 kB 113 kB
index.pack.old gzip 114 kB 115 kB
Total 4.61 MB 4.6 MB ✅ -912 B

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 346 kB 346 kB
app-page-exp..prod.js gzip 192 kB 192 kB
app-page-tur...dev.js gzip 346 kB 346 kB
app-page-tur..prod.js gzip 192 kB 192 kB
app-page-tur...dev.js gzip 342 kB 342 kB
app-page-tur..prod.js gzip 190 kB 190 kB
app-page.run...dev.js gzip 343 kB 343 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 62.9 kB 62.9 kB
Total 3.06 MB 3.06 MB ⚠️ +2 B
📎 Tarball URL
https://vercel-packages.vercel.app/next/commits/eb559c3567b907db04bab056195b83a720f66f9d/next

@sokra sokra requested a review from mischnic April 15, 2026 18:23
@sokra sokra marked this pull request as ready for review April 15, 2026 18:23
Comment thread crates/next-core/src/next_shared/transforms/next_react_server_components.rs Outdated
Comment thread crates/next-core/src/next_shared/transforms/server_actions.rs
- Fix trailing space typo in Middleware row of RSC context table
- Use ResolvedVc<RcStr> and ResolvedVc<CacheKinds> directly as
  parameters in next_server_actions_transform_plugin, avoiding
  .to_resolved().await? inside the function body

Co-Authored-By: Claude <noreply@anthropic.com>
@sokra sokra merged commit f497222 into canary Apr 16, 2026
339 of 343 checks passed
@sokra sokra deleted the sokra/cache-transform-plugin branch April 16, 2026 06:17
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

created-by: Turbopack team PRs by the Turbopack team. Font (next/font) Related to Next.js Font Optimization. locked Turbopack Related to Turbopack with Next.js.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants