From 672198a60258f6e19f9f3bc064eee7f41e371cca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radek=20Sedl=C3=A1k?= Date: Tue, 27 Oct 2026 14:07:16 +0100 Subject: [PATCH] Check only names that can newly collide when appending modules to the 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 --- CHANGELOG.md | 6 ++++++ Njsast/Bundler/BundlerHelpers.cs | 11 ++++++++++- Njsast/Bundler/ToplevelRenameWalker.cs | 19 ++++++++++++++++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f98d02e..036c1e33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## [unreleased] +### Changed + +- Faster bundling of large projects. When appending each module to the bundle, the names of the already merged top level + variables are no longer all re-checked for collisions, only those that can newly collide. In a project with ~2650 + modules this step went from ~11 s to ~0.1 s of a ~40 s build without build cache, output is byte identical. + ## 7.0.5 ### Changed diff --git a/Njsast/Bundler/BundlerHelpers.cs b/Njsast/Bundler/BundlerHelpers.cs index d087e22d..343c8c8c 100644 --- a/Njsast/Bundler/BundlerHelpers.cs +++ b/Njsast/Bundler/BundlerHelpers.cs @@ -80,6 +80,8 @@ public static SourceFile BuildSourceFile(string name, string content, SourceMap. } } + sealed class AppendState { public List PrevAddedNames = []; public List PrevGlobals = []; public bool Seen; } + static readonly System.Runtime.CompilerServices.ConditionalWeakTable AppendStates = new(); public static void AppendToplevelWithRename(AstToplevel main, AstToplevel add, string suffix, HashSet knownDeclaredGlobals, Func? beforeAdd = null) { @@ -100,14 +102,21 @@ public static void AppendToplevelWithRename(AstToplevel main, AstToplevel add, s nonRootSymbolNames = nonRootSymbolNames.ToHashSet(); nonRootSymbolNames.ExceptWith(add.Globals!.Keys); nonRootSymbolNames.ExceptWith(knownDeclaredGlobals); - renameWalker = new(new Dictionary(), nonRootSymbolNames, "m"); + var state = AppendStates.GetOrCreateValue(main); + // only names that can newly collide need a re-check, everything else was already checked against the same name sets + HashSet? candidates = state.Seen ? [..state.PrevAddedNames, ..state.PrevGlobals, ..add.NonRootSymbolNames!] : null; + state.Seen = true; + renameWalker = new(new Dictionary(), nonRootSymbolNames, "m", candidates); renameWalker.Walk(main); + state.PrevGlobals = [..add.Globals.Keys]; add = beforeAdd?.Invoke(add) ?? add; main.Body.AddRange(add.Body.AsReadOnlySpan()); + state.PrevAddedNames = []; foreach (var (_, symbolDef) in add.Variables!) { main.Variables!.TryAdd(symbolDef.Name, symbolDef); + state.PrevAddedNames.Add(symbolDef.Name); } foreach (var (_, symbolDef) in add.Globals!) diff --git a/Njsast/Bundler/ToplevelRenameWalker.cs b/Njsast/Bundler/ToplevelRenameWalker.cs index e6490528..2e9730d0 100644 --- a/Njsast/Bundler/ToplevelRenameWalker.cs +++ b/Njsast/Bundler/ToplevelRenameWalker.cs @@ -8,15 +8,28 @@ class ToplevelRenameWalker : TreeWalker readonly IDictionary _variables; readonly HashSet _nonRootSymbolNames; readonly string _suffix; + readonly HashSet? _onlyNames; public ToplevelRenameWalker(IDictionary variables, - HashSet nonRootSymbolNames, string suffix) + HashSet nonRootSymbolNames, string suffix, HashSet? onlyNames = null) { + _onlyNames = onlyNames; _variables = variables; _nonRootSymbolNames = nonRootSymbolNames; _suffix = "_" + suffix; } + IEnumerable> Candidates(AstToplevel scope) + { + if (_onlyNames == null) return scope.Variables!; + List> res = []; + foreach (var name in _onlyNames) + { + if (scope.Variables!.TryGetValue(name, out var symbol)) res.Add(new(name, symbol)); + } + return res; + } + protected override void Visit(AstNode node) { StopDescending(); @@ -25,7 +38,7 @@ protected override void Visit(AstNode node) return; } var wasRename = false; - foreach (var (name, symbol) in scope.Variables!) + foreach (var (name, symbol) in Candidates(scope)) { if (!_nonRootSymbolNames.Contains(name) && !_variables.ContainsKey(name)) { @@ -39,7 +52,7 @@ protected override void Visit(AstNode node) index++; newName = name + _suffix; if (index > 1) newName += index.ToString(); - } while (_nonRootSymbolNames.Contains(newName) || _variables.ContainsKey(newName) || scope.Variables.ContainsKey(newName)); + } while (_nonRootSymbolNames.Contains(newName) || _variables.ContainsKey(newName) || scope.Variables!.ContainsKey(newName)); Helpers.RenameSymbol(symbol, newName); _variables[newName] = symbol;