Repository navigation
js_parser: compress symbol link chains when following merged declarations #42242
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4af2779
js_parser: compress symbol link chains when following merged declarat…
robobun 7b9f0c3
test: shrink the symbol-chain timing test to fit the default timeout
robobun 5d378a0
test: build the distinct-name baselines with a native join
robobun 85e256c
ast: shorten the follow_symbols doc comment
robobun 010eb4d
ast: make the follow_symbols doc comment one line
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
test/js/bun/transpiler/transpiler-redeclared-symbol-chain-hang.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // bun-fuzz: every re-declaration of a name that merges with the previous one | ||
| // (`enum E {} enum E {}`, top-level `var x; var x;`) links the old symbol to | ||
| // the new one, so n re-declarations form a link chain of length n. The parser | ||
| // walked that chain from the start for every declaration without shortening | ||
| // it, which made n merged `enum` blocks (transpile) and n top-level `var` | ||
| // re-declarations (bundle) O(n^2): 720 KB of `enum E{…}` blocks took 14 s. | ||
| // | ||
| // Each case is timed against the same number of distinctly-named declarations, | ||
| // which does the same work minus the chain, so the check does not depend on | ||
| // how fast the machine is. | ||
| import { expect, test } from "bun:test"; | ||
| import { tempDir } from "harness"; | ||
| import { join } from "node:path"; | ||
|
|
||
| /** | ||
| * Best-of timing. Repeats only while a run is cheap, so a release build takes | ||
| * several samples and a debug build takes one. | ||
| */ | ||
| async function bench<T>(run: () => T | Promise<T>): Promise<{ ms: number; result: T }> { | ||
| let ms = Infinity; | ||
| let spent = 0; | ||
| let result!: T; | ||
| for (let i = 0; i < 5 && spent < 400; i++) { | ||
| const start = performance.now(); | ||
| result = await run(); | ||
| const elapsed = performance.now() - start; | ||
| ms = Math.min(ms, elapsed); | ||
| spent += elapsed; | ||
| } | ||
| return { ms, result }; | ||
| } | ||
|
|
||
| /** | ||
| * `prefix + 0 + suffix + prefix + 1 + suffix + …`. join() formats the numbers | ||
| * natively; building 16k strings in a JS loop takes half a second in a debug | ||
| * build. | ||
| */ | ||
| function numbered(n: number, prefix: string, suffix: string): string { | ||
| return prefix + [...Array(n).keys()].join(suffix + prefix) + suffix; | ||
| } | ||
|
|
||
| test("merged enum declarations transpile in linear time", async () => { | ||
| const n = 16_384; | ||
| const merged = Buffer.alloc(n * 9, "enum E{}\n").toString(); | ||
| const distinct = numbered(n, "enum E", "{}\n"); | ||
| const transpiler = new Bun.Transpiler({ loader: "ts" }); | ||
| transpiler.transformSync("enum Warmup {}"); | ||
|
|
||
| const baseline = await bench(() => transpiler.transformSync(distinct)); | ||
| const { ms, result: out } = await bench(() => transpiler.transformSync(merged)); | ||
|
|
||
| // One `var E;` for the whole chain, then one closure per block. | ||
| expect(out).toStartWith("var E;\n((E) => {})(E ||= {});\n((E) => {})(E ||= {});\n"); | ||
| expect(out.indexOf("var E", 1)).toBe(-1); | ||
| expect(out.length).toBe("var E;\n".length + n * "((E) => {})(E ||= {});\n".length); | ||
|
|
||
| // ~0.8x with the fix; 10x (debug) to 30x (release) before it. | ||
| expect(ms / baseline.ms).toBeLessThan(3); | ||
| }); | ||
|
|
||
| test("top-level var re-declarations bundle in linear time", async () => { | ||
| const n = 16_384; | ||
| using dir = tempDir("redeclared-var-chain", { | ||
| "same.ts": Buffer.alloc(n * 11, "var x = 1;\n").toString() + "export { x };\n", | ||
| "distinct.ts": numbered(n, "var x", " = 1;\n") + "export { x0 };\n", | ||
| }); | ||
| const build = async (file: string) => { | ||
| const result = await Bun.build({ entrypoints: [join(String(dir), file)] }); | ||
| expect(result.success).toBeTrue(); | ||
| return result.outputs[0].text(); | ||
| }; | ||
|
|
||
| const baseline = await bench(() => build("distinct.ts")); | ||
| const { ms, result: out } = await bench(() => build("same.ts")); | ||
|
|
||
| expect(out).toContain("var x = 1;\nvar x = 1;\n"); | ||
| expect(out).toContain("export {\n x\n};"); | ||
|
|
||
| // ~1x with the fix; 10x (debug) to 20x (release) before it. | ||
| expect(ms / baseline.ms).toBeLessThan(4); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.