Repository navigation
⚡️ perf(js): reuse the scope analysis for mangling - #2485
Conversation
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%)
91a79a4 to
1b27434
Compare
Merging this PR will improve performance by 11.19%1 warning
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
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=131072because valgrind's brk limit trips on the underscore row otherwise).A differential over 36,600 programs (tests/js corpora, real
.jsfiles, 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.