Skip to content

perf: evaluate codeSplitting test/name functions before the assignment loop - #10740

Closed
MarshallOfSound wants to merge 1 commit into
rolldown:mainfrom
MarshallOfSound:perf/code-splitting-callbacks
Closed

MarshallOfSound wants to merge 1 commit into
rolldown:mainfrom
MarshallOfSound:perf/code-splitting-callbacks

Conversation

@MarshallOfSound

Copy link
Copy Markdown
Contributor

Function-valued codeSplitting.groups[].test / .name are JS callbacks, and build_module_groups awaited them one (module × group) pair at a time inside the assignment loop — one main-thread round trip each. On a large app with a few function groups that is ~10⁵ sequential hops between link and chunk rendering.

This evaluates them before the loop and caches the results: calls are dispatched in bounded batches, one callback function at a time (every module for group 0's test, then group 1's, …, then the name callbacks likewise), each in the existing stable_id order. A given JS function's queued calls run in dispatch order, so a stateful callback still sees a deterministic sequence; different functions are never in flight together. The set of calls, group membership and output are unchanged.

Synthetic project (20k leaf modules shared across 200 dynamic entries, 6 groups with function test+name, 95,545 callback invocations), second generate() on a warm graph, output byte-identical:

generate()
main 2.30 s
this PR 0.44 s

On the real app this came from (~20k modules, 3 function groups) it removes ~3 s from a production build.

fixture
// gen.mjs — writes src/ and entry.js
import fs from 'node:fs';
const N = 20000, E = 200; fs.mkdirSync('src', { recursive: true }); let entry = '';
for (let i = 0; i < N; i++) fs.writeFileSync(`src/m${i}.js`, `export const v${i} = ${i};\n`);
for (let e = 0; e < E; e++) { let s = ''; for (let k = 0; k < 300; k++) { const i = (e*7919 + k*104729) % N; s += `export { v${i} } from "./m${i}.js";\n`; } fs.writeFileSync(`src/e${e}.js`, s); entry += `export const p${e} = () => import("./src/e${e}.js");\n`; }
fs.writeFileSync('entry.js', entry);
// bench.mjs
const groups = Array.from({ length: 6 }, (_, g) => ({ name: (id) => 'grp' + g + '-' + (id.length % 7), test: (id) => id.endsWith(g + '.js'), priority: g }));
const b = await rolldown({ input: './entry.js' }); await b.generate({});
console.time('generate'); await b.generate({ codeSplitting: { groups } }); console.timeEnd('generate');

AI-assisted (Claude Code); reviewed and benchmarked by me.

@codspeed

codspeed Bot commented Aug 22, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing MarshallOfSound:perf/code-splitting-callbacks (bd43f66) with main (12b5cd0)

Open in CodSpeed

Footnotes

  1. 10 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. ↩

IWANABETHATGUY added a commit that referenced this pull request Aug 24, 2026
Function-valued `codeSplitting.groups[].test` and `.name` are JS
callbacks, and each call crossed the napi boundary on its own, so a
build with M modules and G function groups paid M * G crossings before
it rendered a chunk.

The JS side now hands the binding one shim per callback that takes the
whole id array and loops in JS, so a group crosses at most twice. A
group's calls are contiguous now instead of interleaved with the other
groups', and a group's whole `test` pass runs before its first `name`
call, but each callback still sees its own ids in `stable_id` order.

<details><summary>benchmark</summary>

20k leaf modules shared across 200 dynamic entries, 6 groups with
function `test` + `name`. Best of 11 `generate()` calls, release builds
of all three, byte-identical output.

| | `generate()` | the 86,856 `test` calls alone |
|---|---|---|
| main | 1243 ms | 803 ms |
| #10740 | 412 ms | 57 ms |
| this PR | 361 ms | 18 ms |

The second column comes from timing the same app again with the six
`test` functions replaced by regexes that match identically. The output
stays byte-identical, so the only difference is that those calls never
leave Rust. #10740 overlaps one crossing per call with `try_join_all`,
which leaves 656 ns each; batching the payload leaves 207 ns.

What is left is mostly not the user's code — replaying those same 86,856
calls in plain JS costs 0.7 ms, so ~95% of the 18 ms is marshalling one
id string per module per group. Sending the id list once for all groups
would cut that further.

Benchmark produced with Claude Code.

</details>

This and #10740 solve the same problem and cannot both land.
@MarshallOfSound

Copy link
Copy Markdown
Contributor Author

Superseded by #10745

@MarshallOfSound
MarshallOfSound deleted the perf/code-splitting-callbacks branch August 27, 2026 19:38
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