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;