Skip to content

Commit 7cf2177

Browse files
perf(javascript): hoist a chain's assignment and merge equal exits in the one pass
In the one pass, an `if` chain printing as `?:`, or a conditional, whose every branch assigns one local with one operator prints that assignment once, before its test (`t+=c?a:b`); a compound one only where no test runs code. `if`s next to each other that leave alike, by `break`, `continue` or `return` of nothing, print as one testing each. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent 55626e5 commit 7cf2177

3 files changed

Lines changed: 243 additions & 0 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+
Hoist a shared assignment out of a conditional chain and merge `if`s leaving alike in the one-pass JavaScript minifier.

‎lib/javascript/syntax-printer.js‎

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62693,6 +62693,17 @@ const createNodePrinter = (modules) => {
6269362693
const reducing = new Map();
6269462694
/** @type {Map<number, number>} */
6269562695
const reducedCalls = new Map();
62696+
// By `if` printing as `?:` or conditional, the assignment of one name
62697+
// with one operator all its branches end in, or null, found once.
62698+
/** @type {Map<number, [string, MinifierVariable] | null>} */
62699+
const hoists = new Map();
62700+
// Each `if` the `if` after it may merge with, both leaving the same way;
62701+
// by `if`, 1 where the next continues its test, 2 where it continues the
62702+
// test before it, 3 for both, terser's `if_return` of equal exits.
62703+
/** @type {number[]} */
62704+
const exitPairs = [];
62705+
/** @type {Uint8Array} */
62706+
let mergedExits = new Uint8Array(0);
6269662707
// By parameter of such a function, the name passed for it, read instead.
6269762708
/** @type {Map<TreeDefinition, number>} */
6269862709
const substituted = new Map();
@@ -63373,6 +63384,52 @@ const createNodePrinter = (modules) => {
6337363384
if (value !== 0) noteReduced(callee, call, value);
6337463385
};
6337563386

63387+
/**
63388+
* @param {number} statement a statement
63389+
* @returns {number} the `break`, `continue` or `return` of nothing it is, alone or in a block, as an `if` without `else` its body, 0 for none
63390+
*/
63391+
const bareExitOf = (statement) => {
63392+
if (
63393+
typeOf(statement) !== NodeType.IfStatement ||
63394+
tree.field(0, Field.alternate, statement) !== 0 ||
63395+
hasEdgeComments(statement)
63396+
) {
63397+
return 0;
63398+
}
63399+
let body = tree.field(0, Field.consequent, statement);
63400+
if (
63401+
typeOf(body) === NodeType.BlockStatement &&
63402+
tree.fieldCount(Field.body, body) === 1 &&
63403+
!holdsComments(body)
63404+
) {
63405+
body = tree.field(0, Field.body, body);
63406+
}
63407+
switch (typeOf(body)) {
63408+
case NodeType.BreakStatement:
63409+
case NodeType.ContinueStatement:
63410+
return tree.field(0, Field.label, body) === 0 && !hasEdgeComments(body)
63411+
? body
63412+
: 0;
63413+
case NodeType.ReturnStatement:
63414+
return tree.field(0, Field.argument, body) === 0 &&
63415+
!hasEdgeComments(body)
63416+
? body
63417+
: 0;
63418+
default:
63419+
return 0;
63420+
}
63421+
};
63422+
63423+
/**
63424+
* @param {number} statement a statement
63425+
* @param {number} next the statement after it
63426+
* @returns {boolean} whether both are `if`s leaving alike, which print as one testing either
63427+
*/
63428+
const sameExit = (statement, next) => {
63429+
const exit = bareExitOf(statement);
63430+
return exit !== 0 && typeOf(bareExitOf(next)) === typeOf(exit);
63431+
};
63432+
6337663433
/**
6337763434
* Notes a statement only setting a name with `=`, terser's `unused` of an
6337863435
* assignment: the name it sets counts as unread there, so a name only set
@@ -63423,6 +63480,7 @@ const createNodePrinter = (modules) => {
6342363480
if (i + 1 < count) {
6342463481
const next = tree.field(i + 1, Field.body, node);
6342563482
collapseInto(statement, next);
63483+
if (sameExit(statement, next)) exitPairs.push(statement, next);
6342663484
if (
6342763485
typeOf(statement) === NodeType.IfStatement &&
6342863486
tree.field(0, Field.alternate, statement) === 0 &&
@@ -64131,6 +64189,7 @@ const createNodePrinter = (modules) => {
6413164189
aliasesRead = new Uint8Array(size);
6413264190
keepsEffect = new Uint8Array(size);
6413364191
dropsWrite = new Uint8Array(size);
64192+
mergedExits = new Uint8Array(size);
6413464193
ifForms = new Uint8Array(size);
6413564194
if (treeAliases !== null) {
6413664195
for (const declarator of treeAliases) aliasesRead[declarator] = 1;
@@ -64156,6 +64215,7 @@ const createNodePrinter = (modules) => {
6415664215
reducing.clear();
6415764216
reducedCalls.clear();
6415864217
substituted.clear();
64218+
hoists.clear();
6415964219
callReads.clear();
6416064220
moveReads.clear();
6416164221
if (treeMoves !== null) {
@@ -64215,6 +64275,15 @@ const createNodePrinter = (modules) => {
6421564275
}
6421664276
}
6421764277
writeStatements.length = 0;
64278+
for (let i = 0; i < exitPairs.length; i += 2) {
64279+
const statement = exitPairs[i];
64280+
const next = exitPairs[i + 1];
64281+
if (dropped[statement] !== 1 && dropped[next] !== 1) {
64282+
mergedExits[statement] |= 1;
64283+
mergedExits[next] |= 2;
64284+
}
64285+
}
64286+
exitPairs.length = 0;
6421864287
// A constant whose declaration stays prints its name where it is read,
6421964288
// and a function moved to its read stays where it is.
6422064289
for (const [definition, declarator] of inlined) {
@@ -64417,6 +64486,126 @@ const createNodePrinter = (modules) => {
6441764486
}
6441864487
};
6441964488

64489+
/**
64490+
* terser's `c ? a = x : a = y` as `a = c ? x : y`: the one assignment every
64491+
* branch of an `if` printing as `?:`, or of a conditional, ends in.
64492+
* @param {number} node the `if` or conditional
64493+
* @returns {[string, MinifierVariable] | null} its operator and the name it sets, null where branches differ or a test may change the name
64494+
*/
64495+
const hoistOf = (node) => {
64496+
let found = hoists.get(node);
64497+
if (found !== undefined) return found;
64498+
found = null;
64499+
const isIf = typeOf(node) === NodeType.IfStatement;
64500+
const test = childOf(Field.test, node);
64501+
if (!isIf || ifForm(node) === CONDITIONAL_FORM) {
64502+
const consequent = assignedBy(
64503+
isIf
64504+
? unwrappedBranch(tree.field(0, Field.consequent, node), node)
64505+
: childOf(Field.consequent, node)
64506+
);
64507+
const alternate = assignedBy(
64508+
isIf
64509+
? unwrappedBranch(tree.field(0, Field.alternate, node), node)
64510+
: childOf(Field.alternate, node)
64511+
);
64512+
if (
64513+
consequent !== null &&
64514+
alternate !== null &&
64515+
consequent[0] === alternate[0] &&
64516+
consequent[1] === alternate[1] &&
64517+
// A compound assignment reads the name after the test ran.
64518+
(consequent[0] === "=" || isPureValue(test))
64519+
) {
64520+
found = consequent;
64521+
}
64522+
}
64523+
hoists.set(node, found);
64524+
return found;
64525+
};
64526+
64527+
/**
64528+
* @param {number} branch a branch: a statement, an expression, 0 for none
64529+
* @returns {[string, MinifierVariable] | null} the operator and the local name it ends in assigning, null for none
64530+
*/
64531+
const assignedBy = (branch) => {
64532+
let node = branch;
64533+
const type = typeOf(node);
64534+
if (type === NodeType.ExpressionStatement) {
64535+
node = childOf(Field.expression, node);
64536+
} else if (
64537+
type === NodeType.IfStatement ||
64538+
type === NodeType.ConditionalExpression
64539+
) {
64540+
return hoistOf(node);
64541+
}
64542+
if (
64543+
typeOf(node) !== NodeType.AssignmentExpression ||
64544+
dropsWrite[node] === 1 ||
64545+
hasEdgeComments(node)
64546+
) {
64547+
return null;
64548+
}
64549+
const operator = /** @type {string} */ (tree.textOf(Part.operator, node));
64550+
const target = childOf(Field.left, node);
64551+
if (
64552+
operator === "||=" ||
64553+
operator === "&&=" ||
64554+
operator === "??=" ||
64555+
typeOf(target) !== NodeType.Identifier
64556+
) {
64557+
return null;
64558+
}
64559+
const definition = /** @type {TreeDefinition[]} */ (mangledNames)[target];
64560+
return isMinifierVariable(definition) && !definition.undeclared
64561+
? [operator, definition]
64562+
: null;
64563+
};
64564+
64565+
/**
64566+
* @param {number} level where the `if` or conditional printed is among the printed one's parents, -1 for itself
64567+
* @returns {number} the `if` or conditional printing as `?:` whose branch it is and which hoists the same assignment, 0 for none
64568+
*/
64569+
const hoistingHolder = (level) => {
64570+
let holder = parentAt(level + 1);
64571+
const node = parentAt(level);
64572+
let branch = node;
64573+
if (typeOf(holder) === NodeType.BlockStatement) {
64574+
branch = holder;
64575+
holder = parentAt(level + 2);
64576+
}
64577+
const type = typeOf(holder);
64578+
if (type === NodeType.IfStatement) {
64579+
if (
64580+
childOf(Field.test, holder) === branch ||
64581+
ifForm(holder) !== CONDITIONAL_FORM
64582+
) {
64583+
return 0;
64584+
}
64585+
} else if (
64586+
type !== NodeType.ConditionalExpression ||
64587+
childOf(Field.test, holder) === node
64588+
) {
64589+
return 0;
64590+
}
64591+
return hoistOf(holder) === null ? 0 : holder;
64592+
};
64593+
64594+
/**
64595+
* Prints the name and operator a chain hoists, where the node printed
64596+
* opens it.
64597+
* @param {number} node the `if` or conditional printed
64598+
* @returns {void}
64599+
*/
64600+
const printHoistedHead = (node) => {
64601+
const hoisted = hoistOf(node);
64602+
if (hoisted === null || hoistingHolder(-1) !== 0) return;
64603+
output.print_name(hoisted[1].mangled_name || hoisted[1].name);
64604+
output.space();
64605+
output.print(hoisted[0]);
64606+
output.space();
64607+
};
64608+
6442064609
/**
6442164610
* Prints an `if` as `t?a:b`, or as `return t?x:` the `return` after it ends.
6442264611
* @param {number} node the `if`
@@ -64426,6 +64615,7 @@ const createNodePrinter = (modules) => {
6442664615
const printIfExpression = (node, form) => {
6442764616
const test = childOf(Field.test, node);
6442864617
const branch = form === CONDITIONAL_FORM && branchForm() !== 0;
64618+
if (form === CONDITIONAL_FORM) printHoistedHead(node);
6442964619
if (form === RETURN_FORM) {
6443064620
const previous = returnedBy.get(node);
6443164621
// One continuing the `?:` an `if` before it opens prints none.
@@ -65858,6 +66048,29 @@ const createNodePrinter = (modules) => {
6585866048
printIfExpression(node, form);
6585966049
return;
6586066050
}
66051+
const merged =
66052+
compresses && node < mergedExits.length ? mergedExits[node] : 0;
66053+
if (merged !== 0) {
66054+
// The `if`s next to it leaving alike print as one, testing each.
66055+
const test = childOf(Field.test, node);
66056+
if ((merged & 2) === 0) output.print("if(");
66057+
printNode(
66058+
test,
66059+
bindsLooser(test) ||
66060+
(typeOf(test) === NodeType.LogicalExpression &&
66061+
tree.textOf(Part.operator, test) === "??")
66062+
);
66063+
if ((merged & 1) !== 0) {
66064+
output.print("||");
66065+
/** @type {JavascriptPrintContext} */ (writer).consume(
66066+
tree.field(0, Field.consequent, node)
66067+
);
66068+
} else {
66069+
output.print(")");
66070+
printMaybeBracedBody(tree.field(0, Field.consequent, node));
66071+
}
66072+
return;
66073+
}
6586166074
const alternate = tree.field(0, Field.alternate, node);
6586266075
output.print("if");
6586366076
output.space();
@@ -66369,6 +66582,17 @@ const createNodePrinter = (modules) => {
6636966582
};
6637066583
generators[NodeType.AssignmentPattern] = printBinary;
6637166584
generators[NodeType.AssignmentExpression] = (node) => {
66585+
if (
66586+
compresses &&
66587+
hoistingHolder(
66588+
typeOf(parentAt(0)) === NodeType.ExpressionStatement ? 0 : -1
66589+
) !== 0
66590+
) {
66591+
// The chain it ends a branch of assigns, once, before its test.
66592+
skipNode(childOf(Field.left, node));
66593+
printNode(childOf(Field.right, node));
66594+
return;
66595+
}
6637266596
if (dropsWrite[node] !== 1) {
6637366597
printBinary(node);
6637466598
return;
@@ -66488,6 +66712,7 @@ const createNodePrinter = (modules) => {
6648866712
generators[NodeType.ConditionalExpression] = (node) => {
6648966713
if (dropsDeadCode && printsFolded(node, foldedBranch(node))) return;
6649066714
const test = childOf(Field.test, node);
66715+
if (compresses) printHoistedHead(node);
6649166716
if (compresses) {
6649266717
const consequent = childOf(Field.consequent, node);
6649366718
const alternate = childOf(Field.alternate, node);
@@ -66828,6 +67053,7 @@ const createNodePrinter = (modules) => {
6682867053
} else if (dropped.length > 0) {
6682967054
dropped = new Uint8Array(0);
6683067055
dropsWrite = new Uint8Array(0);
67056+
mergedExits = new Uint8Array(0);
6683167057
ifForms = new Uint8Array(0);
6683267058
inlined.clear();
6683367059
}

‎test/unitCases/javascript/printer.unittest.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15246,6 +15246,18 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1524615246
{},
1524715247
"function f(n,t){n=2;return[(n=>n+1)(n),(n=>()=>n)(t),function(){return this}(),function n(){return n}(),(async()=>1)(),(n=>n)(t,t),(()=>1)?.()]}"
1524815248
],
15249+
// The assignment every branch of a chain ends in prints once, before its test.
15250+
[
15251+
"function f(e, t) { if (e === 7) t += \"a\"; else if (e === 8) t += \"b\"; else t += e; if (e()) t += 1; else t += 2; return [t, e ? t = 1 : t = 2, e ? t = 1 : t += 2, e ? t = 1 : o.t = 2] }",
15252+
{},
15253+
"function f(t,e){e+=t===7?\"a\":t===8?\"b\":t;t()?e+=1:e+=2;return[e,e=t?1:2,t?e=1:e+=2,t?e=1:o.t=2]}"
15254+
],
15255+
// `if`s next to each other leaving alike test as one.
15256+
[
15257+
"function f(a, b, c) { for (const t of a) { if (t < 1) continue; if (t > 9) { continue } g(t) } if (a) return; if (b = c, b) return; if (a ?? b) return; if (c) break_(); g() }",
15258+
{},
15259+
"function f(f,n,t){for(let n of f){if(n<1||n>9)continue;g(n)}if(f||(n=t,n)||(f??n))return;if(t)break_();g()}"
15260+
],
1524915261
[
1525015262
"function f(a){ if (0) x(); Math.PI }",
1525115263
{ compress: {toplevel: false,builtins_ecma: 2020},format: {comments: false} },

0 commit comments

Comments
 (0)