Skip to content

Commit bd1e383

Browse files
perf(javascript): inline single-use functions and literals, join string literals in the one pass
A function declaration read once before it prints at that read, the walk printing it first; an object or array literal of constants folds into its one read anywhere in the next statement; literals added to a string join; and an operation folded into a read prints without parentheses it needs not. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent f2b0c68 commit bd1e383

6 files changed

Lines changed: 357 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Inline single-use functions, literals and string joins in the one-pass minifier.

‎lib/javascript/ScopeAnalyzer.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4723,7 +4723,11 @@ class MinifierTreeScopeAnalyzer {
47234723
((referenceFlags[i] & REFERENCE_CALLEE) === 0 ||
47244724
this.types[init] === NodeType.ArrowFunctionExpression ||
47254725
definition.kind === DECLARES_VAR)) ||
4726-
tree.start(node) < tree.end(init) ||
4726+
(this.types[declarator] === NodeType.FunctionDeclaration
4727+
? // Hoisted, it is made before any read but its own.
4728+
tree.start(node) >= tree.start(init) &&
4729+
tree.end(node) <= tree.end(init)
4730+
: tree.start(node) < tree.end(init)) ||
47274731
// Only a read the statements listing it reach runs after it.
47284732
tree.end(node) >
47294733
bindings[/** @type {number} */ (lists.get(declarator)) + 2] ||

‎lib/javascript/syntax-parser.js‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20964,6 +20964,12 @@ let walkWriter;
2096420964
// By `NodeType`, 1 for the nodes the printer prints inside their parent's text.
2096520965
/** @type {Uint8Array | undefined} */
2096620966
let walkInlined;
20967+
// By node, one its printer reads before the walk reaches it, printed first.
20968+
/** @type {Map<number, number> | undefined} */
20969+
let walkEarly;
20970+
// The nodes printed ahead of their place, which the walk skips there.
20971+
/** @type {Set<number> | undefined} */
20972+
let walkPrintedEarly;
2096720973
// Each node type's child fields, the order they are written in: the fields
2096820974
// holding a node or a list of them, built on first use.
2096920975
/** @type {number[][] | undefined} */
@@ -21050,9 +21056,14 @@ const printNodeTree = (tree, node) => {
2105021056
const count = tree.fieldCount(fields[f], node);
2105121057
for (let i = 0; i < count; i++) {
2105221058
const child = tree.field(i, fields[f], node);
21059+
if (child === 0) continue;
21060+
if (walkEarly !== undefined) {
21061+
const early = walkEarly.get(child);
21062+
if (early !== undefined) printEarly(tree, early);
21063+
}
2105321064
if (
21054-
child !== 0 &&
21055-
(walkInlined === undefined || walkInlined[tree.type(child)] === 0)
21065+
(walkInlined === undefined || walkInlined[tree.type(child)] === 0) &&
21066+
(walkPrintedEarly === undefined || !walkPrintedEarly.has(child))
2105621067
) {
2105721068
printNodeTree(tree, child);
2105821069
}
@@ -21064,6 +21075,29 @@ const printNodeTree = (tree, node) => {
2106421075
/** @type {JavascriptPrintContext} */ (walkWriter).printNode(node, A);
2106521076
};
2106621077

21078+
/**
21079+
* Prints a node ahead of its place, below the innermost node of the walk's
21080+
* path holding it: its parent, as a node read before it is in its list.
21081+
* @param {SyntaxTree} tree the tree
21082+
* @param {number} node the node
21083+
* @returns {void}
21084+
*/
21085+
const printEarly = (tree, node) => {
21086+
let depth = walkParents.length;
21087+
while (
21088+
depth > 0 &&
21089+
(tree.start(walkParents[depth - 1]) > tree.start(node) ||
21090+
tree.end(walkParents[depth - 1]) < tree.end(node))
21091+
) {
21092+
depth--;
21093+
}
21094+
const below = walkParents.splice(depth);
21095+
printNodeTree(tree, node);
21096+
for (let i = 0; i < below.length; i++) walkParents.push(below[i]);
21097+
if (walkPrintedEarly === undefined) walkPrintedEarly = new Set();
21098+
walkPrintedEarly.add(node);
21099+
};
21100+
2106721101
/**
2106821102
* Walks a node and everything under it, its visitors' `enter` before its
2106921103
* children and `exit` after, and where the run prints, its printer last.
@@ -21603,6 +21637,8 @@ const walkSyntaxTree = (tree, program, input, visitors, writer, inlined) => {
2160321637
walkVisitors = [];
2160421638
walkWriter = undefined;
2160521639
walkInlined = undefined;
21640+
walkEarly = undefined;
21641+
walkPrintedEarly = undefined;
2160621642
walkHalted = null;
2160721643
walkLineStarts = null;
2160821644
}
@@ -21683,3 +21719,10 @@ module.exports.A = A;
2168321719
module.exports.grammar = grammar;
2168421720
module.exports.isWalkingSyntaxTree = isWalkingSyntaxTree;
2168521721
module.exports.walkSyntaxTree = walkSyntaxTree;
21722+
/**
21723+
* @param {Map<number, number>} early by node, one its printer reads before the walk reaches it
21724+
* @returns {void}
21725+
*/
21726+
module.exports.printEarlyIn = (early) => {
21727+
walkEarly = early;
21728+
};

0 commit comments

Comments
 (0)