Skip to content

Commit 0c6e9d8

Browse files
feat(javascript): merge consecutive ifs returning or throwing the same value in the minifier (#22507)
* feat(javascript): merge consecutive `if`s returning or throwing the same value in the minifier `if (a) return x; if (b) return x;` becomes `if (a || b) return x;`, and `throw` alike: the second test runs only where the first did not leave. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD * test(javascript): fit two minifier tests to #22473 and #22499 together #22473 counts a conversion as code that may run, so the value #22499's improved case moved past `console` now has effects and stays put; it now moves a comparison, which converts nothing. And #22499 now shortens #22473's literal case, whose assertion holds the output to terser's, so that case runs with the improve phase off. With both in, `vercel/ms` is bigger than oxc's output again (787 against 781 gzip), so it goes back on OXC_SMALLER, where #22499 had retired it on its own. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent cb7a4f1 commit 0c6e9d8

4 files changed

Lines changed: 83 additions & 8 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18640,6 +18640,69 @@ const installImprove = (modules) => {
1864018640
: optimized;
1864118641
});
1864218642

18643+
/**
18644+
* Whether a statement is an `if` with no `else` whose branch only leaves with a value.
18645+
* @param {Node} node a statement
18646+
* @returns {boolean} true for `if (a) return x` and `if (a) throw x`; terser folds bare jumps itself
18647+
*/
18648+
const isBareExitIf = (node) =>
18649+
A.isIfNode(node) &&
18650+
!node.alternate &&
18651+
A.isExitNode(node.consequent) &&
18652+
Boolean(node.consequent.argument);
18653+
18654+
/**
18655+
* `if (a) return x; if (b) return x;` as `if (a || b) return x;`: the second
18656+
* test runs only where the first did not leave, as `||` has it.
18657+
* @param {Node[]} statements the statements, changed in place
18658+
* @returns {void}
18659+
*/
18660+
const mergeRepeatedExits = (statements) => {
18661+
for (let i = 0; i < statements.length - 1; i++) {
18662+
const first = statements[i];
18663+
if (!isBareExitIf(first)) continue;
18664+
const second = statements[i + 1];
18665+
if (
18666+
!isBareExitIf(second) ||
18667+
first.consequent.type !== second.consequent.type ||
18668+
!A.isEquivalent(first.consequent, second.consequent)
18669+
) {
18670+
continue;
18671+
}
18672+
statements.splice(
18673+
i,
18674+
2,
18675+
makeNode(A.IfNode, first, {
18676+
test: makeNode(A.BinaryNode, first.test, {
18677+
operator: "||",
18678+
left: first.test,
18679+
right: second.test
18680+
}),
18681+
consequent: first.consequent,
18682+
alternate: null
18683+
})
18684+
);
18685+
i--;
18686+
}
18687+
};
18688+
18689+
for (const name of [
18690+
"Block",
18691+
"BlockStatement",
18692+
"Lambda",
18693+
"Arrow",
18694+
"Function",
18695+
"ClassStaticBlock"
18696+
]) {
18697+
A.wrapOptimizer(name, (optimizeBody) => (self, compressor) => {
18698+
const statements = A.bodyOf(self);
18699+
if (improvements.enabled && Array.isArray(statements)) {
18700+
mergeRepeatedExits(statements);
18701+
}
18702+
return optimizeBody(self, compressor);
18703+
});
18704+
}
18705+
1864318706
/**
1864418707
* The `typeof` an equality with a string reads, and that string, in either
1864518708
* order, as terser leaves either.

‎test/helpers/oxcSmaller.js‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ const OXC_SMALLER_BY_REASON = [
6262
"oxc minifier: peephole/esbuild.rs:645",
6363
"oxc minifier: peephole/esbuild.rs:684",
6464
"oxc minifier: peephole/esbuild.rs:688",
65-
"oxc minifier: peephole/esbuild.rs:692",
6665
"oxc minifier: peephole/esbuild.rs:700",
67-
"oxc minifier: peephole/esbuild.rs:706",
6866
"oxc minifier: peephole/esbuild.rs:824",
6967
"oxc minifier: peephole/esbuild.rs:828",
7068
"oxc minifier: peephole/esbuild.rs:832",
@@ -573,7 +571,6 @@ const OXC_SMALLER_BY_REASON = [
573571
"oxc minifier: peephole/minimize_conditions.rs:1130",
574572
"oxc minifier: peephole/minimize_conditions.rs:1206",
575573
"oxc minifier: peephole/minimize_conditions.rs:1226",
576-
"oxc minifier: peephole/minimize_conditions.rs:124",
577574
"oxc minifier: peephole/minimize_conditions.rs:1245",
578575
"oxc minifier: peephole/minimize_conditions.rs:1254",
579576
"oxc minifier: peephole/minimize_conditions.rs:1294",
@@ -596,7 +593,6 @@ const OXC_SMALLER_BY_REASON = [
596593
"oxc minifier: peephole/minimize_conditions.rs:1320",
597594
"oxc minifier: peephole/minimize_conditions.rs:1321",
598595
"oxc minifier: peephole/minimize_conditions.rs:1325",
599-
"oxc minifier: peephole/minimize_conditions.rs:133",
600596
"oxc minifier: peephole/minimize_conditions.rs:1334",
601597
"oxc minifier: peephole/minimize_conditions.rs:1335",
602598
"oxc minifier: peephole/minimize_conditions.rs:1336",
@@ -655,6 +651,7 @@ const OXC_SMALLER_BY_REASON = [
655651
"swc minifier: fixture/issues/firebase/dist/1/input.js",
656652
"swc minifier: fixture/issues/react-instancesearch/004/input.js",
657653
"swc minifier: fixture/issues/react/hooks/3/input.js",
654+
"swc minifier: fixture/issues/vercel/005/input.js",
658655
"swc minifier: fixture/next/31077/static/chunks/1606726a.10299989c08cb523/input.js",
659656
"swc minifier: fixture/next/33265/static/chunks/pages/index-cb36c1bf7f830e3c/input.js",
660657
"swc minifier: fixture/next/36127/2/3/input.js",
@@ -686,6 +683,7 @@ const OXC_SMALLER_BY_REASON = [
686683
"swc minifier: fixture/projects/next/archive-3/pages/dynamic/ssr-true-6d4aa8fc503b9d073aef/input.js",
687684
"swc minifier: fixture/projects/react/.6/input.js",
688685
"swc minifier: fixture/projects/wmr/archive-1/chunks/hoofd.module.6c5395cb/input.js",
686+
"swc minifier: full/vercel/ms/1/input.js",
689687
"swc minifier: projects/files/backbone-1.1.0.js",
690688
"swc minifier: projects/files/jquery-1.9.1.js",
691689
"swc minifier: projects/files/jquery.mobile-1.4.2.js",
@@ -1339,7 +1337,6 @@ const OXC_SMALLER_BY_REASON = [
13391337
"oxc minifier: peephole/minimize_statements.rs:133",
13401338
"oxc minifier: peephole/minimize_statements.rs:137",
13411339
"oxc minifier: peephole/minimize_statements.rs:141",
1342-
"oxc minifier: peephole/minimize_statements.rs:177",
13431340
"oxc minifier: peephole/minimize_statements.rs:188",
13441341
"oxc minifier: peephole/minimize_statements.rs:195",
13451342
"oxc minifier: peephole/minimize_statements.rs:351"
@@ -1411,7 +1408,6 @@ const OXC_SMALLER_BY_REASON = [
14111408
[
14121409
"oxc writes `if` statements shorter, as oxc's `peephole/minimize_if_statement.rs` tests show",
14131410
[
1414-
"oxc minifier: peephole/minimize_if_statement.rs:61",
14151411
"oxc minifier: peephole/minimize_if_statement.rs:74",
14161412
"oxc minifier: peephole/minimize_if_statement.rs:78",
14171413
"oxc minifier: peephole/minimize_if_statement.rs:82"

‎test/specCases/minify-corpora.spectest.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,16 @@ for (const [reason, keys] of /** @type {[string, string[]][]} */ ([
16011601
"superPropStatics.js (the default minimizer's options)"
16021602
]
16031603
],
1604+
[
1605+
"consecutive `if`s leaving with one value merged, where the source repeats the `if` gzip matched",
1606+
[
1607+
"S7.9_A5.8_T1.js (the default minimizer's options)",
1608+
"S7.9_A5.9_T1.js (the default minimizer's options)",
1609+
"if_return/if_return_same_value (its own options)",
1610+
"if_return/if_return_same_value (the default minimizer's options)",
1611+
"symbol-logical-not-evaluation.js (the default minimizer's options)"
1612+
]
1613+
],
16041614
[
16051615
"a literal folded into the string it makes where the source repeats the literal gzip matched",
16061616
[

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ const IMPROVED_CASES = [
284284
`(() => { ("x"); ${TRY} })();`,
285285
{ compress: { side_effects: false }, mangle: false }
286286
],
287+
[
288+
"consecutive `if`s leaving the same way",
289+
'function f(a, b, c) { if (a == null) return true; if (g(a, b)) return true; if (c) return true; for (var i = 0; i < 2; i++) { if (a === i) continue; if (b === i) continue; console.log(i); } try { if (a === 7) throw a; if (b === 7) throw a; } catch (e) { console.log("caught", e); } for (;;) { if (a) break; if (b) break; console.log(3); break; } return false; } function g(a, b) { return a === b; } console.log(f(1, 1), f(null, 2), f(7, 0), f(0, 7), f(0, 0, 0));',
290+
{ compress: {}, mangle: false }
291+
],
287292
[
288293
"an arrow reading `this`, which it shares",
289294
`(() => { ${TRY} console.log(typeof this); })();`,
@@ -642,7 +647,7 @@ const IMPROVED_CASES = [
642647
],
643648
[
644649
"a value moved past a read of `console`, a built-in or one of its methods",
645-
"function f(x) { var r = x + 1, a = r * r - r; console.log(a + 7); var v = g(); console.log(Math.max(v, 1)); var c; c = 2; console.log(c); } function g() { return 3; } f(1);"
650+
"function f(x) { var a = x === 1 ? 2 : 3; console.log(a + 7); var v = g(); console.log(Math.max(v, 1)); var c; c = 2; console.log(c); } function g() { return 3; } f(1);"
646651
],
647652
[
648653
"an array of empty strings",
@@ -710,6 +715,7 @@ const KEPT_CASES = [
710715
["a conditional whose other branch reads a global getter again", 'var n = 0; Object.defineProperty(globalThis, "g", { get: function () { return n++; }, configurable: true }); function f(b) { return g ? b : g; } console.log(f(1), f(2));'],
711716
["a conditional whose other branch reads a getter twice or a different binding", "var n = 0, o = { get x() { return ++n; } }; function f(b) { var a = 1; { let a = 0; var r = a ? b : o.x ? b : o.x; } return [r, a, n]; } console.log(f(2));"],
712717
["a bigint and a number concatenated, which throws", "try { console.log(1n + 2); } catch (e) { console.log(e.name); }"],
718+
["consecutive `if`s leaving with different values or to different labels", 'function f(a, b) { if (a) return 1; if (b) return 2; x: for (;;) { for (;;) { if (a) break x; if (b) break; console.log(3); break x; } console.log(4); break; } for (var i = 0; i < 2; i++) console.log(i); } f(1, 0); f(0, 1); f(0, 0);'],
713719
["an array literal joining to a string longer than it", 'console.log([!0, !0, !0] + "");'],
714720
["`+` and `~` of what a literal does not give", "function f(a) { return [+[a], ~a !== 0 ? 1 : 2]; } console.log(f(1), f(1n));"],
715721
["a string literal indexed past its end, by no constant or as a target", 'function f(i) { var s = "abc"; "abc"[0] = 1; return ["abc"[5], "abc"[i], "abc"[1.5], "abc"[-1]]; } console.log(f(1));'],
@@ -5521,7 +5527,7 @@ describe("syntax-printer", () => {
55215527
const input =
55225528
"(function () { var array = []; var push = array.push, slice = array.slice; console.log(typeof push); })();";
55235529
const options = { compress: {}, mangle: false };
5524-
const { code } = await minify(input, options);
5530+
const { code } = await unimproved(() => minify(input, options));
55255531
const reference = await terserReference().minify(input, options);
55265532
expect(code).toBe(reference.code);
55275533
});

0 commit comments

Comments
 (0)