Skip to content

Commit

Permalink
fix(es/minifier): Abort seq inliner if b can short-circuit (#8128)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8119
  • Loading branch information
kdy1 authored Oct 18, 2023
1 parent 736831c commit 111de26
Show file tree
Hide file tree
Showing 13 changed files with 464 additions and 3 deletions.
65 changes: 65 additions & 0 deletions crates/swc/tests/exec/issues-8xxx/8119/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es2022",
"loose": false,
"minify": {
"compress": {
"arguments": false,
"arrows": true,
"booleans": true,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"directives": true,
"drop_console": false,
"drop_debugger": true,
"evaluate": true,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"properties": true,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": true,
"switches": true,
"typeofs": true,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"const_to_let": true,
"pristine_globals": true,
"passes": 2
},
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true
}
24 changes: 24 additions & 0 deletions crates/swc/tests/exec/issues-8xxx/8119/1/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const myArr = [];
// function with side effect
function foo(arr) {
arr.push('foo');
return 'foo';
}
let a;

if (Math.random() > 1.00000) {
a = true;
}

// the function call below should always run
// regardless of whether `a` is `undefined`
let b = foo(myArr);

// const seems to keep this line here instead of
// moving it behind the logitcal nullish assignment
// const b = foo(myArr);

a ??= b;

console.log(a);
console.log(myArr);
24 changes: 24 additions & 0 deletions crates/swc/tests/exec/issues-8xxx/8119/2/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const myArr = [];
// function with side effect
function foo(arr) {
arr.push('foo');
return 'foo';
}
let a;

if (Math.random() > -0.1) {
a = true;
}

// the function call below should always run
// regardless of whether `a` is `undefined`
let b = foo(myArr);

// const seems to keep this line here instead of
// moving it behind the logitcal nullish assignment
// const b = foo(myArr);

a ??= b;

console.log(a);
console.log(myArr);
65 changes: 65 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8119/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es2022",
"loose": false,
"minify": {
"compress": {
"arguments": false,
"arrows": true,
"booleans": true,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"directives": true,
"drop_console": false,
"drop_debugger": true,
"evaluate": true,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"properties": true,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": true,
"switches": true,
"typeofs": true,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"const_to_let": true,
"pristine_globals": true,
"passes": 2
},
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true
}
24 changes: 24 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8119/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const myArr = [];
// function with side effect
function foo(arr) {
arr.push('foo');
return 'foo';
}
let a;

if (Math.random() > 0.5) {
a = true;
}

// the function call below should always run
// regardless of whether `a` is `undefined`
let b = foo(myArr);

// const seems to keep this line here instead of
// moving it behind the logitcal nullish assignment
// const b = foo(myArr);

a ??= b;

console.log(a);
console.log(myArr);
10 changes: 10 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8119/output/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let a;
const myArr = [];
Math.random() > 0.5 && (a = !0);
// the function call below should always run
// regardless of whether `a` is `undefined`
let b = (myArr.push('foo'), 'foo');
console.log(// const seems to keep this line here instead of
// moving it behind the logitcal nullish assignment
// const b = foo(myArr);
a ??= b), console.log(myArr);
4 changes: 4 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2489,6 +2489,10 @@ impl Optimizer<'_> {
/// 1, arr[i]`
//
fn should_not_check_rhs_of_assign(&self, a: &Mergable, b: &mut AssignExpr) -> Result<bool, ()> {
if b.op.may_short_circuit() {
return Ok(true);
}

if let Some(a_id) = a.id() {
match a {
Mergable::Expr(Expr::Assign(AssignExpr { op: op!("="), .. })) => {}
Expand Down
Loading

1 comment on commit 111de26

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 111de26 Previous: 4f67794 Ratio
es/full/bugs-1 288829 ns/iter (± 4729) 285618 ns/iter (± 6329) 1.01
es/full/minify/libraries/antd 1389036540 ns/iter (± 21284821) 1386313980 ns/iter (± 24822725) 1.00
es/full/minify/libraries/d3 294138948 ns/iter (± 3409382) 289848387 ns/iter (± 2129801) 1.01
es/full/minify/libraries/echarts 1109978388 ns/iter (± 7166441) 1103835486 ns/iter (± 8002603) 1.01
es/full/minify/libraries/jquery 88689893 ns/iter (± 223944) 88444329 ns/iter (± 131550) 1.00
es/full/minify/libraries/lodash 103844115 ns/iter (± 205738) 103694449 ns/iter (± 401752) 1.00
es/full/minify/libraries/moment 52339031 ns/iter (± 105814) 52299663 ns/iter (± 555346) 1.00
es/full/minify/libraries/react 18844953 ns/iter (± 60064) 18875043 ns/iter (± 75554) 1.00
es/full/minify/libraries/terser 228399108 ns/iter (± 325412) 230926211 ns/iter (± 1411456) 0.99
es/full/minify/libraries/three 409129572 ns/iter (± 3074984) 406505582 ns/iter (± 1425320) 1.01
es/full/minify/libraries/typescript 2760432748 ns/iter (± 14531611) 2759524618 ns/iter (± 13642008) 1.00
es/full/minify/libraries/victory 590763185 ns/iter (± 4211196) 598056874 ns/iter (± 11703653) 0.99
es/full/minify/libraries/vue 126254991 ns/iter (± 968542) 125107901 ns/iter (± 646092) 1.01
es/full/codegen/es3 34082 ns/iter (± 92) 34981 ns/iter (± 323) 0.97
es/full/codegen/es5 34171 ns/iter (± 128) 35082 ns/iter (± 136) 0.97
es/full/codegen/es2015 34093 ns/iter (± 133) 35106 ns/iter (± 161) 0.97
es/full/codegen/es2016 33986 ns/iter (± 148) 35030 ns/iter (± 180) 0.97
es/full/codegen/es2017 34115 ns/iter (± 97) 35077 ns/iter (± 203) 0.97
es/full/codegen/es2018 34052 ns/iter (± 214) 34867 ns/iter (± 218) 0.98
es/full/codegen/es2019 34122 ns/iter (± 132) 35128 ns/iter (± 97) 0.97
es/full/codegen/es2020 34160 ns/iter (± 81) 35152 ns/iter (± 168) 0.97
es/full/all/es3 175675957 ns/iter (± 684933) 178297141 ns/iter (± 1215853) 0.99
es/full/all/es5 168845996 ns/iter (± 1550552) 170952382 ns/iter (± 1087977) 0.99
es/full/all/es2015 127673298 ns/iter (± 985543) 129037936 ns/iter (± 880047) 0.99
es/full/all/es2016 126569701 ns/iter (± 503674) 128931320 ns/iter (± 823433) 0.98
es/full/all/es2017 125724545 ns/iter (± 606938) 127864302 ns/iter (± 698819) 0.98
es/full/all/es2018 123594957 ns/iter (± 631930) 124681982 ns/iter (± 613298) 0.99
es/full/all/es2019 122743563 ns/iter (± 1066059) 125025589 ns/iter (± 731101) 0.98
es/full/all/es2020 119250751 ns/iter (± 890148) 121492810 ns/iter (± 763908) 0.98
es/full/parser 561468 ns/iter (± 3541) 562089 ns/iter (± 3815) 1.00
es/full/base/fixer 18690 ns/iter (± 180) 18552 ns/iter (± 137) 1.01
es/full/base/resolver_and_hygiene 83334 ns/iter (± 239) 82925 ns/iter (± 172) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.