Skip to content

⚡️ perf(js): reuse the scope analysis for mangling - #2485

Merged
gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:perf/js-reuse-analysis
Nov 18, 2026
Merged

gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:perf/js-reuse-analysis

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

With both folding and mangling on, the JavaScript minifier resolved every scope and binding to reach the compress fixpoint, then discarded that resolution and computed it again before renaming.

The compress pass now hands its settled resolution to the mangler when its last pass changed nothing, and the mangler analyzes afresh on every other exit as before. The node arena is also presized from the source length, capped so the size can't overflow.

Release build, callgrind instructions per call, reusing the resolution: underscore 1.13 (67 kB) 13,560,368 → 12,011,842 (−11%); 320 expression statements 1,268,004 → 1,175,553 (−7%); 128 call initializers in one declaration 1,173,365 → 974,420 (−17%). The presize then takes another 1.1–1.9% off each row (measured with glibc.malloc.mmap_threshold=131072 because valgrind's brk limit trips on the underscore row otherwise).

A differential over 36,600 programs (tests/js corpora, real .js files, generated scope-heavy code with eval, with, labels and shadowed globals) under every fold/mangle combination, as script and module, matches main; valgrind memcheck reports no errors or leaks.

Once a compress pass returns 0 the tree has settled, and the driver
then mangled it, which resolved the same tree again from scratch. The
final pass now hands its resolution to jm_mangle, which takes it over
instead of analyzing again. Nothing touches the tree or the analysis
between the two: the driver goes straight from the fixpoint to the
mangle. A pass that changes the tree keeps no resolution, and the
mangle resolves afresh as before.

Callgrind instructions per call, upstream/main to this commit:

- underscore 1.13 (67 kB): 13,560,368 to 12,200,628 (-10%)
- 320 expression statements: 1,268,004 to 1,175,674 (-7%)
- 128 call initializers / one declaration: 1,173,365 to 992,747 (-15%)

The differential corpus of 20,816 JavaScript inputs (the test corpus,
a seeded random generator and the bench cases) gives identical output
on this branch and on upstream/main.
The node arena started at 64 nodes and doubled, copying every node at
each doubling, so a large script paid for its arena many times over.
jm_program_new now takes the source length and allocates a node per
four code points up front, the same guess the CSS tokenizer makes. The
guess is capped at what an int32_t node index and a size_t byte count
can hold, so a huge source cannot wrap the allocation; past the cap the
arena still grows on demand. The output does not change.

Callgrind instructions per call, after the analysis-reuse commit:

- underscore 1.13 (67 kB): 12,011,842 to 11,817,647 (-1.6%)
- 320 expression statements: 1,175,553 to 1,163,166 (-1.1%)
- 128 call initializers / one declaration: 974,420 to 955,641 (-1.9%)
@gaborbernat gaborbernat added the enhancement New feature or request label Nov 18, 2026
@gaborbernat
gaborbernat force-pushed the perf/js-reuse-analysis branch from 91a79a4 to 1b27434 Compare November 18, 2026 18:21
@codspeed

codspeed Bot commented Nov 18, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 11.19%

1 warning

⚠️ 22 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 22 improved benchmarks
✅ 558 untouched benchmarks
⏩ 91 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_feature[minify-js-names] 1.6 ms 1.3 ms +20.83%
⚡ test_feature[minify-js-names-single-character] 1,196.4 µs 991.6 µs +20.65%
⚡ test_feature[minify-js-names-multiple-characters] 1.4 ms 1.2 ms +20.43%
⚡ test_feature[minify-js-var-initialization-early] 3.7 ms 3.1 ms +19.21%
⚡ test_feature[minify-js-single-use] 2.2 ms 1.9 ms +16.9%
⚡ test_feature[minify-js-single-use-single] 1,107.6 µs 955.2 µs +15.95%
⚡ test_feature[minify-js-single-use-separated] 2.5 ms 2.1 ms +15.71%
⚡ test_feature[minify-js-unused-declarations] 2.9 ms 2.6 ms +10.33%
⚡ test_feature[minify-js-unused-declarations-single] 1,089.3 µs 989.1 µs +10.13%
⚡ test_feature[minify-js-var-initialization] 3.9 ms 3.5 ms +9.95%
⚡ test_feature[minify-js-var-initialization-single] 1.2 ms 1.1 ms +9.16%
⚡ test_feature[minify-js] 14 ms 12.8 ms +9.05%
⚡ test_feature[minify-js-unlink] 4.5 ms 4.1 ms +8.82%
⚡ test_feature[minify-js-unlink-separated] 4.8 ms 4.5 ms +8.25%
⚡ test_feature[minify-js-unlink-single] 1.1 ms 1 ms +7.92%
⚡ test_feature[minify-js-jquery] 57.6 ms 53.5 ms +7.73%
⚡ test_feature[minify-js-guards-single] 1.5 ms 1.4 ms +6.95%
⚡ test_feature[minify-js-guards] 4.8 ms 4.4 ms +6.86%
⚡ test_feature[minify-js-sequences-small] 1.3 ms 1.3 ms +6.41%
⚡ test_feature[minify-js-sequences] 4.1 ms 3.8 ms +6.18%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:perf/js-reuse-analysis (1b27434) with main (dd58ccd)

Open in CodSpeed

Footnotes

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

@gaborbernat
gaborbernat merged commit 8876d20 into tox-dev:main Nov 18, 2026
30 of 49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant