Skip to content

Commit

Permalink
refactor(es/minifier): Remove mutated and mutation_by_call_count (#…
Browse files Browse the repository at this point in the history
…7890)

**Description:**

`mutated` can be seen as a combination of `reassigned` and `has_property_mutation`, and `mutation_by_call_count` is simply useless.
  • Loading branch information
Austaras committed Aug 30, 2023
1 parent 853b480 commit 8db968a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 46 deletions.
11 changes: 5 additions & 6 deletions crates/swc_ecma_minifier/src/compress/optimize/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ impl Optimizer<'_> {
//
// TODO: Allow `length` in usage.accessed_props
if usage.declared
&& !usage.reassigned
&& !usage.mutated
&& !usage.has_property_mutation
&& !usage.mutated()
&& usage.accessed_props.is_empty()
&& !usage.is_infected()
&& is_inline_enabled
Expand Down Expand Up @@ -165,7 +163,7 @@ impl Optimizer<'_> {
}
}

if !usage.mutated {
if !usage.mutated() {
self.mode.store(ident.to_id(), &*init);
}

Expand All @@ -177,7 +175,8 @@ impl Optimizer<'_> {
// new variant is added for multi inline, think carefully
if is_inline_enabled
&& usage.declared_count == 1
&& usage.can_inline_var()
&& usage.assign_count == 0
&& (!usage.has_property_mutation || !usage.reassigned)
&& match init {
Expr::Ident(Ident {
sym: js_word!("eval"),
Expand Down Expand Up @@ -323,7 +322,7 @@ impl Optimizer<'_> {
&& usage.declared
&& may_remove
&& !usage.reassigned
&& (usage.can_inline_var() || usage.is_mutated_only_by_one_call())
&& usage.assign_count == 0
&& ref_count == 1
{
match init {
Expand Down
7 changes: 2 additions & 5 deletions crates/swc_ecma_minifier/src/compress/optimize/props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ impl Optimizer<'_> {
.vars
.get(&name.to_id())
.map(|v| {
!v.mutated
&& v.mutation_by_call_count == 0
!v.mutated()
&& !v.used_as_ref
&& !v.used_as_arg
&& !v.used_in_cond
&& (!v.is_fn_local || !self.mode.should_be_very_correct())
&& !v.reassigned
&& !v.is_infected()
})
.unwrap_or(false)
Expand Down Expand Up @@ -153,8 +151,7 @@ impl Optimizer<'_> {
.map(|v| {
v.ref_count == 1
&& v.has_property_access
&& !v.mutated
&& v.mutation_by_call_count == 0
&& !v.mutated()
&& v.is_fn_local
&& !v.executed_multiple_time
&& !v.used_as_arg
Expand Down
3 changes: 1 addition & 2 deletions crates/swc_ecma_minifier/src/compress/optimize/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ impl Optimizer<'_> {
return true;
}

if !usage.mutated && !usage.reassigned && usage.no_side_effect_for_member_access
{
if !usage.mutated() && usage.no_side_effect_for_member_access {
return false;
}
}
Expand Down
37 changes: 9 additions & 28 deletions crates/swc_ecma_minifier/src/program_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ pub(crate) struct VarUsageInfo {
pub(crate) declared_as_for_init: bool,

pub(crate) assign_count: u32,
pub(crate) mutation_by_call_count: u32,

/// The number of direct and indirect reference to this identifier.
/// ## Things to note
Expand All @@ -75,8 +74,6 @@ pub(crate) struct VarUsageInfo {

/// The variable itself is assigned after reference.
pub(crate) reassigned: bool,
/// The variable itself or a property of it is modified.
pub(crate) mutated: bool,

pub(crate) has_property_access: bool,
pub(crate) has_property_mutation: bool,
Expand Down Expand Up @@ -139,10 +136,8 @@ impl Default for VarUsageInfo {
declared_as_fn_expr: Default::default(),
declared_as_for_init: Default::default(),
assign_count: Default::default(),
mutation_by_call_count: Default::default(),
usage_count: Default::default(),
reassigned: Default::default(),
mutated: Default::default(),
has_property_access: Default::default(),
has_property_mutation: Default::default(),
exported: Default::default(),
Expand Down Expand Up @@ -170,16 +165,13 @@ impl Default for VarUsageInfo {
}

impl VarUsageInfo {
pub(crate) fn is_mutated_only_by_one_call(&self) -> bool {
self.assign_count == 0 && self.mutation_by_call_count == 1
}

pub(crate) fn is_infected(&self) -> bool {
!self.infects_to.is_empty()
}

pub(crate) fn can_inline_var(&self) -> bool {
!self.mutated || (self.assign_count == 0 && !self.reassigned)
/// The variable itself or a property of it is modified.
pub(crate) fn mutated(&self) -> bool {
self.assign_count > 0 || self.has_property_mutation
}

pub(crate) fn can_inline_fn_once(&self) -> bool {
Expand Down Expand Up @@ -230,6 +222,8 @@ impl Storage for ProgramData {
|| (var_info.var_initialized && !e.get().var_initialized);

if var_info.var_initialized {
// If it is inited in some other child scope and also inited in current
// scope
if e.get().var_initialized || e.get().ref_count > 0 {
e.get_mut().assign_count += 1;
e.get_mut().reassigned = true;
Expand All @@ -242,6 +236,8 @@ impl Storage for ProgramData {
// If it is inited in some other child scope, but referenced in
// current child scope
if !inited && e.get().var_initialized && var_info.ref_count > 0 {
e.get_mut().var_initialized = false;
e.get_mut().assign_count += 1;
e.get_mut().reassigned = true
}
}
Expand All @@ -256,8 +252,6 @@ impl Storage for ProgramData {
}
}

e.get_mut().mutated |= var_info.mutated;

e.get_mut().has_property_access |= var_info.has_property_access;
e.get_mut().has_property_mutation |= var_info.has_property_mutation;
e.get_mut().exported |= var_info.exported;
Expand All @@ -276,7 +270,6 @@ impl Storage for ProgramData {
e.get_mut().executed_multiple_time |= var_info.executed_multiple_time;
e.get_mut().used_in_cond |= var_info.used_in_cond;
e.get_mut().assign_count += var_info.assign_count;
e.get_mut().mutation_by_call_count += var_info.mutation_by_call_count;
e.get_mut().usage_count += var_info.usage_count;

e.get_mut().infects_to.extend(var_info.infects_to);
Expand Down Expand Up @@ -362,7 +355,6 @@ impl Storage for ProgramData {
tracing::trace!("declare_decl(`{}`): Already declared", i);
}

v.mutated = true;
v.reassigned = true;
v.assign_count += 1;
}
Expand Down Expand Up @@ -398,7 +390,7 @@ impl Storage for ProgramData {
self.initialized_vars.truncate(len)
}

fn mark_property_mutattion(&mut self, id: Id, ctx: Ctx) {
fn mark_property_mutation(&mut self, id: Id, ctx: Ctx) {
let e = self.vars.entry(id).or_default();
e.has_property_mutation = true;

Expand Down Expand Up @@ -484,10 +476,6 @@ impl VarDataLike for VarUsageInfo {
*self.accessed_props.entry(name).or_default() += 1;
}

fn mark_mutated(&mut self) {
self.mutated = true;
}

fn mark_used_as_ref(&mut self) {
self.used_as_ref = true;
}
Expand Down Expand Up @@ -610,9 +598,6 @@ impl ProgramData {

let call_may_mutate = ctx.in_call_arg_of == Some(CalleeKind::Unknown);

// Passing object as a argument is possibly modification.
e.mutated |= is_modify || (call_may_mutate && ctx.is_exact_arg);

e.executed_multiple_time |= ctx.executed_multiple_time;
e.used_in_cond |= ctx.in_cond;

Expand Down Expand Up @@ -647,15 +632,11 @@ impl ProgramData {
self.report(other.0, ctx, true, dejavu)
}
} else {
if call_may_mutate && ctx.is_exact_arg {
e.mutation_by_call_count += 1;
}

e.usage_count += 1;
}

if call_may_mutate && ctx.is_exact_arg {
self.mark_property_mutattion(i, ctx)
self.mark_property_mutation(i, ctx)
}
}
}
4 changes: 2 additions & 2 deletions crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,8 +919,8 @@ where
v.add_accessed_property(prop.sym.clone());
}

if self.ctx.in_assign_lhs || self.ctx.is_delete_arg {
self.data.mark_property_mutattion(obj.to_id(), self.ctx)
if self.ctx.in_assign_lhs || self.ctx.in_update_arg || self.ctx.is_delete_arg {
self.data.mark_property_mutation(obj.to_id(), self.ctx)
}
})
}
Expand Down
4 changes: 1 addition & 3 deletions crates/swc_ecma_usage_analyzer/src/analyzer/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub trait Storage: Sized + Default {
fn get_initialized_cnt(&self) -> usize;
fn truncate_initialized_cnt(&mut self, len: usize);

fn mark_property_mutattion(&mut self, id: Id, ctx: Ctx);
fn mark_property_mutation(&mut self, id: Id, ctx: Ctx);
}

pub trait ScopeDataLike: Sized + Default + Clone {
Expand Down Expand Up @@ -65,8 +65,6 @@ pub trait VarDataLike: Sized {

fn add_accessed_property(&mut self, name: JsWord);

fn mark_mutated(&mut self);

fn mark_used_as_ref(&mut self);

fn add_infects_to(&mut self, other: Access);
Expand Down

1 comment on commit 8db968a

@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: 8db968a Previous: 01cbd6e Ratio
es/full/bugs-1 289587 ns/iter (± 8574) 281948 ns/iter (± 5522) 1.03
es/full/minify/libraries/antd 1307477861 ns/iter (± 14777675) 1277436742 ns/iter (± 11914269) 1.02
es/full/minify/libraries/d3 271250776 ns/iter (± 3773463) 272742982 ns/iter (± 5011878) 0.99
es/full/minify/libraries/echarts 1037081860 ns/iter (± 5370837) 1029168289 ns/iter (± 8724141) 1.01
es/full/minify/libraries/jquery 83329372 ns/iter (± 148904) 83069298 ns/iter (± 145405) 1.00
es/full/minify/libraries/lodash 96864275 ns/iter (± 256435) 95976469 ns/iter (± 234333) 1.01
es/full/minify/libraries/moment 49311457 ns/iter (± 76661) 49221386 ns/iter (± 191493) 1.00
es/full/minify/libraries/react 17904183 ns/iter (± 42696) 17820916 ns/iter (± 40222) 1.00
es/full/minify/libraries/terser 215382555 ns/iter (± 473389) 212977185 ns/iter (± 437593) 1.01
es/full/minify/libraries/three 381401157 ns/iter (± 4094636) 377781880 ns/iter (± 1573365) 1.01
es/full/minify/libraries/typescript 2617597398 ns/iter (± 10208844) 2542358645 ns/iter (± 17115133) 1.03
es/full/minify/libraries/victory 562203434 ns/iter (± 4807051) 539938601 ns/iter (± 3428657) 1.04
es/full/minify/libraries/vue 117887359 ns/iter (± 284349) 117099013 ns/iter (± 235679) 1.01
es/full/codegen/es3 34940 ns/iter (± 76) 35928 ns/iter (± 63) 0.97
es/full/codegen/es5 34886 ns/iter (± 88) 35926 ns/iter (± 93) 0.97
es/full/codegen/es2015 34987 ns/iter (± 130) 36107 ns/iter (± 66) 0.97
es/full/codegen/es2016 34972 ns/iter (± 152) 36205 ns/iter (± 105) 0.97
es/full/codegen/es2017 35097 ns/iter (± 82) 35814 ns/iter (± 60) 0.98
es/full/codegen/es2018 35018 ns/iter (± 147) 36042 ns/iter (± 75) 0.97
es/full/codegen/es2019 34918 ns/iter (± 83) 35898 ns/iter (± 90) 0.97
es/full/codegen/es2020 34927 ns/iter (± 140) 36022 ns/iter (± 62) 0.97
es/full/all/es3 171439564 ns/iter (± 1323992) 168222297 ns/iter (± 1495192) 1.02
es/full/all/es5 163324140 ns/iter (± 958337) 160440620 ns/iter (± 1409188) 1.02
es/full/all/es2015 121943894 ns/iter (± 730798) 120197991 ns/iter (± 379301) 1.01
es/full/all/es2016 121238720 ns/iter (± 783695) 119663237 ns/iter (± 947790) 1.01
es/full/all/es2017 120031820 ns/iter (± 572314) 119109973 ns/iter (± 1125763) 1.01
es/full/all/es2018 118231755 ns/iter (± 738093) 115996519 ns/iter (± 943052) 1.02
es/full/all/es2019 117527678 ns/iter (± 292078) 116011860 ns/iter (± 632826) 1.01
es/full/all/es2020 112782526 ns/iter (± 384914) 112006871 ns/iter (± 873785) 1.01
es/full/parser 498673 ns/iter (± 6036) 488089 ns/iter (± 5901) 1.02
es/full/base/fixer 18329 ns/iter (± 89) 19082 ns/iter (± 322) 0.96
es/full/base/resolver_and_hygiene 80975 ns/iter (± 153) 80384 ns/iter (± 124) 1.01
serialization of serde 279 ns/iter (± 0) 293 ns/iter (± 0) 0.95
css/minify/libraries/bootstrap 29235806 ns/iter (± 82142) 29132169 ns/iter (± 214820) 1.00
css/visitor/compare/clone 1616969 ns/iter (± 2615) 1627331 ns/iter (± 3044) 0.99
css/visitor/compare/visit_mut_span 1751052 ns/iter (± 2290) 1738909 ns/iter (± 1994) 1.01
css/visitor/compare/visit_mut_span_panic 1829270 ns/iter (± 4554) 1814185 ns/iter (± 7414) 1.01
css/visitor/compare/fold_span 2545057 ns/iter (± 5153) 2559281 ns/iter (± 8221) 0.99
css/visitor/compare/fold_span_panic 2750558 ns/iter (± 7230) 2751150 ns/iter (± 17401) 1.00
css/lexer/bootstrap_5_1_3 4525500 ns/iter (± 2657) 4499476 ns/iter (± 3920) 1.01
css/lexer/foundation_6_7_4 3808307 ns/iter (± 4212) 3790690 ns/iter (± 4063) 1.00
css/lexer/tailwind_3_1_1 721185 ns/iter (± 1147) 722963 ns/iter (± 594) 1.00
css/parser/bootstrap_5_1_3 19413869 ns/iter (± 41114) 19145115 ns/iter (± 37485) 1.01
css/parser/foundation_6_7_4 15369993 ns/iter (± 102350) 15314673 ns/iter (± 22467) 1.00
css/parser/tailwind_3_1_1 2985325 ns/iter (± 13871) 2959832 ns/iter (± 2890) 1.01
es/codegen/colors 732611 ns/iter (± 398111) 729812 ns/iter (± 397874) 1.00
es/codegen/large 3115710 ns/iter (± 1640400) 3138673 ns/iter (± 1667753) 0.99
es/codegen/with-parser/colors 45102 ns/iter (± 523) 46131 ns/iter (± 287) 0.98
es/codegen/with-parser/large 483670 ns/iter (± 1453) 489422 ns/iter (± 2647) 0.99
es/minify/libraries/antd 1125297877 ns/iter (± 9385319) 1110878275 ns/iter (± 7258943) 1.01
es/minify/libraries/d3 237257710 ns/iter (± 635245) 236273763 ns/iter (± 448167) 1.00
es/minify/libraries/echarts 888334156 ns/iter (± 6011628) 880656712 ns/iter (± 2092251) 1.01
es/minify/libraries/jquery 72979823 ns/iter (± 369390) 72290705 ns/iter (± 135303) 1.01
es/minify/libraries/lodash 86362206 ns/iter (± 235520) 85888640 ns/iter (± 175093) 1.01
es/minify/libraries/moment 43260398 ns/iter (± 74805) 42795983 ns/iter (± 59757) 1.01
es/minify/libraries/react 15919936 ns/iter (± 55180) 15817905 ns/iter (± 62852) 1.01
es/minify/libraries/terser 184416417 ns/iter (± 709909) 182653172 ns/iter (± 379202) 1.01
es/minify/libraries/three 317637269 ns/iter (± 1348713) 316773829 ns/iter (± 2124570) 1.00
es/minify/libraries/typescript 2218603398 ns/iter (± 10102966) 2200242761 ns/iter (± 10483700) 1.01
es/minify/libraries/victory 466636885 ns/iter (± 1138608) 463042365 ns/iter (± 1113759) 1.01
es/minify/libraries/vue 105641780 ns/iter (± 217258) 104390355 ns/iter (± 241830) 1.01
es/visitor/compare/clone 1913896 ns/iter (± 4654) 1925817 ns/iter (± 3444) 0.99
es/visitor/compare/visit_mut_span 2264193 ns/iter (± 6331) 2267687 ns/iter (± 4626) 1.00
es/visitor/compare/visit_mut_span_panic 2295227 ns/iter (± 1571) 2308415 ns/iter (± 6022) 0.99
es/visitor/compare/fold_span 3312648 ns/iter (± 6625) 3318181 ns/iter (± 6296) 1.00
es/visitor/compare/fold_span_panic 3450762 ns/iter (± 6672) 3465311 ns/iter (± 6749) 1.00
es/lexer/colors 12849 ns/iter (± 25) 13342 ns/iter (± 32) 0.96
es/lexer/angular 6008034 ns/iter (± 20138) 6134446 ns/iter (± 2443) 0.98
es/lexer/backbone 777837 ns/iter (± 1571) 798192 ns/iter (± 2720) 0.97
es/lexer/jquery 4405178 ns/iter (± 14138) 4576939 ns/iter (± 9629) 0.96
es/lexer/jquery mobile 6702589 ns/iter (± 11719) 6858937 ns/iter (± 5422) 0.98
es/lexer/mootools 3494670 ns/iter (± 7183) 3566288 ns/iter (± 1798) 0.98
es/lexer/underscore 650313 ns/iter (± 3521) 664021 ns/iter (± 896) 0.98
es/lexer/three 20978090 ns/iter (± 62703) 21504268 ns/iter (± 21444) 0.98
es/lexer/yui 3753358 ns/iter (± 28118) 3832922 ns/iter (± 1953) 0.98
es/parser/colors 26720 ns/iter (± 308) 26593 ns/iter (± 86) 1.00
es/parser/angular 13273712 ns/iter (± 113613) 13265442 ns/iter (± 66471) 1.00
es/parser/backbone 1944602 ns/iter (± 26214) 1959546 ns/iter (± 9744) 0.99
es/parser/jquery 10607668 ns/iter (± 53661) 10673200 ns/iter (± 38449) 0.99
es/parser/jquery mobile 16542508 ns/iter (± 122488) 16416015 ns/iter (± 50979) 1.01
es/parser/mootools 8259020 ns/iter (± 19023) 8244232 ns/iter (± 20331) 1.00
es/parser/underscore 1701377 ns/iter (± 23374) 1686437 ns/iter (± 7724) 1.01
es/parser/three 46212831 ns/iter (± 216611) 46030796 ns/iter (± 116723) 1.00
es/parser/yui 8107859 ns/iter (± 68626) 8158490 ns/iter (± 38557) 0.99
es/preset-env/usage/builtin_type 136757 ns/iter (± 32693) 135446 ns/iter (± 31395) 1.01
es/preset-env/usage/property 17025 ns/iter (± 103) 17143 ns/iter (± 58) 0.99
es/resolver/typescript 87640517 ns/iter (± 848250) 86690693 ns/iter (± 950549) 1.01
es/fixer/typescript 62679504 ns/iter (± 310006) 62529148 ns/iter (± 281452) 1.00
es/hygiene/typescript 129548015 ns/iter (± 741274) 128828326 ns/iter (± 676726) 1.01
es/resolver_with_hygiene/typescript 241110678 ns/iter (± 750974) 237414774 ns/iter (± 820730) 1.02
es/visitor/base-perf/module_clone 60313 ns/iter (± 676) 61222 ns/iter (± 314) 0.99
es/visitor/base-perf/fold_empty 64412 ns/iter (± 387) 64167 ns/iter (± 592) 1.00
es/visitor/base-perf/fold_noop_impl_all 64461 ns/iter (± 221) 64393 ns/iter (± 219) 1.00
es/visitor/base-perf/fold_noop_impl_vec 64530 ns/iter (± 782) 64469 ns/iter (± 274) 1.00
es/visitor/base-perf/boxing_boxed_clone 56 ns/iter (± 0) 57 ns/iter (± 0) 0.98
es/visitor/base-perf/boxing_unboxed_clone 40 ns/iter (± 0) 39 ns/iter (± 0) 1.03
es/visitor/base-perf/boxing_boxed 107 ns/iter (± 0) 108 ns/iter (± 0) 0.99
es/visitor/base-perf/boxing_unboxed 77 ns/iter (± 0) 77 ns/iter (± 0) 1
es/visitor/base-perf/visit_empty 0 ns/iter (± 0) 0 ns/iter (± 0) NaN
es/visitor/base-perf/visit_contains_this 2526 ns/iter (± 23) 2527 ns/iter (± 10) 1.00
es/base/parallel/resolver/typescript 3622641874 ns/iter (± 287796129) 3871012312 ns/iter (± 305074011) 0.94
es/base/parallel/hygiene/typescript 1445046034 ns/iter (± 17075492) 1435491797 ns/iter (± 28997735) 1.01
misc/visitors/time-complexity/time 5 134 ns/iter (± 8) 130 ns/iter (± 8) 1.03
misc/visitors/time-complexity/time 10 393 ns/iter (± 2) 358 ns/iter (± 1) 1.10
misc/visitors/time-complexity/time 15 694 ns/iter (± 20) 557 ns/iter (± 0) 1.25
misc/visitors/time-complexity/time 20 1052 ns/iter (± 5) 1058 ns/iter (± 12) 0.99
misc/visitors/time-complexity/time 40 4189 ns/iter (± 12) 3563 ns/iter (± 9) 1.18
misc/visitors/time-complexity/time 60 7345 ns/iter (± 54) 7379 ns/iter (± 50) 1.00
es/full-target/es2016 230774 ns/iter (± 1006) 236152 ns/iter (± 564) 0.98
es/full-target/es2017 218817 ns/iter (± 1039) 224648 ns/iter (± 1839) 0.97
es/full-target/es2018 208454 ns/iter (± 1356) 212852 ns/iter (± 332) 0.98
es2020_nullish_coalescing 69998 ns/iter (± 382) 71949 ns/iter (± 298) 0.97
es2020_optional_chaining 81878 ns/iter (± 441) 81955 ns/iter (± 211) 1.00
es2022_class_properties 114499 ns/iter (± 437) 115085 ns/iter (± 296) 0.99
es2018_object_rest_spread 74621 ns/iter (± 466) 76148 ns/iter (± 822) 0.98
es2019_optional_catch_binding 65082 ns/iter (± 257) 64800 ns/iter (± 207) 1.00
es2017_async_to_generator 64465 ns/iter (± 394) 64969 ns/iter (± 172) 0.99
es2016_exponentiation 65744 ns/iter (± 294) 69425 ns/iter (± 311) 0.95
es2015_arrow 72143 ns/iter (± 455) 72290 ns/iter (± 181) 1.00
es2015_block_scoped_fn 69468 ns/iter (± 501) 68897 ns/iter (± 298) 1.01
es2015_block_scoping 123690 ns/iter (± 1072) 120124 ns/iter (± 350) 1.03

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

Please sign in to comment.