Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion Njsast/Bundler/BundlerHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public static SourceFile BuildSourceFile(string name, string content, SourceMap.
}
}

sealed class AppendState { public List<string> PrevAddedNames = []; public List<string> PrevGlobals = []; public bool Seen; }
static readonly System.Runtime.CompilerServices.ConditionalWeakTable<AstToplevel, AppendState> AppendStates = new();
public static void AppendToplevelWithRename(AstToplevel main, AstToplevel add, string suffix, HashSet<string> knownDeclaredGlobals,
Func<AstToplevel, AstToplevel>? beforeAdd = null)
{
Expand All @@ -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<string, SymbolDef>(), 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<string>? candidates = state.Seen ? [..state.PrevAddedNames, ..state.PrevGlobals, ..add.NonRootSymbolNames!] : null;
state.Seen = true;
renameWalker = new(new Dictionary<string, SymbolDef>(), 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!)
Expand Down
19 changes: 16 additions & 3 deletions Njsast/Bundler/ToplevelRenameWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,28 @@ class ToplevelRenameWalker : TreeWalker
readonly IDictionary<string, SymbolDef> _variables;
readonly HashSet<string> _nonRootSymbolNames;
readonly string _suffix;
readonly HashSet<string>? _onlyNames;

public ToplevelRenameWalker(IDictionary<string, SymbolDef> variables,
HashSet<string> nonRootSymbolNames, string suffix)
HashSet<string> nonRootSymbolNames, string suffix, HashSet<string>? onlyNames = null)
{
_onlyNames = onlyNames;
_variables = variables;
_nonRootSymbolNames = nonRootSymbolNames;
_suffix = "_" + suffix;
}

IEnumerable<KeyValuePair<string, SymbolDef>> Candidates(AstToplevel scope)
{
if (_onlyNames == null) return scope.Variables!;
List<KeyValuePair<string, SymbolDef>> 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();
Expand All @@ -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))
{
Expand All @@ -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;
Expand Down
Loading