Skip to content

fix(binding): kept plugin contexts no longer keep a finished build alive - #11899

Draft
shulaoda wants to merge 2 commits into
mainfrom
fix/plugin-context-build-lifetime
Draft

shulaoda wants to merge 2 commits into
mainfrom
fix/plugin-context-build-lifetime

Conversation

@shulaoda

@shulaoda shulaoda commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Stacked on #11898, which reverts #11855.
Related to #10268

The GC of the JS engine frees the native data behind a binding object only when it collects the object. On wasm it does not see Wasm memory, so it can wait very long, and the data of every build piles up. #11855 and #10268 work around this by calling dropInner() from JS after each hook, on threadless WASI only.

This PR follows one rule instead. The GC that frees a piece of memory must be able to see it, or must not be needed at all.

What changes

  • Plugin contexts no longer keep a build alive, on every platform. BindingPluginContext holds the plugin context weakly, and the plugin driver owns it until the build finishes. A context forked for one resolveId call points to the driver's context and keeps the skipped calls itself. BindingTransformPluginContext holds the transform context weakly, and the driver keeps each transform context until the module's sourcemap chain changes. After the build, a kept context throws This plugin context can no longer be used because its build has finished. parse, meta and the log methods keep working.
  • On wasm, JS gets plain data instead of native objects for output chunks and assets, rendered chunks and modules, the renderChunk meta and module info. The data lives in the JS heap, where the GC sees it, so nothing needs to be dropped. Each of these is Either<BindingX, BindingXData> in the binding, so the generated types are the same on every target. Native keeps the lazy objects.

No dropInner() is needed for any of these classes.

Behavior of a kept context

Rollup 4.68.7, main, #10268 on threadless WASI and this PR, all run with the same script.

Case Rollup main #10268 This PR
Another plugin calls a kept getCombinedSourcemap while the module's chain is unchanged works works throws works
The same after the chain changed works throws throws throws
Kept in resolveId, used later in the same build works works throws works
Kept in the first generate(), used in the second builds once resolve throws, the rest read the first build throws throws
emitFile, load or resolve after close() no error, the result is never used no error, resolve throws throws throws
getModuleInfo, getModuleIds or getFileName after close() returns the data returns the data throws throws
parse, meta or warn after close() works works works works

The only case where this PR differs from Rollup with a useful result is reading build data after close(). Keeping that data would mean keeping it until GC again.

Memory

All numbers use the 300-module graph of the workerd suite of #10268.

Wasm in Node (wasm32-wasip1-threads), live Rust heap after each of 100 builds, average and maximum in MiB. The Wasm heap was counted with an allocator wrapper that is not part of this PR. hooks adds moduleParsed, renderChunk reading chunk.modules and generateBundle reading the code.

nohooks hooks gc() after each build
main 2.82 / 5.17 2.40 / 4.14 0.35 / 0.44
Weak contexts only 2.78 / 7.16 1.99 / 3.38 0.35 / 0.43
This PR 0.61 / 0.81 0.47 / 0.65 0.12 / 0.13

Node still runs its GC now and then, so the weak contexts alone change little here. In workerd the GC does not collect these objects, and the contexts matter much more. This is the workerd suite of #10268 (threadless WASI, one reused instance, 20 rebuilds), as Wasm memory growth per rebuild in MiB.

nohooks transform-ast
#10268 with its JS release turned off (as main) 0.746 1.000
The same with weak contexts 0.426 0.511
#10268 0.165 0.206
#10268 with weak contexts, without dropInner() on the three context classes 0.188 0.257

On native, the live Rust heap over 100 builds without gc() goes from 3.1 to 29.1 MiB on main and from 0.8 to 26.8 MiB with weak contexts. The build already clears most of its data when it finishes. The rest is outputs, which native keeps lazy and #11655 reports to V8.

Cost

A wasm build copies every field when the data crosses to JS, also the fields that JS never reads. For the hooks case above that is 108.7 to 112.2 ms per build on main and 114.9 to 116.0 ms with this PR, about 4 ms. A generateBundle or writeBundle hook gets its own copy of the bundle, so each such hook adds a copy. Native does not change.

Not covered

  • BindingNormalizedOptions and BindingChunkingContext stay native objects. Both are small and shared by the whole build, and the options objects that hooks get already hold plain values.
  • A native MagicString gives its content to Rust when it is sent, so only one that is never sent waits for the GC.
  • Each hook call still creates one JS context object, and its small native part waits for the GC. One context per plugin per build, as in Rollup, would remove it.

@netlify

netlify Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit 62670b1
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/6b04e4464da38df1b0e5f007

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview URL: https://fix-plugin-context-build-lifetime-rolldown-rs.voidzero-docs.workers.dev (commit 62670b1)

This URL reflects your latest Preview deployment

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://53ce358a-rolldown-rs.voidzero-docs.workers.dev 62670b1 2026-11-24T01:14:02.419Z Visit the dashboard ↗
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://526af342-rolldown-rs.voidzero-docs.workers.dev 0fb4ea4 2026-11-23T20:48:34.453Z Visit the dashboard ↗

@shulaoda
shulaoda force-pushed the fix/plugin-context-build-lifetime branch from 0fb4ea4 to 2f7ae9b Compare November 24, 2026 01:05
@shulaoda
shulaoda force-pushed the revert/binding-drop-inner branch from 7bdc1b5 to 9d289ee Compare November 24, 2026 01:05
Base automatically changed from revert/binding-drop-inner to main November 24, 2026 01:11
shulaoda added a commit that referenced this pull request Nov 24, 2026
…ract release_arc helper (#11855)" (#11898)

Related to #11899

This reverts #11855, which is not in any release yet.

#11655 fixes the related memory problem. It returns freed memory to the
OS once rolldown is idle, and lets V8 GC track the native memory that an
output holds. #11855 only adds an API to drop native data early. It does
not fix the actual problem, and it is not automatic, because every
caller has to know when to drop.

On wasm, #11655 does not help, because the GC does not see Wasm memory.
That case needs a better solution than calling `dropInner()` from JS
after every hook. #11899 is that solution. Plugin contexts hold their
build weakly, so the build frees its data when it finishes, and on wasm
JS gets plain data instead of native objects. No class needs
`dropInner()`, and a context that a plugin keeps still works for the
rest of its build, as in Rollup. #11899 has the measurements and a
comparison with Rollup, main and #10268.

#10268 calls these methods, so it needs to follow this change.
@shulaoda
shulaoda force-pushed the fix/plugin-context-build-lifetime branch from 2f7ae9b to 62670b1 Compare November 24, 2026 01:11
@shulaoda
shulaoda removed this pull request from stack #11900 November 24, 2026 01:12

This branch has not been deployed

No deployments
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.

1 participant