Repository navigation
perf: send only changed chunk properties back to Rust from output hooks - #11488
Conversation
When a `generateBundle` or `writeBundle` hook assigned any property of a chunk, the chunk went back to Rust with all of its properties. Rust serialized the chunk's sourcemap, JavaScript parsed it, and Rust parsed it again, and the code was copied both ways. Rust also replaced the chunk data, so the next plugin that read `chunk.code` copied it again. Vite's `vite:css-post` assigns `chunk.imports` of every chunk whenever a build has a pure CSS chunk, so a large Vite build paid this for every chunk. `collectChangedBundle` now sends only the properties that `update_output_chunk` applies, that the plugin read or assigned, and that differ from Rust's values. It sends nothing for a chunk with no change. `JsOutputChunk` keeps only the applied fields, all optional except `filename` and `isEntry`, and Rust keeps every field that is left out.
Generated by `just build-browser`, as the Node Validation CI job does.
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
A `generateBundle` or `writeBundle` hook that assigns a non-string to `chunk.code`, or a non-array to `chunk.imports` or `chunk.dynamicImports`, now fails the build with an error that names the chunk and the property. Rust reads a field set to `undefined` as unchanged, so a plugin bug such as `chunk.code = result?.code` kept the old code silently.
…k` field `update_output_chunk` applies every field of `JsOutputChunk`, and `collectChunkChange` sends them by hand. A field added on the Rust side now fails type-check in `collectChunkChange` until it is listed there.
|
I pushed two commits on top so that two mistakes fail loudly instead of being ignored:
Main rejects those assignments too, with |
|
/ecosystem-ci run |
✅ Deploy Preview for rolldown-rs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
nice! thank you. |
@rolldown/browser
@rolldown/debug
rolldown
@rolldown/binding-android-arm-eabi
@rolldown/binding-android-arm64
@rolldown/binding-darwin-arm64
@rolldown/binding-darwin-x64
@rolldown/binding-freebsd-x64
@rolldown/binding-linux-arm-gnueabihf
@rolldown/binding-linux-arm64-gnu
@rolldown/binding-linux-arm64-musl
@rolldown/binding-linux-ppc64-gnu
@rolldown/binding-linux-s390x-gnu
@rolldown/binding-linux-x64-gnu
@rolldown/binding-linux-x64-musl
@rolldown/binding-openharmony-arm64
@rolldown/binding-wasm32-wasi
@rolldown/binding-win32-arm64-msvc
@rolldown/binding-win32-x64-msvc
commit: |
|
📝 Ran ecosystem CI with rolldown
✅ |
|
📝 Ran ecosystem CI with rolldown
✅ module-federation, marko, analogjs, astro, laravel, vite-plugin-svelte, quasar, vike, vinext, vitepress, storybook, nuxt, vite-plugin-pwa, sveltekit, unocss, nitro, waku, vuepress, vite-setup-catalogue, vite-plugin-react, vite-environment-examples, vitest, vite-plugin-vue |
## [1.3.1] - 2026-11-09 ### 🚀 Features - inline-common-chunks: inline chunks using `import.meta.ROLLDOWN_FILE_URL_*` (#11543) by @mattlewis92 ### 🐛 Bug Fixes - cli: apply --environment before loading the binding (#11553) by @Brooooooklyn - rolldown_plugin_vite_resolve: escape module names in generated error stubs (#11584) by @sapphi-red - add the code location to transform logs and errors at position 0 (#11568) by @chinmayajha - match sticky regexes only at the start of the string (#11569) by @chinmayajha - iife/umd: skip namespace markers when the entry has no exports (#11567) by @lazerg - keep a declaration's name when it clashes with an external import (#11052) by @Nic-Polumeyv - drop top-level statements made dead by imported constants before chunk linking (#11053) by @elliott-with-the-longest-name-on-github - keep `this.resolve` custom options apart for overlapping calls (#11505) by @wan9chi - transform: reuse TsconfigCache's answer when no tsconfig owns the file (#11265) by @sarankumar1325 - treat top-level `this` of a required module without module syntax as its exports (#11271) by @sarankumar1325 - keep side effects of a module whose exported constant is inlined (#11440) by @hyfdev - leave same-chunk dynamic imports out of `dynamicImports` (#11156) by @kkkhs ### 🚜 Refactor - prepend transferred imports as AST fragments (#11464) by @Dunqing ### 📚 Documentation - show `vp` first in setup commands (#11503) by @IWANABETHATGUY ### ⚡ Performance - incremental: reuse link-time facade symbols across rebuilds (#11542) by @mattlewis92 - incremental: share module sourcemap chains across cache clones (#11540) by @mattlewis92 - chunk_optimizer: answer runtime-merge reachability from one chunk-level graph (#11541) by @mattlewis92 - send only changed chunk properties back to Rust from output hooks (#11488) by @anonrig ### 🧪 Testing - remove review fixtures committed by mistake in #11052 (#11574) by @hyfdev - wasi: cover raw context destruction (#11561) by @Brooooooklyn ### ⚙️ Miscellaneous Tasks - deps: update rust crates (#11573) by @renovate[bot] - deps: update mimalloc-safe to 0.1.70 (#11589) by @shulaoda - renovate: update GitHub Actions that are pinned to a commit (#11583) by @hyfdev - skip Playwright browser downloads in Prepare Release (#11579) by @shulaoda - pin the rolldown crates to exact versions of each other on release (#11577) by @shulaoda - update `CodSpeedHQ/action` to v5.6.3 for Ubuntu 26.04 (#11575) by @hyfdev - remove stale entries from `.typos.toml` (#11571) by @iiio2 - pick the Prepare Release target branch from the version (#11525) by @shulaoda - run every OS on pushes to maintenance branches (#11520) by @shulaoda - rebase maintenance Vite branches onto their Vite minor (#11515) by @shulaoda - run CI on pushes to maintenance branches (#11516) by @shulaoda - support patch releases from maintenance branches (#11393) by @shulaoda ### ❤️ New Contributors * @chinmayajha made their first contribution in [#11568](#11568) * @lazerg made their first contribution in [#11567](#11567) * @elliott-with-the-longest-name-on-github made their first contribution in [#11053](#11053) * @sarankumar1325 made their first contribution in [#11265](#11265) * @anonrig made their first contribution in [#11488](#11488) * @kkkhs made their first contribution in [#11156](#11156) Co-authored-by: shulaoda <[email protected]>
Why
In a large Vite 8 app, the production client build has 2,034 chunks with 143 MB of hidden sourcemaps, and the SSR build has 3,371 chunks with 146 MB.
generateBundlehooks are expensive enough there that the app limits some of its plugins to theclientenvironment to avoid them.A CPU profile of that build shows that most of this cost comes from sending chunks back to Rust after a hook. When a
generateBundleorwriteBundlehook assigned any property of a chunk,collectChangedBundlesent the chunk back with all of its properties:moduleIds,exportsand other fields were copied both ways.update_output_chunkignores most of them.outputCodeCachefrom perf: avoid copying module code forrenderedLengthand sharechunk.codeacross plugins #11342 missed, and the next plugin that readchunk.codecopied it out again.Vite's
vite:css-postrunschunk.imports = chunk.imports.filter(...)on every chunk whenever a build has a pure CSS chunk, and most chunks end up with the same file names. So every chunk took this round trip. In that app's build,collectChangedBundletook 720 to 1,015 ms of main-thread time, 607 to 866 ms of it inchunk.map. Parsing those maps again in Rust adds more time, which a JavaScript profile does not show.What
collectChangedBundlenow sends back only the chunk properties thatupdate_output_chunkapplies (code,map,imports,dynamicImports,isEntry,fileName). Of those, it sends the ones that the plugin changed:codeonly when it was assigned. A string the plugin read cannot have changed.mapwhen it was read or assigned, because a read map can be changed in place.importsanddynamicImportswhen they were read or assigned and differ from Rust's array. This also catches changes made in place.fileNameandisEntryare always sent. They are cheap, andisEntrytells a chunk apart from an asset inEither<JsOutputChunk, JsOutputAsset>.A chunk with no change is not sent at all. Rust keeps its data, and
outputCodeCachekeeps serving its code.JsOutputChunknow has only the applied fields, and every field exceptfilenameandisEntryis optional.update_output_chunkkeeps any field that is left out.map: nullstill removes the map, with the sameOption<Either<BindingSourcemap, Null>>pattern thatrenderChunkuses. Assets are unchanged.Plugins should see no difference. Each change reaches the next hook and the written output as before, including changes made in place to a read
importsarray ormap,map = null, renames, and deleted outputs.Measured effect
Interleaved A/B on release builds (
just build-rolldown-release): base ismainat 30a5421, patched is this branch. Each run is a fresh Node process, and each pair alternates which build runs first. The workload is a generated project shaped like that app's client build: 2,182 chunks, 30 MB of minified JS, and 136 MB of hidden sourcemaps. It runs throughrolldown()andwrite()/generate()with these plugins:generateBundlehooks that only read (moduleIdsandimportsof every chunk)vite:css-postwith 256 pure CSS chunksvite:build-import-analysis, which reads every chunk's code and rewrites 528 chunks with their mapsvite:manifestvite:reporterwriteBundlehookThe "generateBundle phase" column runs from the first
generateBundlehook to a final no-op one, so it includes Rust applying each hook's changes.generate()generate()generate()generate()write()write()The output files had the same hash in every run of both builds. Other jobs were running on this Mac during the runs (load average 10 to 24). That load caused the few lost pairs, and it makes the full range of the base times (up to 2.7 s) wider than the improvement.
In the app itself, I ran its production build (
vite build --app) with each build copied in throughpackages/perf-kit/harness.mjs rolldown-install. CPU profiles of two rounds showed this main-thread time in the output-hook bridge:collectChangedBundlechunk.mapgetterchunk.codegetterWith hashes normalized, the patched output matched the base output in all 10,958 files except five. Those five also differ between two base builds: a script the app obfuscates anew on every build (with its map, for client and server) and the client manifest.
The bundle objects that each hook creates are not part of this change.
createOutputBundlestill spends 450 to 600 ms in that build.Tests
packages/rolldown/tests/behaviors/output-bundle-changes.test.tsbuilds a small project through the public API. One test applies thevite:css-postpattern: it assigns every chunk'simports, rewrites the importers, and deletes the pure CSS chunk. The test checks that the next hook,writeBundle, the written files and the returned output see the newimportsandcode, the unread maps unchanged, and the deleted chunk gone. A second test checks that changes made in place to a readimportsarray andmap, andmap = null, reach the next hook and the output.packages/rolldown/tests/utils/transform-to-rollup-output.test.tsrunstransformToOutputBundleandcollectChangedBundleon stand-ins for the native chunk that record which getters run. The tests check that a chunk assigned the values it had is not sent, that only changed properties are sent, and that no code, map ormoduleIdsis copied out of Rust unless the plugin read it. The behavior tests pass onmainand on this branch. The unit tests use the newChangedOutputsshape, so they cannot run againstmain. A scratch copy of the same scenario run againstmain's source showed the old behavior: the unchanged chunk was sent with all of its properties, and the maps, code andmoduleIdswere copied out of Rust.Commands:
No
internal-docs/page covers the output-hook bridge, so this PR has no docs change. The reasons are in the comments oncollectChunkChangeandJsOutputChunk.