Skip to content

Commit 2cdf722

Browse files
committed
feat(minifier): constant fold small integer multiplication (n <= 255) (#12236)
ref: evanw/esbuild@492e299
1 parent 314f970 commit 2cdf722

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

crates/oxc_minifier/src/peephole/fold_constants.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,12 @@ impl<'a> PeepholeOptimizations {
336336
|| *right == 0.0
337337
|| right.is_nan()
338338
|| right.is_infinite()
339+
// Small number multiplication.
340+
|| (e.operator == BinaryOperator::Multiplication
341+
&& left.abs() <= 255.0
342+
&& left.fract() == 0.0
343+
&& right.abs() <= 255.0
344+
&& right.fract() == 0.0)
339345
})
340346
.and_then(|_| ctx.eval_binary(e)),
341347
BinaryOperator::Division => Self::extract_numeric_values(e)
@@ -1751,6 +1757,12 @@ mod test {
17511757
// test("x = (null + 1) * 2", "x = 2");
17521758
// test("x = y + (z * 24 * 60 * 60 * 1000)", "x = y + z * 864E5");
17531759
fold("x = y + (z & 24 & 60 & 60 & 1000)", "x = y + (z & 8)");
1760+
fold("x = -1 * -1", "x = 1");
1761+
fold("x = 1 * -1", "x = -1");
1762+
fold("x = 255 * 255", "x = 65025");
1763+
fold("x = -255 * 255", "x = -65025");
1764+
fold("x = -255 * -255", "x = 65025");
1765+
fold_same("x = 256 * 255");
17541766
}
17551767

17561768
#[test]

crates/oxc_minifier/tests/peephole/esbuild.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ fn constant_evaluation_test() {
901901
test("x = +{valueOf:()=>1}", "x = +{ valueOf: () => 1 };");
902902
test("x = 3 + 6", "x = 9;");
903903
test("x = 3 - 6", "x = -3;");
904-
test("x = 3 * 6", "x = 3 * 6;");
904+
test("x = 3 * 6", "x = 18;");
905905
test("x = 3 / 6", "x = 3 / 6;");
906906
test("x = 3 % 6", "x = 3 % 6;");
907907
test("x = 3 ** 6", "x = 3 ** 6;");

tasks/minsize/minsize.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Original | minified | minified | gzip | gzip | Fixture
33
-------------------------------------------------------------------------------------
44
72.14 kB | 23.49 kB | 23.70 kB | 8.47 kB | 8.54 kB | react.development.js
55

6-
173.90 kB | 59.52 kB | 59.82 kB | 19.18 kB | 19.33 kB | moment.js
6+
173.90 kB | 59.51 kB | 59.82 kB | 19.18 kB | 19.33 kB | moment.js
77

88
287.63 kB | 89.33 kB | 90.07 kB | 30.95 kB | 31.95 kB | jquery.js
99

@@ -15,7 +15,7 @@ Original | minified | minified | gzip | gzip | Fixture
1515

1616
1.01 MB | 440.17 kB | 458.89 kB | 122.37 kB | 126.71 kB | bundle.min.js
1717

18-
1.25 MB | 647.00 kB | 646.76 kB | 160.28 kB | 163.73 kB | three.js
18+
1.25 MB | 647 kB | 646.76 kB | 160.28 kB | 163.73 kB | three.js
1919

2020
2.14 MB | 716.13 kB | 724.14 kB | 161.80 kB | 181.07 kB | victory.js
2121

0 commit comments

Comments
 (0)