Skip to content

Commit 2fa104f

Browse files
committed
avoid walking scopes multiple times for performance reasons
1 parent 33166f2 commit 2fa104f

2 files changed

Lines changed: 74 additions & 18 deletions

File tree

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,29 @@ const getFinalName = (
168168
}
169169
};
170170

171-
const getSymbolsFromScope = (s, untilScope) => {
172-
const allUsedNames = new Set();
171+
const addScopeSymbols1 = (s, nameSet, scopeSet) => {
173172
let scope = s;
174173
while (scope) {
174+
if (scopeSet.has(scope)) break;
175+
scopeSet.add(scope);
175176
for (const variable of scope.variables) {
176-
allUsedNames.add(variable.name);
177+
nameSet.add(variable.name);
178+
}
179+
scope = scope.upper;
180+
}
181+
};
182+
183+
const addScopeSymbols2 = (s, nameSet, scopeSet1, scopeSet2) => {
184+
let scope = s;
185+
while (scope) {
186+
if (scopeSet1.has(scope)) break;
187+
if (scopeSet2.has(scope)) break;
188+
scopeSet1.add(scope);
189+
for (const variable of scope.variables) {
190+
nameSet.add(variable.name);
177191
}
178-
if (untilScope === scope) break;
179192
scope = scope.upper;
180193
}
181-
return allUsedNames;
182194
};
183195

184196
const getAllReferences = variable => {
@@ -196,11 +208,6 @@ const getAllReferences = variable => {
196208
return set;
197209
};
198210

199-
const reduceSet = (a, b) => {
200-
for (const item of b) a.add(item);
201-
return a;
202-
};
203-
204211
const getPathInAst = (ast, node) => {
205212
if (ast === node) {
206213
return [];
@@ -816,8 +823,31 @@ class ConcatenatedModule extends Module {
816823
"onsubmit"
817824
]);
818825

826+
// Set of already checked scopes
827+
const alreadyCheckedScopes = new Set();
828+
819829
// get all global names
820830
for (const info of modulesWithInfo) {
831+
const superClassExpressions = [];
832+
833+
// ignore symbols from moduleScope
834+
if (info.moduleScope) {
835+
alreadyCheckedScopes.add(info.moduleScope);
836+
837+
// The super class expression in class scopes behaves weird
838+
// We store ranges of all super class expressions to make
839+
// renaming to work correctly
840+
for (const childScope of info.moduleScope.childScopes) {
841+
if (childScope.type !== "class") continue;
842+
if (!childScope.block.superClass) continue;
843+
superClassExpressions.push({
844+
range: childScope.block.superClass.range,
845+
variables: childScope.variables
846+
});
847+
}
848+
}
849+
850+
// add global symbols
821851
if (info.globalScope) {
822852
for (const reference of info.globalScope.through) {
823853
const name = reference.identifier.name;
@@ -826,12 +856,21 @@ class ConcatenatedModule extends Module {
826856
name
827857
)
828858
) {
829-
for (const s of getSymbolsFromScope(
830-
reference.from,
831-
info.moduleScope
832-
)) {
833-
allUsedNames.add(s);
859+
for (const expr of superClassExpressions) {
860+
if (
861+
expr.range[0] <= reference.identifier.range[0] &&
862+
expr.range[1] >= reference.identifier.range[1]
863+
) {
864+
for (const variable of expr.variables) {
865+
allUsedNames.add(variable.name);
866+
}
867+
}
834868
}
869+
addScopeSymbols1(
870+
reference.from,
871+
allUsedNames,
872+
alreadyCheckedScopes
873+
);
835874
} else {
836875
allUsedNames.add(name);
837876
}
@@ -856,9 +895,16 @@ class ConcatenatedModule extends Module {
856895
const name = variable.name;
857896
if (allUsedNames.has(name)) {
858897
const references = getAllReferences(variable);
859-
const symbolsInReferences = references
860-
.map(ref => getSymbolsFromScope(ref.from, info.moduleScope))
861-
.reduce(reduceSet, new Set());
898+
const symbolsInReferences = new Set();
899+
const alreadyCheckedInnerScopes = new Set();
900+
for (const ref of references) {
901+
addScopeSymbols2(
902+
ref.from,
903+
symbolsInReferences,
904+
alreadyCheckedInnerScopes,
905+
alreadyCheckedScopes
906+
);
907+
}
862908
const newName = this.findNewName(
863909
name,
864910
allUsedNames,

‎test/cases/scope-hoisting/renaming-4967/file1.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,15 @@ export function test() {
66
function file1_js_a() {
77
return "fail";
88
}
9+
function file1_a() {
10+
return "fail";
11+
}
912
return a();
1013
}
14+
15+
function renaming_4967_file1_js_a() {
16+
return "fail";
17+
}
18+
function renaming_4967_file1_a() {
19+
return "fail";
20+
}

0 commit comments

Comments
 (0)