Skip to content

Commit

Permalink
fix(es/minifier): Abort seq inliner using visitor (#7699)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #7697.
  • Loading branch information
kdy1 authored Jul 25, 2023
1 parent cd98859 commit a26dbce
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
41 changes: 38 additions & 3 deletions crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2031,9 +2031,11 @@ impl Optimizer<'_> {
pat_usage: Default::default(),
target: a_id,
in_lhs: false,
abort: false,
in_abort: false,
};
b.visit_with(&mut v);
if v.expr_usage != 1 || v.pat_usage != 0 {
if v.expr_usage != 1 || v.pat_usage != 0 || v.abort {
log_abort!(
"sequences: Aborting merging of an update expression because of \
usage counts ({}, ref = {}, pat = {})",
Expand Down Expand Up @@ -2103,9 +2105,11 @@ impl Optimizer<'_> {
pat_usage: Default::default(),
target: a_id,
in_lhs: false,
abort: false,
in_abort: false,
};
b.visit_with(&mut v);
if v.expr_usage != 1 || v.pat_usage != 0 {
if v.expr_usage != 1 || v.pat_usage != 0 || v.abort {
log_abort!(
"sequences: Aborting merging of an update expression because of \
usage counts ({}, ref = {}, pat = {})",
Expand Down Expand Up @@ -2426,9 +2430,11 @@ impl Optimizer<'_> {
pat_usage: Default::default(),
target: &left_id,
in_lhs: false,
abort: false,
in_abort: false,
};
b.visit_with(&mut v);
if v.expr_usage != 1 || v.pat_usage != 0 {
if v.expr_usage != 1 || v.pat_usage != 0 || v.abort {
log_abort!(
"sequences: Aborting because of usage counts ({}{:?}, ref = {}, pat = {})",
left_id.sym,
Expand Down Expand Up @@ -2493,15 +2499,23 @@ struct UsageCounter<'a> {
expr_usage: usize,
pat_usage: usize,

abort: bool,

target: &'a Ident,
in_lhs: bool,
in_abort: bool,
}

impl Visit for UsageCounter<'_> {
noop_visit_type!();

fn visit_ident(&mut self, i: &Ident) {
if self.target.sym == i.sym && self.target.span.ctxt == i.span.ctxt {
if self.in_abort {
self.abort = true;
return;
}

if self.in_lhs {
self.pat_usage += 1;
} else {
Expand All @@ -2521,6 +2535,27 @@ impl Visit for UsageCounter<'_> {
}
}

fn visit_update_expr(&mut self, e: &UpdateExpr) {
let old_in_abort = self.in_abort;
self.in_abort = true;
e.visit_children_with(self);
self.in_abort = old_in_abort;
}

fn visit_await_expr(&mut self, e: &AwaitExpr) {
let old_in_abort = self.in_abort;
self.in_abort = true;
e.visit_children_with(self);
self.in_abort = old_in_abort;
}

fn visit_yield_expr(&mut self, e: &YieldExpr) {
let old_in_abort = self.in_abort;
self.in_abort = true;
e.visit_children_with(self);
self.in_abort = old_in_abort;
}

fn visit_pat(&mut self, p: &Pat) {
let old = self.in_lhs;
self.in_lhs = true;
Expand Down
47 changes: 47 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/7697/1/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"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": true,
"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
}
6 changes: 6 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/7697/1/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let id = 0;

export function getId() {
id = id % 9999;
return `${id++}`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let id = 0;
export function getId() {
return id %= 9999, `${id++}`;
}

1 comment on commit a26dbce

@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: a26dbce Previous: bf72362 Ratio
es/full/bugs-1 285853 ns/iter (± 3976) 295893 ns/iter (± 13971) 0.97
es/full/minify/libraries/antd 1344833713 ns/iter (± 14217258) 1300169604 ns/iter (± 4500607) 1.03
es/full/minify/libraries/d3 279204650 ns/iter (± 2237406) 274043350 ns/iter (± 4335986) 1.02
es/full/minify/libraries/echarts 1063150951 ns/iter (± 10732679) 1039880170 ns/iter (± 5018225) 1.02
es/full/minify/libraries/jquery 84904492 ns/iter (± 161413) 84374921 ns/iter (± 110014) 1.01
es/full/minify/libraries/lodash 98457286 ns/iter (± 364404) 98222610 ns/iter (± 212860) 1.00
es/full/minify/libraries/moment 50061650 ns/iter (± 163498) 49670352 ns/iter (± 619350) 1.01
es/full/minify/libraries/react 17973589 ns/iter (± 51428) 17960005 ns/iter (± 34524) 1.00
es/full/minify/libraries/terser 218591049 ns/iter (± 690385) 216010404 ns/iter (± 689184) 1.01
es/full/minify/libraries/three 386302591 ns/iter (± 1787165) 377923656 ns/iter (± 1668424) 1.02
es/full/minify/libraries/typescript 2704054102 ns/iter (± 22056080) 2628788358 ns/iter (± 7883740) 1.03
es/full/minify/libraries/victory 564354197 ns/iter (± 4061461) 548099870 ns/iter (± 5933934) 1.03
es/full/minify/libraries/vue 120172468 ns/iter (± 854681) 119701859 ns/iter (± 183959) 1.00
es/full/codegen/es3 35011 ns/iter (± 72) 34543 ns/iter (± 119) 1.01
es/full/codegen/es5 34999 ns/iter (± 68) 34649 ns/iter (± 60) 1.01
es/full/codegen/es2015 35033 ns/iter (± 91) 34628 ns/iter (± 91) 1.01
es/full/codegen/es2016 34965 ns/iter (± 47) 34694 ns/iter (± 83) 1.01
es/full/codegen/es2017 35032 ns/iter (± 75) 34551 ns/iter (± 105) 1.01
es/full/codegen/es2018 34979 ns/iter (± 101) 34605 ns/iter (± 113) 1.01
es/full/codegen/es2019 34942 ns/iter (± 1233) 34743 ns/iter (± 40) 1.01
es/full/codegen/es2020 34977 ns/iter (± 103) 34673 ns/iter (± 80) 1.01
es/full/all/es3 171618168 ns/iter (± 1168306) 171234167 ns/iter (± 1020832) 1.00
es/full/all/es5 163925618 ns/iter (± 679913) 164162911 ns/iter (± 1023878) 1.00
es/full/all/es2015 124431534 ns/iter (± 880049) 123244810 ns/iter (± 781926) 1.01
es/full/all/es2016 123200028 ns/iter (± 619632) 122342313 ns/iter (± 709363) 1.01
es/full/all/es2017 123245103 ns/iter (± 441737) 121986620 ns/iter (± 661749) 1.01
es/full/all/es2018 120268317 ns/iter (± 673033) 119692545 ns/iter (± 843644) 1.00
es/full/all/es2019 120128071 ns/iter (± 717580) 118829379 ns/iter (± 861081) 1.01
es/full/all/es2020 114965126 ns/iter (± 696101) 114315407 ns/iter (± 574066) 1.01
es/full/parser 534584 ns/iter (± 6733) 525440 ns/iter (± 4371) 1.02
es/full/base/fixer 18671 ns/iter (± 58) 18414 ns/iter (± 111) 1.01
es/full/base/resolver_and_hygiene 84065 ns/iter (± 202) 84204 ns/iter (± 129) 1.00
serialization of serde 291 ns/iter (± 0) 303 ns/iter (± 3) 0.96
css/minify/libraries/bootstrap 28949232 ns/iter (± 46454) 28674479 ns/iter (± 110495) 1.01
css/visitor/compare/clone 1658228 ns/iter (± 4149) 1624199 ns/iter (± 6925) 1.02
css/visitor/compare/visit_mut_span 1777030 ns/iter (± 8025) 1767440 ns/iter (± 6766) 1.01
css/visitor/compare/visit_mut_span_panic 1853930 ns/iter (± 11176) 1847172 ns/iter (± 7857) 1.00
css/visitor/compare/fold_span 2577578 ns/iter (± 18410) 2556062 ns/iter (± 7931) 1.01
css/visitor/compare/fold_span_panic 2763185 ns/iter (± 15490) 2770439 ns/iter (± 24089) 1.00
css/lexer/bootstrap_5_1_3 4573530 ns/iter (± 3298) 4462039 ns/iter (± 1862) 1.02
css/lexer/foundation_6_7_4 3830140 ns/iter (± 1250) 3757639 ns/iter (± 2448) 1.02
css/lexer/tailwind_3_1_1 728054 ns/iter (± 553) 712980 ns/iter (± 682) 1.02
css/parser/bootstrap_5_1_3 19232293 ns/iter (± 86971) 19486349 ns/iter (± 28668) 0.99
css/parser/foundation_6_7_4 15341687 ns/iter (± 23260) 15371962 ns/iter (± 25007) 1.00
css/parser/tailwind_3_1_1 3000016 ns/iter (± 6973) 2973750 ns/iter (± 2966) 1.01
es/codegen/colors 734181 ns/iter (± 401976) 721208 ns/iter (± 394982) 1.02
es/codegen/large 3149366 ns/iter (± 1678863) 2909534 ns/iter (± 1547630) 1.08
es/codegen/with-parser/colors 44249 ns/iter (± 459) 43495 ns/iter (± 709) 1.02
es/codegen/with-parser/large 481484 ns/iter (± 1538) 477720 ns/iter (± 848) 1.01
es/minify/libraries/antd 1156504565 ns/iter (± 4953297) 1127593432 ns/iter (± 10153674) 1.03
es/minify/libraries/d3 241483316 ns/iter (± 882622) 241324872 ns/iter (± 1396529) 1.00
es/minify/libraries/echarts 924971927 ns/iter (± 5245711) 899228805 ns/iter (± 2738218) 1.03
es/minify/libraries/jquery 74113433 ns/iter (± 130044) 74115616 ns/iter (± 111342) 1.00
es/minify/libraries/lodash 88011118 ns/iter (± 139015) 88599712 ns/iter (± 175361) 0.99
es/minify/libraries/moment 43586399 ns/iter (± 167938) 43814868 ns/iter (± 47407) 0.99
es/minify/libraries/react 16043184 ns/iter (± 79994) 16067741 ns/iter (± 36107) 1.00
es/minify/libraries/terser 188989193 ns/iter (± 1090067) 191278221 ns/iter (± 1617346) 0.99
es/minify/libraries/three 321622425 ns/iter (± 1603076) 333234245 ns/iter (± 7480428) 0.97
es/minify/libraries/typescript 2260702679 ns/iter (± 9526861) 2264671257 ns/iter (± 32709803) 1.00
es/minify/libraries/victory 474077776 ns/iter (± 2146811) 476643246 ns/iter (± 12098503) 0.99
es/minify/libraries/vue 108075393 ns/iter (± 230809) 109965142 ns/iter (± 702471) 0.98
es/visitor/compare/clone 1941946 ns/iter (± 5235) 1941047 ns/iter (± 5119) 1.00
es/visitor/compare/visit_mut_span 2282686 ns/iter (± 4265) 2297610 ns/iter (± 3926) 0.99
es/visitor/compare/visit_mut_span_panic 2316399 ns/iter (± 3406) 2325886 ns/iter (± 9451) 1.00
es/visitor/compare/fold_span 3328557 ns/iter (± 6727) 3358573 ns/iter (± 8482) 0.99
es/visitor/compare/fold_span_panic 3483310 ns/iter (± 3915) 3474007 ns/iter (± 8543) 1.00
es/lexer/colors 12323 ns/iter (± 16) 12501 ns/iter (± 54) 0.99
es/lexer/angular 6103630 ns/iter (± 4731) 6080239 ns/iter (± 5155) 1.00
es/lexer/backbone 794949 ns/iter (± 2846) 792490 ns/iter (± 3006) 1.00
es/lexer/jquery 4442027 ns/iter (± 2306) 4413579 ns/iter (± 4182) 1.01
es/lexer/jquery mobile 6828738 ns/iter (± 8982) 6822677 ns/iter (± 6595) 1.00
es/lexer/mootools 3533154 ns/iter (± 3609) 3516828 ns/iter (± 1499) 1.00
es/lexer/underscore 660415 ns/iter (± 656) 660890 ns/iter (± 699) 1.00
es/lexer/three 20872648 ns/iter (± 7655) 20948756 ns/iter (± 34174) 1.00
es/lexer/yui 3809028 ns/iter (± 1419) 3803661 ns/iter (± 3539) 1.00
es/parser/colors 26542 ns/iter (± 63) 26194 ns/iter (± 49) 1.01
es/parser/angular 13424494 ns/iter (± 66326) 13303598 ns/iter (± 89480) 1.01
es/parser/backbone 2003941 ns/iter (± 9034) 1985651 ns/iter (± 7848) 1.01
es/parser/jquery 10851556 ns/iter (± 41895) 10975975 ns/iter (± 126890) 0.99
es/parser/jquery mobile 16773044 ns/iter (± 64838) 17034163 ns/iter (± 336252) 0.98
es/parser/mootools 8386175 ns/iter (± 15739) 8299811 ns/iter (± 32571) 1.01
es/parser/underscore 1719445 ns/iter (± 8426) 1699900 ns/iter (± 9684) 1.01
es/parser/three 46661889 ns/iter (± 575202) 45486790 ns/iter (± 162656) 1.03
es/parser/yui 8247312 ns/iter (± 35669) 8128756 ns/iter (± 27853) 1.01
es/preset-env/usage/builtin_type 135617 ns/iter (± 31429) 136236 ns/iter (± 31828) 1.00
es/preset-env/usage/property 17218 ns/iter (± 52) 16928 ns/iter (± 34) 1.02
es/resolver/typescript 89348999 ns/iter (± 1347422) 87168172 ns/iter (± 1592381) 1.03
es/fixer/typescript 65763096 ns/iter (± 1281292) 62444797 ns/iter (± 333555) 1.05
es/hygiene/typescript 137435295 ns/iter (± 882323) 131869880 ns/iter (± 567411) 1.04
es/resolver_with_hygiene/typescript 250713824 ns/iter (± 1776308) 242657260 ns/iter (± 799710) 1.03
es/visitor/base-perf/module_clone 60761 ns/iter (± 389) 59233 ns/iter (± 288) 1.03
es/visitor/base-perf/fold_empty 64934 ns/iter (± 291) 63066 ns/iter (± 259) 1.03
es/visitor/base-perf/fold_noop_impl_all 65151 ns/iter (± 535) 63327 ns/iter (± 338) 1.03
es/visitor/base-perf/fold_noop_impl_vec 64854 ns/iter (± 299) 63438 ns/iter (± 625) 1.02
es/visitor/base-perf/boxing_boxed_clone 56 ns/iter (± 0) 56 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed_clone 40 ns/iter (± 0) 41 ns/iter (± 0) 0.98
es/visitor/base-perf/boxing_boxed 109 ns/iter (± 0) 110 ns/iter (± 0) 0.99
es/visitor/base-perf/boxing_unboxed 77 ns/iter (± 0) 78 ns/iter (± 0) 0.99
es/visitor/base-perf/visit_empty 0 ns/iter (± 0) 0 ns/iter (± 0) NaN
es/visitor/base-perf/visit_contains_this 2521 ns/iter (± 14) 2536 ns/iter (± 10) 0.99
es/base/parallel/resolver/typescript 3842950052 ns/iter (± 317692659) 3680798049 ns/iter (± 254509330) 1.04
es/base/parallel/hygiene/typescript 1485192199 ns/iter (± 17118738) 1520686086 ns/iter (± 31729178) 0.98
misc/visitors/time-complexity/time 5 117 ns/iter (± 8) 137 ns/iter (± 11) 0.85
misc/visitors/time-complexity/time 10 376 ns/iter (± 5) 334 ns/iter (± 56) 1.13
misc/visitors/time-complexity/time 15 679 ns/iter (± 4) 525 ns/iter (± 73) 1.29
misc/visitors/time-complexity/time 20 1177 ns/iter (± 10) 918 ns/iter (± 70) 1.28
misc/visitors/time-complexity/time 40 3544 ns/iter (± 252) 3545 ns/iter (± 15) 1.00
misc/visitors/time-complexity/time 60 7357 ns/iter (± 18) 7355 ns/iter (± 42) 1.00
es/full-target/es2016 234365 ns/iter (± 591) 237080 ns/iter (± 2119) 0.99
es/full-target/es2017 222566 ns/iter (± 634) 224368 ns/iter (± 995) 0.99
es/full-target/es2018 212027 ns/iter (± 549) 212654 ns/iter (± 773) 1.00
es2020_nullish_coalescing 70695 ns/iter (± 465) 72773 ns/iter (± 467) 0.97
es2020_optional_chaining 81040 ns/iter (± 289) 83037 ns/iter (± 340) 0.98
es2022_class_properties 116944 ns/iter (± 405) 118872 ns/iter (± 455) 0.98
es2018_object_rest_spread 75636 ns/iter (± 272) 76886 ns/iter (± 337) 0.98
es2019_optional_catch_binding 64976 ns/iter (± 320) 65638 ns/iter (± 335) 0.99
es2017_async_to_generator 63478 ns/iter (± 346) 64509 ns/iter (± 341) 0.98
es2016_exponentiation 68912 ns/iter (± 226) 69807 ns/iter (± 289) 0.99
es2015_arrow 72714 ns/iter (± 192) 73292 ns/iter (± 237) 0.99
es2015_block_scoped_fn 68552 ns/iter (± 267) 69593 ns/iter (± 304) 0.99
es2015_block_scoping 118412 ns/iter (± 442) 121628 ns/iter (± 528) 0.97

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

Please sign in to comment.