Repository navigation
Check only names that can newly collide when appending modules to the bundle - #92
Merged
Bobris merged 1 commit intoOct 27, 2026
Merged
Conversation
… bundle AppendToplevelWithRename walked all accumulated top level variables of the bundle for every appended module, which is quadratic in the module count (~22k variables x 2650 modules, ~11 s of a ~40 s build without build cache). Only names added since the previous append can newly collide, so walk just those. Output of the nsi-bobwai build is byte identical. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
BundlerHelpers.AppendToplevelWithRenameis quadratic in the number of modules. For every module appended to the bundle it copies the growingnonRootSymbolNamesset and then walks all top level variables accumulated so far (ToplevelRenameWalker.Walk(main)). In a large project (phoenixnsi-bobwai, 2650 modules, ~22,500 accumulated variables on average) that is ~60 M name checks.It is invisible in the log, the
Bundler phaselines cover only Parse, Compress, Mangle and Print, but it sits between Parse and Compress.Measured on 7.0.5 with a stopwatch probe around the three steps: walk of
main10.4-11.0 s, set copy 0.45 s, walk of the added module 0.2 s. Adotnet-tracesampled-thread-time profile agrees (ToplevelRenameWalker.Visitself time ~9 s plus ~2 s waiting for GC). The step also runs on builds where every source comes from the build cache, so those builds do not avoid it.Change
After each append no top level variable of
maincollides with a non-root name, so only names that can newly collide need another check:ToplevelRenameWalkergets an optionalonlyNamesfilter, and the first append still walks everything. The previous-append state is kept in aConditionalWeakTablekeyed by the bundle toplevel.Results
phoenix
nsi-bobwai,bb b --nocache, Windows, three interleaved runs each:mainBuild doneThe stock build is ReadyToRun and the patched one was published without it, so the real gain is at least that.
diff -rq), not onlya.js.Njsast.TestBundler tests: 90 of 90 pass.Njsast.Testhas 166 failures (TypeScript and JSX oracle tests,EsmToCjs) that are identical with and without this change.Please review
The correctness argument is the invariant above plus identical output on one large project. The exclusion sets (
add.Globals,knownDeclaredGlobals) differ per append, which makes the edge cases subtle, so a review of whether the candidate set is complete would be appreciated. I did not add a dedicated unit test for the exclusion edge cases.Not included, but visible in the same profile: the per-module
ToHashSet()copy (~0.5 s) could be a predicate over both sets, and aBundler phase Append took ...log line would make this step visible.