Skip to content

Commit

Permalink
feat(es/minifier): Handle more indexing expression (#8750)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8747
  • Loading branch information
levi-nz authored Jul 2, 2024
1 parent fb3f045 commit 570c47a
Show file tree
Hide file tree
Showing 55 changed files with 1,245 additions and 274 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//// [destructuringArrayBindingPatternAndAssignment2.ts]
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
var _ref_1 = (_sliced_to_array([][0], 1)[0], _sliced_to_array([][1], 1));
var _ref_1 = (_sliced_to_array(void 0, 1)[0], _sliced_to_array(void 0, 1));
_sliced_to_array(_ref_1[0], 1)[0];
var _undefined = _sliced_to_array(void 0, 2), _undefined_1 = (_sliced_to_array(_undefined[0], 1)[0], _sliced_to_array(_undefined[1], 1));
_sliced_to_array(_undefined_1[0], 1)[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _to_property_key } from "@swc/helpers/_/_to_property_key";
var trace = [], order = function(n) {
return trace.push(n);
}, tmp = [][0];
}, tmp = void 0;
(void 0 === tmp ? order(0) : tmp)[order(1)];
var tmp1 = {};
(void 0 === tmp1 ? order(0) : tmp1)[order(1)];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ new Class(), d0({
y: new SubClass()
}).y, ({
y: new Class()
}).y, ({}).y;
}).y;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//// [parserForStatement9.ts]
for(var tmp = [][0], x = void 0 === tmp ? ('a' in {}) : tmp; !x; x = !x)console.log(x);
for(var _ref_x = {}.x, x1 = void 0 === _ref_x ? ('a' in {}) : _ref_x; !x1; x1 = !x1)console.log(x1);
for(var tmp = void 0, x = void 0 === tmp ? ('a' in {}) : tmp; !x; x = !x)console.log(x);
for(var _ref_x = void 0, x1 = void 0 === _ref_x ? ('a' in {}) : _ref_x; !x1; x1 = !x1)console.log(x1);
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
//// [templateStringInIndexExpressionES6.ts]
"abc0abc"["0"];
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Emu.D = function _class() {
_class_call_check(this, _class), this._model = 1;
};
//// [second.js]
({}).D._wrapperInstance;
(void 0)._wrapperInstance;
9 changes: 5 additions & 4 deletions crates/swc_ecma_minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ name = "swc_ecma_minifier"
repository = "https://github.com/swc-project/swc.git"
version = "0.197.1"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[lib]
bench = false
Expand Down Expand Up @@ -49,6 +49,7 @@ ryu-js = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tracing = { workspace = true }
phf = { workspace = true }

swc_atoms = { version = "0.6.5", path = "../swc_atoms" }
swc_common = { version = "0.34.0", path = "../swc_common" }
Expand Down Expand Up @@ -80,4 +81,4 @@ testing = { version = "0.36.0", path = "../testing" }

[[bench]]
harness = false
name = "full"
name = "full"
26 changes: 26 additions & 0 deletions crates/swc_ecma_minifier/src/compress/pure/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use radix_fmt::Radix;
use swc_common::{util::take::Take, Spanned, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_utils::{number::ToJsString, ExprExt, IsEmpty, Value};
#[cfg(feature = "debug")]
use {crate::debug::dump, tracing::debug};

use super::Pure;
use crate::compress::util::{eval_as_number, is_pure_undefined_or_null};
Expand Down Expand Up @@ -639,6 +641,30 @@ impl Pure<'_> {
}
}

pub(super) fn eval_member_expr(&mut self, e: &mut Expr) {
let member_expr = match e {
Expr::Member(x) => x,
_ => return,
};

#[cfg(feature = "debug")]
debug!(
"before: optimize_member_expr: {}",
dump(&*member_expr, false)
);

if let Some(replacement) =
self.optimize_member_expr(&mut member_expr.obj, &member_expr.prop)
{
*e = replacement;
self.changed = true;
report_change!("member_expr: Optimized member expression");

#[cfg(feature = "debug")]
debug!("after: optimize_member_expr: {}", dump(&*e, false));
}
}

fn eval_trivial_two(&mut self, a: &Expr, b: &mut Expr) {
if let Expr::Assign(AssignExpr {
left: a_left,
Expand Down
Loading

1 comment on commit 570c47a

@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: 570c47a Previous: 72021de Ratio
es/full/bugs-1 297662 ns/iter (± 12253) 271524 ns/iter (± 5144) 1.10
es/full/minify/libraries/antd 1877015936 ns/iter (± 76744836) 1972320497 ns/iter (± 44760327) 0.95
es/full/minify/libraries/d3 346053793 ns/iter (± 2356409) 364460341 ns/iter (± 3783037) 0.95
es/full/minify/libraries/echarts 1541989214 ns/iter (± 38848508) 1573283082 ns/iter (± 61406967) 0.98
es/full/minify/libraries/jquery 103358081 ns/iter (± 356754) 102932139 ns/iter (± 399880) 1.00
es/full/minify/libraries/lodash 114693151 ns/iter (± 311141) 113684561 ns/iter (± 5222504) 1.01
es/full/minify/libraries/moment 59668213 ns/iter (± 214356) 59606558 ns/iter (± 169781) 1.00
es/full/minify/libraries/react 18411772 ns/iter (± 30537) 18324796 ns/iter (± 34496) 1.00
es/full/minify/libraries/terser 275783975 ns/iter (± 2774137) 280056428 ns/iter (± 4055851) 0.98
es/full/minify/libraries/three 471612000 ns/iter (± 4173139) 477273833 ns/iter (± 4875234) 0.99
es/full/minify/libraries/typescript 3249990102 ns/iter (± 21877822) 3258055504 ns/iter (± 28032949) 1.00
es/full/minify/libraries/victory 744968486 ns/iter (± 8841573) 739959873 ns/iter (± 14613604) 1.01
es/full/minify/libraries/vue 146080465 ns/iter (± 1245245) 146498471 ns/iter (± 607108) 1.00
es/full/codegen/es3 59657 ns/iter (± 239) 57646 ns/iter (± 200) 1.03
es/full/codegen/es5 59917 ns/iter (± 189) 58010 ns/iter (± 234) 1.03
es/full/codegen/es2015 59636 ns/iter (± 133) 57604 ns/iter (± 178) 1.04
es/full/codegen/es2016 59648 ns/iter (± 146) 57881 ns/iter (± 225) 1.03
es/full/codegen/es2017 59918 ns/iter (± 150) 57679 ns/iter (± 201) 1.04
es/full/codegen/es2018 59562 ns/iter (± 88) 57748 ns/iter (± 284) 1.03
es/full/codegen/es2019 59645 ns/iter (± 220) 57729 ns/iter (± 123) 1.03
es/full/codegen/es2020 59750 ns/iter (± 144) 57769 ns/iter (± 891) 1.03
es/full/all/es3 160357434 ns/iter (± 759365) 162947325 ns/iter (± 652713) 0.98
es/full/all/es5 154398073 ns/iter (± 772187) 157896613 ns/iter (± 875741) 0.98
es/full/all/es2015 114182513 ns/iter (± 518904) 116741816 ns/iter (± 4398266) 0.98
es/full/all/es2016 113849418 ns/iter (± 598831) 115893272 ns/iter (± 906602) 0.98
es/full/all/es2017 112994925 ns/iter (± 537575) 115229758 ns/iter (± 575122) 0.98
es/full/all/es2018 111105057 ns/iter (± 791068) 112942626 ns/iter (± 861306) 0.98
es/full/all/es2019 110571982 ns/iter (± 542720) 112501031 ns/iter (± 824366) 0.98
es/full/all/es2020 106082289 ns/iter (± 756914) 108760770 ns/iter (± 808861) 0.98
es/full/parser 452140 ns/iter (± 1338) 477279 ns/iter (± 2715) 0.95
es/full/base/fixer 15391 ns/iter (± 149) 15782 ns/iter (± 106) 0.98
es/full/base/resolver_and_hygiene 78623 ns/iter (± 165) 80422 ns/iter (± 369) 0.98
serialization of serde 272 ns/iter (± 1) 277 ns/iter (± 5) 0.98
css/minify/libraries/bootstrap 21986154 ns/iter (± 104723) 22173833 ns/iter (± 115040) 0.99
css/visitor/compare/clone 1372951 ns/iter (± 10007) 1367445 ns/iter (± 15892) 1.00
css/visitor/compare/visit_mut_span 1498999 ns/iter (± 21103) 1483294 ns/iter (± 13036) 1.01
css/visitor/compare/visit_mut_span_panic 1512185 ns/iter (± 20445) 1508233 ns/iter (± 37740) 1.00
css/visitor/compare/fold_span 2025190 ns/iter (± 21264) 1998847 ns/iter (± 8077) 1.01
css/visitor/compare/fold_span_panic 2156853 ns/iter (± 16151) 2137787 ns/iter (± 4608) 1.01
css/lexer/bootstrap_5_1_3 3515305 ns/iter (± 6460) 3457409 ns/iter (± 14762) 1.02
css/lexer/foundation_6_7_4 2842067 ns/iter (± 5792) 2796574 ns/iter (± 4750) 1.02
css/lexer/tailwind_3_1_1 544314 ns/iter (± 4721) 535378 ns/iter (± 965) 1.02
css/parser/bootstrap_5_1_3 16338405 ns/iter (± 84176) 16071300 ns/iter (± 41198) 1.02
css/parser/foundation_6_7_4 12990508 ns/iter (± 57261) 12850239 ns/iter (± 518418) 1.01
css/parser/tailwind_3_1_1 2505402 ns/iter (± 32550) 2553433 ns/iter (± 1433) 0.98
es/codegen/colors 730347 ns/iter (± 399814) 715718 ns/iter (± 392110) 1.02
es/codegen/large 3010469 ns/iter (± 1633832) 2872136 ns/iter (± 1567307) 1.05
es/codegen/with-parser/colors 40312 ns/iter (± 407) 42623 ns/iter (± 279) 0.95
es/codegen/with-parser/large 455341 ns/iter (± 2034) 472727 ns/iter (± 3781) 0.96
es/minify/libraries/antd 1587009729 ns/iter (± 44599895) 1414912541 ns/iter (± 24070671) 1.12
es/minify/libraries/d3 301243366 ns/iter (± 13111356) 279806368 ns/iter (± 1233674) 1.08
es/minify/libraries/echarts 1314343298 ns/iter (± 22246190) 1142665046 ns/iter (± 8049108) 1.15
es/minify/libraries/jquery 84129017 ns/iter (± 866104) 82310561 ns/iter (± 384768) 1.02
es/minify/libraries/lodash 105688667 ns/iter (± 811831) 105063406 ns/iter (± 2616340) 1.01
es/minify/libraries/moment 48872927 ns/iter (± 625980) 48368388 ns/iter (± 179356) 1.01
es/minify/libraries/react 16278739 ns/iter (± 54997) 16551085 ns/iter (± 60396) 0.98
es/minify/libraries/terser 230143688 ns/iter (± 4318135) 221468316 ns/iter (± 1833435) 1.04
es/minify/libraries/three 406067920 ns/iter (± 8039106) 438267567 ns/iter (± 14441075) 0.93
es/minify/libraries/typescript 2932847097 ns/iter (± 22423918) 2886370341 ns/iter (± 20972344) 1.02
es/minify/libraries/victory 718060024 ns/iter (± 16702906) 665944661 ns/iter (± 23666504) 1.08
es/minify/libraries/vue 125015161 ns/iter (± 940916) 122872661 ns/iter (± 1432037) 1.02
es/visitor/compare/clone 1732733 ns/iter (± 8049) 1874666 ns/iter (± 19678) 0.92
es/visitor/compare/visit_mut_span 2080969 ns/iter (± 59072) 2263643 ns/iter (± 28527) 0.92
es/visitor/compare/visit_mut_span_panic 2072925 ns/iter (± 46530) 2251252 ns/iter (± 80084) 0.92
es/visitor/compare/fold_span 2792637 ns/iter (± 13087) 3019248 ns/iter (± 30380) 0.92
es/visitor/compare/fold_span_panic 2818249 ns/iter (± 10158) 3088356 ns/iter (± 43512) 0.91
es/lexer/colors 9742 ns/iter (± 78) 9750 ns/iter (± 209) 1.00
es/lexer/angular 4966038 ns/iter (± 26886) 5166637 ns/iter (± 49110) 0.96
es/lexer/backbone 634486 ns/iter (± 632) 675962 ns/iter (± 7628) 0.94
es/lexer/jquery 3716272 ns/iter (± 23914) 3812012 ns/iter (± 31917) 0.97
es/lexer/jquery mobile 5467676 ns/iter (± 36885) 5665483 ns/iter (± 62693) 0.97
es/lexer/mootools 2893554 ns/iter (± 4526) 2982346 ns/iter (± 22800) 0.97
es/lexer/underscore 528952 ns/iter (± 8601) 556604 ns/iter (± 4153) 0.95
es/lexer/three 16380829 ns/iter (± 67346) 16711324 ns/iter (± 129650) 0.98
es/lexer/yui 2909293 ns/iter (± 4213) 2978973 ns/iter (± 10647) 0.98
es/lexer/cal-com 13337547 ns/iter (± 37142) 13664471 ns/iter (± 50148) 0.98
es/lexer/typescript 91857057 ns/iter (± 518230) 96255669 ns/iter (± 583731) 0.95
es/parser/colors 23865 ns/iter (± 414) 24782 ns/iter (± 230) 0.96
es/parser/angular 12493571 ns/iter (± 93184) 13578513 ns/iter (± 286663) 0.92
es/parser/backbone 1839773 ns/iter (± 23053) 1929620 ns/iter (± 22168) 0.95
es/parser/jquery 10139396 ns/iter (± 83413) 10679140 ns/iter (± 226899) 0.95
es/parser/jquery mobile 15624488 ns/iter (± 136014) 16035610 ns/iter (± 149650) 0.97
es/parser/mootools 7933714 ns/iter (± 76865) 8280698 ns/iter (± 91738) 0.96
es/parser/underscore 1597918 ns/iter (± 7257) 1654689 ns/iter (± 12962) 0.97
es/parser/three 44364553 ns/iter (± 685079) 54850552 ns/iter (± 1900653) 0.81
es/parser/yui 7542908 ns/iter (± 35073) 7938074 ns/iter (± 82752) 0.95
es/parser/cal-com 39237221 ns/iter (± 311612) 46574014 ns/iter (± 2189089) 0.84
es/parser/typescript 267115840 ns/iter (± 1288514) 351312226 ns/iter (± 7152242) 0.76
es/preset-env/usage/builtin_type 136160 ns/iter (± 33412) 138135 ns/iter (± 33521) 0.99
es/preset-env/usage/property 16029 ns/iter (± 111) 15999 ns/iter (± 52) 1.00
es/resolver/typescript 98824797 ns/iter (± 2565591) 101476837 ns/iter (± 4263386) 0.97
es/fixer/typescript 76179757 ns/iter (± 4028413) 83822747 ns/iter (± 1209348) 0.91
es/hygiene/typescript 175387503 ns/iter (± 3948242) 181247857 ns/iter (± 3490947) 0.97
es/resolver_with_hygiene/typescript 299384362 ns/iter (± 4756992) 308093764 ns/iter (± 2468624) 0.97
es/visitor/base-perf/module_clone 56208 ns/iter (± 516) 57371 ns/iter (± 465) 0.98
es/visitor/base-perf/fold_empty 59988 ns/iter (± 951) 60404 ns/iter (± 579) 0.99
es/visitor/base-perf/fold_noop_impl_all 60341 ns/iter (± 550) 60493 ns/iter (± 455) 1.00
es/visitor/base-perf/fold_noop_impl_vec 59970 ns/iter (± 585) 60480 ns/iter (± 683) 0.99
es/visitor/base-perf/boxing_boxed_clone 59 ns/iter (± 0) 60 ns/iter (± 0) 0.98
es/visitor/base-perf/boxing_unboxed_clone 47 ns/iter (± 0) 47 ns/iter (± 0) 1
es/visitor/base-perf/boxing_boxed 105 ns/iter (± 0) 106 ns/iter (± 0) 0.99
es/visitor/base-perf/boxing_unboxed 81 ns/iter (± 0) 81 ns/iter (± 0) 1
es/visitor/base-perf/visit_contains_this 2577 ns/iter (± 5) 2553 ns/iter (± 14) 1.01
es/base/parallel/resolver/typescript 2449410446 ns/iter (± 98162962) 2492870513 ns/iter (± 203658326) 0.98
es/base/parallel/hygiene/typescript 3166931365 ns/iter (± 63006068) 3234292144 ns/iter (± 57805393) 0.98
babelify-only 506422 ns/iter (± 7886) 495413 ns/iter (± 14393) 1.02
parse_and_babelify_angular 41160821 ns/iter (± 483751) 39465892 ns/iter (± 3274611) 1.04
parse_and_babelify_backbone 4457359 ns/iter (± 118769) 4342691 ns/iter (± 179282) 1.03
parse_and_babelify_jquery 30562371 ns/iter (± 188942) 28366112 ns/iter (± 234120) 1.08
parse_and_babelify_jquery_mobile 54311679 ns/iter (± 459923) 49576024 ns/iter (± 921956) 1.10
parse_and_babelify_mootools 28450237 ns/iter (± 560255) 25989630 ns/iter (± 323282) 1.09
parse_and_babelify_underscore 3757872 ns/iter (± 25614) 3659551 ns/iter (± 21596) 1.03
parse_and_babelify_yui 29608253 ns/iter (± 1420375) 24766075 ns/iter (± 536094) 1.20
html/minify/document/css_spec 45568972 ns/iter (± 341897) 45902773 ns/iter (± 2524738) 0.99
html/minify/document/github 18101946 ns/iter (± 52551) 18099531 ns/iter (± 219157) 1.00
html/minify/document/stackoverflow 16468544 ns/iter (± 630142) 16791927 ns/iter (± 306988) 0.98
html/minify/document_fragment/css_spec 45210778 ns/iter (± 490688) 44613332 ns/iter (± 523328) 1.01
html/minify/document_fragment/github 17931927 ns/iter (± 184337) 17951449 ns/iter (± 146035) 1.00
html/minify/document_fragment/stackoverflow 16261688 ns/iter (± 52348) 16389425 ns/iter (± 278288) 0.99
html/document/visitor/compare/clone 167025 ns/iter (± 1989) 166259 ns/iter (± 1541) 1.00
html/document/visitor/compare/visit_mut_span 183322 ns/iter (± 1414) 183874 ns/iter (± 1527) 1.00
html/document/visitor/compare/visit_mut_span_panic 186751 ns/iter (± 2927) 190046 ns/iter (± 2901) 0.98
html/document/visitor/compare/fold_span 229986 ns/iter (± 1470) 230600 ns/iter (± 972) 1.00
html/document/visitor/compare/fold_span_panic 280487 ns/iter (± 759) 279523 ns/iter (± 3001) 1.00
html/document_fragment/visitor/compare/clone 168279 ns/iter (± 855) 171313 ns/iter (± 4198) 0.98
html/document_fragment/visitor/compare/visit_mut_span 182792 ns/iter (± 896) 184542 ns/iter (± 792) 0.99
html/document_fragment/visitor/compare/visit_mut_span_panic 187870 ns/iter (± 1107) 187954 ns/iter (± 644) 1.00
html/document_fragment/visitor/compare/fold_span 228197 ns/iter (± 1650) 229181 ns/iter (± 1725) 1.00
html/document_fragment/visitor/compare/fold_span_panic 277037 ns/iter (± 787) 280705 ns/iter (± 1573) 0.99
html/lexer/css_2021_spec 12830540 ns/iter (± 23445) 12797658 ns/iter (± 114536) 1.00
html/lexer/github_com_17_05_2022 4930457 ns/iter (± 21544) 4911636 ns/iter (± 80173) 1.00
html/lexer/stackoverflow_com_17_05_2022 4678445 ns/iter (± 58347) 4662069 ns/iter (± 5695) 1.00
html/parser/parser_document/css_2021_spec 25709382 ns/iter (± 68347) 25254353 ns/iter (± 316240) 1.02
html/parser/parser_document/github_com_17_05_2022 8560765 ns/iter (± 18321) 8364899 ns/iter (± 15102) 1.02
html/parser/parser_document/stackoverflow_com_17_05_2022 7543387 ns/iter (± 27364) 7334983 ns/iter (± 21969) 1.03
html/parser/parser_document_fragment/css_2021_spec 26562967 ns/iter (± 39932) 24955204 ns/iter (± 73293) 1.06
html/parser/parser_document_fragment/github_com_17_05_2022 8819371 ns/iter (± 29461) 8366364 ns/iter (± 10557) 1.05
html/parser/parser_document_fragment/stackoverflow_com_17_05_2022 7937705 ns/iter (± 25006) 7328629 ns/iter (± 17170) 1.08

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

Please sign in to comment.