Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 24, 2024
1 parent fe748b4 commit 22d6072
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
14 changes: 12 additions & 2 deletions crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,25 @@ impl<'a, 'b> PeepholeFoldConstants {
| BinaryOperator::Multiplication
| BinaryOperator::Exponential => match (&e.left, &e.right) {
(Expression::NumericLiteral(left), Expression::NumericLiteral(right)) => {
// Do not fold any division unless rhs is 0.
if e.operator == BinaryOperator::Division
&& right.value != 0.0
&& !right.value.is_nan()
&& !right.value.is_infinite()
{
return None;
}
let value = ctx.eval_binary_expression(e)?;
let ConstantValue::Number(num) = value else { return None };
Self::approximate_printed_int_char_count(num);
(num.is_nan()
|| num.is_infinite()
|| (num.abs() <= f64::powf(2.0, 53.0)
&& Self::approximate_printed_int_char_count(num)
<= Self::approximate_printed_int_char_count(left.value)
+ Self::approximate_printed_int_char_count(right.value)
+ e.operator.as_str().len()))
.then(|| value)
.then_some(value)
}
_ => ctx.eval_binary_expression(e),
}
Expand All @@ -280,6 +289,7 @@ impl<'a, 'b> PeepholeFoldConstants {
}

// https://github.com/evanw/esbuild/blob/v0.24.2/internal/js_ast/js_ast_helpers.go#L1128
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
fn approximate_printed_int_char_count(value: f64) -> usize {
let mut count = if value.is_infinite() {
"Infinity".len()
Expand Down Expand Up @@ -1407,7 +1417,7 @@ mod test {
#[test]
fn test_fold_arithmetic() {
test("x = 10 + 20", "x = 30");
test("x = 2 / 4", "x = 0.5");
test_same("x = 2 / 4");
test("x = 2.25 * 3", "x = 6.75");
test_same("z = x * y");
test_same("x = y * 5");
Expand Down
8 changes: 4 additions & 4 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ Original | minified | minified | gzip | gzip | Fixture

544.10 kB | 73.31 kB | 72.48 kB | 26.12 kB | 26.20 kB | lodash.js

555.77 kB | 275.92 kB | 270.13 kB | 91.13 kB | 90.80 kB | d3.js
555.77 kB | 275.71 kB | 270.13 kB | 91.08 kB | 90.80 kB | d3.js

1.01 MB | 466.83 kB | 458.89 kB | 126.74 kB | 126.71 kB | bundle.min.js

1.25 MB | 661.43 kB | 646.76 kB | 163.92 kB | 163.73 kB | three.js
1.25 MB | 661.30 kB | 646.76 kB | 163.92 kB | 163.73 kB | three.js

2.14 MB | 740.33 kB | 724.14 kB | 181.33 kB | 181.07 kB | victory.js
2.14 MB | 740.13 kB | 724.14 kB | 181.28 kB | 181.07 kB | victory.js

3.20 MB | 1.02 MB | 1.01 MB | 331.93 kB | 331.56 kB | echarts.js
3.20 MB | 1.02 MB | 1.01 MB | 331.88 kB | 331.56 kB | echarts.js

6.69 MB | 2.39 MB | 2.31 MB | 495.62 kB | 488.28 kB | antd.js

Expand Down

0 comments on commit 22d6072

Please sign in to comment.