Skip to content

Commit fa15f02

Browse files
perf(javascript): index a scope's function calls once for copiesEveryCall (#22637)
* perf(javascript): index a scope's function calls once for copiesEveryCall Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC * test(javascript): cover a constructed function in copiesEveryCall Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent 13ff092 commit fa15f02

3 files changed

Lines changed: 79 additions & 32 deletions

File tree

‎.changeset/046-printer-parse-peak-memory.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Cut the peak memory of minifying through webpack's printer.
5+
Cut the peak memory and time of minifying through webpack's printer.

‎lib/javascript/syntax-printer.js‎

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21250,6 +21250,68 @@ const installImprove = (modules) => {
2125021250
// Their right side may not run, so an argument read there may not either.
2125121251
const LOGICAL_OPERATORS = new Set(["&&", "||", "??"]);
2125221252

21253+
/**
21254+
* @typedef {object} ScopeCall
21255+
* @property {Node} call a call of a function the scope declares
21256+
* @property {Node[]} ancestors the nodes around it, the scope's node first
21257+
*/
21258+
21259+
/**
21260+
* @typedef {object} ScopeFunctionCalls
21261+
* @property {number} references how many references the function had when the scope was walked
21262+
* @property {ScopeCall[]} calls its calls in the scope's node
21263+
*/
21264+
21265+
/** @type {WeakMap<Scope, Map<SymbolDefinition, ScopeFunctionCalls>>} */
21266+
const callsByScope = new WeakMap();
21267+
21268+
/**
21269+
* The calls in a scope's node of a function it declares, from one walk of
21270+
* the scope shared by its functions, walked again once a reference is added.
21271+
* @param {SymbolDefinition} definition the function
21272+
* @returns {ScopeCall[]} its calls
21273+
*/
21274+
const callsOf = (definition) => {
21275+
const scope = definition.scope;
21276+
const recorded = callsByScope.get(scope);
21277+
const entry = recorded && recorded.get(definition);
21278+
// A copy made since the walk adds calls the record lacks.
21279+
if (entry && entry.references === definition.references.length) {
21280+
return entry.calls;
21281+
}
21282+
/** @type {Map<SymbolDefinition, ScopeFunctionCalls>} */
21283+
const found = new Map([
21284+
[definition, { references: definition.references.length, calls: [] }]
21285+
]);
21286+
A.walk_parent(
21287+
scope.node,
21288+
(
21289+
/** @type {Node} */ node,
21290+
/** @type {{ parent: (n?: number) => Node | undefined }} */ parents
21291+
) => {
21292+
if (!A.isSymbolRefNode(node)) return;
21293+
const target = node.definition;
21294+
if (!target || target.scope !== scope) return;
21295+
let calls = found.get(target);
21296+
if (calls === undefined) {
21297+
calls = { references: target.references.length, calls: [] };
21298+
found.set(target, calls);
21299+
}
21300+
const call = parents.parent();
21301+
if (!call || !A.isCallNode(call) || call.callee !== node) return;
21302+
/** @type {Node[]} */
21303+
const ancestors = [];
21304+
for (let i = 1, parent; (parent = parents.parent(i)); i++) {
21305+
ancestors.push(parent);
21306+
}
21307+
ancestors.reverse();
21308+
calls.calls.push({ call, ancestors });
21309+
}
21310+
);
21311+
callsByScope.set(scope, found);
21312+
return /** @type {ScopeFunctionCalls} */ (found.get(definition)).calls;
21313+
};
21314+
2125321315
/**
2125421316
* Whether every read of a function is a call a copy can replace, so the
2125521317
* declaration goes: a copy left beside it only adds to the program.
@@ -21260,36 +21322,20 @@ const installImprove = (modules) => {
2126021322
const copiesEveryCall = (definition, copies) => {
2126121323
let result = everyCallCopied.get(definition);
2126221324
if (result === undefined) {
21263-
let calls = 0;
21264-
result = true;
21265-
A.walk_parent(
21266-
definition.scope.node,
21267-
(
21268-
/** @type {Node} */ node,
21269-
/** @type {{ parent: (n?: number) => Node | undefined }} */ parents
21270-
) => {
21271-
if (
21272-
!result ||
21273-
!A.isCallNode(node) ||
21274-
!A.isSymbolRefNode(node.callee) ||
21275-
node.callee.definition !== definition
21276-
) {
21277-
return result ? undefined : A.walk_abort;
21278-
}
21279-
calls++;
21280-
if (
21281-
A.isNewNode(node) ||
21282-
!copies(
21283-
node,
21284-
/** @type {CompressorShape} */ (/** @type {unknown} */ (parents))
21285-
)
21286-
) {
21287-
result = false;
21288-
return A.walk_abort;
21289-
}
21290-
}
21291-
);
21292-
result = result && calls === definition.references.length;
21325+
const calls = callsOf(definition);
21326+
result =
21327+
calls.length === definition.references.length &&
21328+
calls.every(({ call, ancestors }) => {
21329+
if (A.isNewNode(call)) return false;
21330+
/** @type {{ parent: (n?: number) => Node | undefined }} */
21331+
const parents = {
21332+
parent: (n = 0) => ancestors[ancestors.length - 1 - n]
21333+
};
21334+
return copies(
21335+
call,
21336+
/** @type {CompressorShape} */ (/** @type {unknown} */ (parents))
21337+
);
21338+
});
2129321339
everyCallCopied.set(definition, result);
2129421340
}
2129521341
return result;

‎test/unitCases/syntax-printer.unittest.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,8 @@ const KEPT_CASES = [
13361336
["a function incrementing a parameter, passed the variable of its name", "!function () { function show(props, name) { props.seen = name++; props.next = name; } function create(props, name, flag) { flag && show(props, name); return [props, name]; } console.log(create({}, \"a\", 1)[0].next, create({}, 1, 1)[0].next); }();"],
13371337
["a function writing a parameter by a pattern, passed the variable of its name", "!function () { function show(props, name) { [name] = [name + \"!\"]; props.seen = name; } function create(props, name, flag) { flag && show(props, name); return [props, name]; } console.log(create({}, \"a\", 1)[0].seen, create({}, \"b\", 0)[0].seen); }();"],
13381338
["a function passed variables of its parameters' names, of a function reading `arguments` or `eval`", "!function () { function show(props, name) { props.seen = name; } function args(props, name, flag) { flag && show(props, name); return [props, name, arguments.length]; } function evaluates(props, name, flag) { flag && show(props, name); return [props, eval(\"name\")]; } console.log(args({}, \"a\", 1)[0].seen, evaluates({}, \"b\", 1)[0].seen); }();"],
1339-
["a function passed a global of its parameter's name", "var name = \"g\"; !function () { function show(props, name) { props.seen = name; } function create(props, flag) { flag && show(props, name); return props; } console.log(create({}, 1).seen, create({}, 0).seen); }();"]
1339+
["a function passed a global of its parameter's name", "var name = \"g\"; !function () { function show(props, name) { props.seen = name; } function create(props, flag) { flag && show(props, name); return props; } console.log(create({}, 1).seen, create({}, 0).seen); }();"],
1340+
["a function constructed as well as called, its calls not copied", "!function () { var total = Number(process.argv.length > 99); function add(n) { total += n; } add(1); add(2); new add(3); console.log(total); }();"]
13401341
];
13411342

13421343
// An `arguments` read terser already writes right: no parameter it aliases is

0 commit comments

Comments
 (0)