Repository navigation
bundler: with --splitting --target bun, require() of an ES module is a chunk boundary #40519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
3aba854
8e47fee
6f22c10
90eaf78
ef0d359
1e0c413
ccd5f63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| app*/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Generates a synthetic app whose entry reaches N "tool" modules only through | ||
| // function-scoped require() calls in a registry, and invokes K of them at | ||
| // startup. Each tool is a few KB of code so that bundling/bytecode cost is | ||
| // measurable. Usage: bun gen.ts <outdir> <N> <K> | ||
| import { mkdirSync, writeFileSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
|
|
||
| const [outdir, nStr = "400", kStr = "8"] = process.argv.slice(2); | ||
| const N = +nStr; | ||
| const K = +kStr; | ||
| mkdirSync(join(outdir, "tools"), { recursive: true }); | ||
|
|
||
| function toolSource(i: number): string { | ||
| const fns: string[] = []; | ||
| for (let j = 0; j < 40; j++) { | ||
| fns.push(` | ||
| export function op${i}_${j}(input) { | ||
| const parts = String(input).split(",").map((s, idx) => ({ s, idx, w: s.length * ${j + 1} })); | ||
| let acc = ${i * 7 + j}; | ||
| for (const p of parts) { | ||
| acc = (acc * 31 + p.w + p.idx) % 1000003; | ||
| if (p.s.startsWith("x")) acc ^= ${j * 13 + 1}; | ||
| } | ||
| return { tool: ${i}, op: ${j}, acc, tag: "tool${i}-op${j}-" + acc.toString(36) }; | ||
| }`); | ||
| } | ||
| return `import { helper } from "../helper.ts"; | ||
| ${fns.join("\n")} | ||
| export const Tool${i} = { | ||
| name: "tool${i}", | ||
| run(input) { return helper(op${i}_0(input).tag); }, | ||
| ops: [${Array.from({ length: 40 }, (_, j) => `op${i}_${j}`).join(", ")}], | ||
| }; | ||
| `; | ||
| } | ||
|
|
||
| for (let i = 0; i < N; i++) { | ||
| writeFileSync(join(outdir, "tools", `tool${i}.ts`), toolSource(i)); | ||
| } | ||
|
|
||
| writeFileSync(join(outdir, "helper.ts"), `export function helper(s) { return s.toUpperCase(); }\n`); | ||
|
|
||
| const cases = Array.from( | ||
| { length: N }, | ||
| (_, i) => ` case "tool${i}": return require("./tools/tool${i}.ts").Tool${i};`, | ||
| ).join("\n"); | ||
| writeFileSync( | ||
| join(outdir, "registry.ts"), | ||
| `export function getTool(name) { | ||
| switch (name) { | ||
| ${cases} | ||
| } | ||
| throw new Error("unknown tool " + name); | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| writeFileSync( | ||
| join(outdir, "entry.ts"), | ||
| `import { getTool } from "./registry.ts"; | ||
| const used = [${Array.from({ length: K }, (_, i) => `"tool${Math.floor((i * N) / K)}"`).join(", ")}]; | ||
| let out = ""; | ||
| for (const name of used) out += getTool(name).run("a,b,xc,d") + ";"; | ||
| if (process.env.BENCH_REPORT) { | ||
| console.log(JSON.stringify({ | ||
| ms_since_start: +performance.now().toFixed(1), | ||
| footprint_mb: +((Bun.unsafe.memoryFootprint() ?? -1) / 1048576).toFixed(1), | ||
| out_len: out.length, | ||
| })); | ||
| } | ||
| `, | ||
| ); | ||
| console.log(`generated ${N} tools, ${K} used, in ${outdir}`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Builds the generated app four ways (splitting ± split-require, source and | ||
| // bytecode) with the given bun binary, then measures startup wall time, the | ||
| // in-process time to the end of the entry module, and the physical footprint. | ||
| // Runs are interleaved across variants so drift hits them equally. | ||
| // Usage: bun run.ts <bun-binary> <appdir> [runs] | ||
| import { statSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
|
|
||
| const [bun, appdir, runsStr = "15"] = process.argv.slice(2); | ||
| const runs = +runsStr; | ||
| if (!bun || !appdir || !(runs > 0)) { | ||
| console.error("usage: bun run.ts <bun-binary> <appdir> [runs]"); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const variants: [name: string, args: string[]][] = [ | ||
| ["source", []], | ||
| ["source-split-require", ["--split-require"]], | ||
| ["bytecode", ["--bytecode"]], | ||
| ["bytecode-split-require", ["--bytecode", "--split-require"]], | ||
| ]; | ||
|
|
||
| function median(xs: number[]) { | ||
| const s = [...xs].sort((a, b) => a - b); | ||
| return s[s.length >> 1]; | ||
| } | ||
|
|
||
| const built: { name: string; outfile: string; build_ms: number; wall: number[]; inproc: number[]; footprint: number[] }[] = | ||
| []; | ||
| for (const [name, args] of variants) { | ||
| const outfile = join(appdir, "out", name); | ||
| const t0 = performance.now(); | ||
| const build = Bun.spawnSync({ | ||
| cmd: [ | ||
| bun, | ||
| "build", | ||
| "--compile", | ||
| "--splitting", | ||
| "--target=bun", | ||
| "--format=esm", | ||
| "--minify", | ||
| ...args, | ||
| `--outfile=${outfile}`, | ||
| join(appdir, "entry.ts"), | ||
| ], | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| if (build.exitCode !== 0) { | ||
| console.error(name, "build failed:\n", build.stderr.toString()); | ||
| process.exit(1); | ||
| } | ||
| built.push({ name, outfile, build_ms: performance.now() - t0, wall: [], inproc: [], footprint: [] }); | ||
| } | ||
|
|
||
| let outLen: number | undefined; | ||
| for (let i = 0; i < runs; i++) { | ||
| for (const v of built) { | ||
| const s = performance.now(); | ||
|
Comment on lines
+57
to
+59
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚀 Performance & Scalability | 🟡 Minor | ⚡ Quick win Rotate the variant order for each runtime round. Each round runs 🤖 Prompt for AI Agents |
||
| const p = Bun.spawnSync({ | ||
| cmd: [v.outfile], | ||
| env: { ...process.env, BENCH_REPORT: "1" }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| v.wall.push(performance.now() - s); | ||
| if (p.exitCode !== 0) { | ||
| console.error(v.name, "run failed:\n", p.stderr.toString()); | ||
| process.exit(1); | ||
| } | ||
| const r = JSON.parse(p.stdout.toString()); | ||
| v.inproc.push(r.ms_since_start); | ||
| v.footprint.push(r.footprint_mb); | ||
| if (outLen !== undefined && outLen !== r.out_len) { | ||
| console.error(v.name, "output differs from the other variants"); | ||
| process.exit(1); | ||
| } | ||
| outLen = r.out_len; | ||
|
Comment on lines
+71
to
+78
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win Compare the complete generated result.
🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
|
|
||
| for (const v of built) { | ||
| console.log( | ||
| JSON.stringify({ | ||
| variant: v.name, | ||
| build_ms: Math.round(v.build_ms), | ||
| exe_bytes: statSync(v.outfile).size, | ||
| wall_ms_median: +median(v.wall).toFixed(1), | ||
| entry_done_ms_median: +median(v.inproc).toFixed(1), | ||
| footprint_mb_median: median(v.footprint), | ||
| }), | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Validate required and integer generator arguments.
If
outdiris absent,join(outdir, "tools")throws a path type error instead of showing the documented usage. IfNis fractional or non-positive, the tool-file loop andArray.from()can generate different module sets. Reject invalidoutdir,N, andKvalues before creating files.🤖 Prompt for AI Agents