Skip to content

Check only names that can newly collide when appending modules to the bundle - #92

Merged
Bobris merged 1 commit into
bobril:masterfrom
sedlakr:perf/bundler-incremental-main-rename
Oct 27, 2026
Merged

Bobris merged 1 commit into
bobril:masterfrom
sedlakr:perf/bundler-incremental-main-rename

Conversation

@sedlakr

@sedlakr sedlakr commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Problem

BundlerHelpers.AppendToplevelWithRename is quadratic in the number of modules. For every module appended to the bundle it copies the growing nonRootSymbolNames set and then walks all top level variables accumulated so far (ToplevelRenameWalker.Walk(main)). In a large project (phoenix nsi-bobwai, 2650 modules, ~22,500 accumulated variables on average) that is ~60 M name checks.

It is invisible in the log, the Bundler phase lines 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 main 10.4-11.0 s, set copy 0.45 s, walk of the added module 0.2 s. A dotnet-trace sampled-thread-time profile agrees (ToplevelRenameWalker.Visit self 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 main collides with a non-root name, so only names that can newly collide need another check:

  • the variable names added by the previous append,
  • the globals of the previous added module (they were excluded from the name set during that walk, but may not be now),
  • the non-root names of the module being added.

ToplevelRenameWalker gets an optional onlyNames filter, and the first append still walks everything. The previous-append state is kept in a ConditionalWeakTable keyed by the bundle toplevel.

Results

phoenix nsi-bobwai, bb b --nocache, Windows, three interleaved runs each:

before after
walk of main ~11-12 s 0.12 s
Build done 37.7 / 39.9 / 42.8 s 27.6 / 27.8 / 30.7 s

The stock build is ReadyToRun and the patched one was published without it, so the real gain is at least that.

  • Output directory is byte-identical (diff -rq), not only a.js.
  • Njsast.Test Bundler tests: 90 of 90 pass.
  • The rest of Njsast.Test has 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 a Bundler phase Append took ... log line would make this step visible.

… 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>
@Bobris
Bobris merged commit d2fe5e6 into bobril:master Oct 27, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants