Skip to content

Commit 5b08b3b

Browse files
authored
[acorn-opt] Use Set for reaches and names. NFC (#24498)
Those are already as sets semantically, this just makes it a bit clearer.
1 parent db82c22 commit 5b08b3b

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

tools/acorn-optimizer.mjs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ function JSDCE(ast, aggressive) {
266266
assert(id.type === 'Identifier');
267267
const curr = id.name;
268268
const value = node.init;
269-
const keep = !(curr in names) || (value && hasSideEffects(value));
269+
const keep = !names.has(curr) || (value && hasSideEffects(value));
270270
if (!keep) removedHere = 1;
271271
return keep;
272272
});
@@ -282,7 +282,7 @@ function JSDCE(ast, aggressive) {
282282
}
283283
},
284284
FunctionDeclaration(node, _c) {
285-
if (Object.prototype.hasOwnProperty.call(names, node.id.name)) {
285+
if (names.has(node.id.name)) {
286286
removed++;
287287
emptyOut(node);
288288
return;
@@ -329,7 +329,7 @@ function JSDCE(ast, aggressive) {
329329
// we can ignore self-references, i.e., references to ourselves inside
330330
// ourselves, for named defined (defun) functions
331331
const ownName = defun ? node.id.name : '';
332-
const names = {};
332+
const names = new Set();
333333
for (const name in scopes.pop()) {
334334
if (name === ownName) continue;
335335
const data = scope[name];
@@ -340,7 +340,7 @@ function JSDCE(ast, aggressive) {
340340
}
341341
if (data.def && !data.use && !data.param) {
342342
// this is eliminateable!
343-
names[name] = 0;
343+
names.add(name);
344344
}
345345
}
346346
cleanUp(node.body, names);
@@ -427,12 +427,12 @@ function JSDCE(ast, aggressive) {
427427
const scope = scopes.pop();
428428
assert(scopes.length === 0);
429429

430-
const names = {};
430+
const names = new Set();
431431
for (const [name, data] of Object.entries(scope)) {
432432
if (data.def && !data.use) {
433433
assert(!data.param); // can't be
434434
// this is eliminateable!
435-
names[name] = 0;
435+
names.add(name);
436436
}
437437
}
438438
cleanUp(ast, names);
@@ -641,7 +641,6 @@ function emitDCEGraph(ast) {
641641
const nameToGraphName = {};
642642
const modulePropertyToGraphName = {};
643643
const exportNameToGraphName = {}; // identical to wasmExports['..'] nameToGraphName
644-
const graph = [];
645644
let foundWasmImportsAssign = false;
646645
let foundMinimalRuntimeExports = false;
647646

@@ -821,18 +820,18 @@ function emitDCEGraph(ast) {
821820
const info = (infos[name] = {
822821
name: name,
823822
import: ['env', nativeName],
824-
reaches: {},
823+
reaches: new Set(),
825824
});
826825
if (nameToGraphName.hasOwnProperty(jsName)) {
827-
info.reaches[nameToGraphName[jsName]] = 1;
826+
info.reaches.add(nameToGraphName[jsName]);
828827
} // otherwise, it's a number, ignore
829828
}
830829
for (const [e, _] of Object.entries(exportNameToGraphName)) {
831830
const name = exportNameToGraphName[e];
832831
infos[name] = {
833832
name: name,
834833
export: e,
835-
reaches: {},
834+
reaches: new Set(),
836835
};
837836
}
838837
// a function that handles a node we visit, in either a defun or
@@ -867,7 +866,7 @@ function emitDCEGraph(ast) {
867866
if (reached) {
868867
function addReach(reached) {
869868
if (defunInfo) {
870-
defunInfo.reaches[reached] = 1; // defun reaches it
869+
defunInfo.reaches.add(reached); // defun reaches it
871870
} else {
872871
if (infos[reached]) {
873872
infos[reached].root = true; // in global scope, root it
@@ -891,26 +890,19 @@ function emitDCEGraph(ast) {
891890
const name = getGraphName(defun.id.name, 'defun');
892891
const info = (infos[name] = {
893892
name: name,
894-
reaches: {},
893+
reaches: new Set(),
895894
});
896895
fullWalk(defun.body, (node) => visitNode(node, info));
897896
});
898897
fullWalk(ast, (node) => visitNode(node, null));
899898
// Final work: print out the graph
900899
// sort for determinism
901-
function sortedNamesFromMap(map) {
902-
const names = [];
903-
for (const name of Object.keys(map)) {
904-
names.push(name);
905-
}
906-
names.sort();
907-
return names;
908-
}
909-
sortedNamesFromMap(infos).forEach((name) => {
910-
const info = infos[name];
911-
info.reaches = sortedNamesFromMap(info.reaches);
912-
graph.push(info);
913-
});
900+
const graph = Object.entries(infos)
901+
.sort(([name1], [name2]) => (name1 > name2 ? 1 : -1))
902+
.map(([_name, info]) => ({
903+
...info,
904+
reaches: Array.from(info.reaches).sort(),
905+
}));
914906
dump(graph);
915907
}
916908

0 commit comments

Comments
 (0)