Skip to content

Commit 13106e0

Browse files
authoredOct 19, 2023
refactor(es/minifier): Decouple assign_count from reassigned (#8137)
1 parent a18ffc1 commit 13106e0

File tree

5 files changed

+35
-39
lines changed

5 files changed

+35
-39
lines changed
 

‎crates/swc_ecma_minifier/src/compress/optimize/inline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl Optimizer<'_> {
174174
// new variant is added for multi inline, think carefully
175175
if is_inline_enabled
176176
&& usage.declared_count == 1
177-
&& usage.assign_count == 0
177+
&& usage.assign_count == 1
178178
&& (!usage.has_property_mutation || !usage.reassigned)
179179
&& match init {
180180
Expr::Ident(Ident { sym, .. }) if &**sym == "eval" => false,
@@ -326,7 +326,7 @@ impl Optimizer<'_> {
326326
&& usage.declared
327327
&& may_remove
328328
&& !usage.reassigned
329-
&& usage.assign_count == 0
329+
&& usage.assign_count == 1
330330
&& ref_count == 1
331331
{
332332
match init {

‎crates/swc_ecma_minifier/src/compress/optimize/sequences.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2327,8 +2327,7 @@ impl Optimizer<'_> {
23272327
if let Some(usage) = self.data.vars.get(&left_id.to_id()) {
23282328
// We are eliminating one usage, so we use 1 instead of
23292329
// 0
2330-
if !$force_drop && usage.usage_count == 1 && usage.assign_count == 0
2331-
{
2330+
if !$force_drop && usage.usage_count == 1 && !usage.reassigned {
23322331
report_change!("sequences: Dropping inlined variable");
23332332
a.name.take();
23342333
}

‎crates/swc_ecma_minifier/src/program_data.rs

+20-23
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub(crate) struct VarUsageInfo {
6464

6565
pub(crate) declared_as_for_init: bool,
6666

67+
/// The number of assign and initialization to this identifier.
6768
pub(crate) assign_count: u32,
6869

6970
/// The number of direct and indirect reference to this identifier.
@@ -171,7 +172,7 @@ impl VarUsageInfo {
171172

172173
/// The variable itself or a property of it is modified.
173174
pub(crate) fn mutated(&self) -> bool {
174-
self.assign_count > 0 || self.has_property_mutation
175+
self.assign_count > 1 || self.has_property_mutation
175176
}
176177

177178
pub(crate) fn can_inline_fn_once(&self) -> bool {
@@ -221,11 +222,16 @@ impl Storage for ProgramData {
221222
let var_assigned = var_info.assign_count > 0
222223
|| (var_info.var_initialized && !e.get().var_initialized);
223224

225+
if var_info.assign_count > 0 {
226+
if e.get().initialized() {
227+
e.get_mut().reassigned = true
228+
}
229+
}
230+
224231
if var_info.var_initialized {
225232
// If it is inited in some other child scope and also inited in current
226233
// scope
227234
if e.get().var_initialized || e.get().ref_count > 0 {
228-
e.get_mut().assign_count += 1;
229235
e.get_mut().reassigned = true;
230236
} else {
231237
// If it is referred outside child scope, it will
@@ -237,7 +243,6 @@ impl Storage for ProgramData {
237243
// current child scope
238244
if !inited && e.get().var_initialized && var_info.ref_count > 0 {
239245
e.get_mut().var_initialized = false;
240-
e.get_mut().assign_count += 1;
241246
e.get_mut().reassigned = true
242247
}
243248
}
@@ -246,12 +251,6 @@ impl Storage for ProgramData {
246251

247252
e.get_mut().reassigned |= var_info.reassigned;
248253

249-
if var_info.assign_count > 0 {
250-
if e.get().initialized() {
251-
e.get_mut().reassigned = true
252-
}
253-
}
254-
255254
e.get_mut().has_property_access |= var_info.has_property_access;
256255
e.get_mut().has_property_mutation |= var_info.has_property_mutation;
257256
e.get_mut().exported |= var_info.exported;
@@ -349,13 +348,16 @@ impl Storage for ProgramData {
349348
v.is_top_level |= ctx.is_top_level;
350349

351350
// assigned or declared before this declaration
352-
if has_init && (v.declared || v.var_initialized || v.assign_count > 0) {
353-
#[cfg(feature = "debug")]
354-
{
355-
tracing::trace!("declare_decl(`{}`): Already declared", i);
351+
if has_init {
352+
if v.declared || v.var_initialized || v.assign_count > 0 {
353+
#[cfg(feature = "debug")]
354+
{
355+
tracing::trace!("declare_decl(`{}`): Already declared", i);
356+
}
357+
358+
v.reassigned = true;
356359
}
357360

358-
v.reassigned = true;
359361
v.assign_count += 1;
360362
}
361363

@@ -403,7 +405,7 @@ impl Storage for ProgramData {
403405

404406
for other in to_mark_mutate {
405407
let other = self.vars.entry(other).or_insert_with(|| {
406-
let simple_assign = ctx.is_exact_reassignment && !ctx.is_op_assign;
408+
let simple_assign = ctx.is_exact_assignment && !ctx.is_op_assign;
407409

408410
VarUsageInfo {
409411
used_above_decl: !simple_assign,
@@ -570,7 +572,7 @@ impl ProgramData {
570572
let e = self.vars.entry(i.clone()).or_insert_with(|| {
571573
// trace!("insert({}{:?})", i.0, i.1);
572574

573-
let simple_assign = ctx.is_exact_reassignment && !ctx.is_op_assign;
575+
let simple_assign = ctx.is_exact_assignment && !ctx.is_op_assign;
574576

575577
VarUsageInfo {
576578
used_above_decl: !simple_assign,
@@ -601,7 +603,7 @@ impl ProgramData {
601603
e.executed_multiple_time |= ctx.executed_multiple_time;
602604
e.used_in_cond |= ctx.in_cond;
603605

604-
if is_modify && ctx.is_exact_reassignment {
606+
if is_modify && ctx.is_exact_assignment {
605607
if is_first {
606608
if e.assign_count > 0 || e.initialized() {
607609
e.reassigned = true
@@ -610,13 +612,8 @@ impl ProgramData {
610612
e.assign_count += 1;
611613

612614
if !ctx.is_op_assign {
613-
if e.ref_count == 1
614-
&& ctx.in_assign_lhs
615-
&& e.var_kind != Some(VarDeclKind::Const)
616-
&& !inited
617-
{
615+
if e.ref_count == 1 && e.var_kind != Some(VarDeclKind::Const) && !inited {
618616
self.initialized_vars.insert(i.clone());
619-
e.assign_count -= 1;
620617
e.var_initialized = true;
621618
} else {
622619
e.reassigned = true

‎crates/swc_ecma_usage_analyzer/src/analyzer/ctx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub struct Ctx {
112112
pub in_pat_of_param: bool,
113113
pub in_catch_param: bool,
114114
/// `true` for `foo.bar` and `false` for `foo` in `foo.bar++`
115-
pub is_exact_reassignment: bool,
115+
pub is_exact_assignment: bool,
116116

117117
pub is_callee: bool,
118118

‎crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ where
218218
fn visit_assign_expr(&mut self, n: &AssignExpr) {
219219
let ctx = Ctx {
220220
in_assign_lhs: true,
221-
is_exact_reassignment: true,
221+
is_exact_assignment: true,
222222
is_op_assign: n.op != op!("="),
223223
is_delete_arg: false,
224224
..self.ctx
@@ -227,7 +227,7 @@ where
227227

228228
let ctx = Ctx {
229229
in_assign_lhs: false,
230-
is_exact_reassignment: false,
230+
is_exact_assignment: false,
231231
is_delete_arg: false,
232232
// We mark bar in
233233
//
@@ -407,7 +407,7 @@ where
407407
},
408408
is_delete_arg: false,
409409
is_exact_arg: true,
410-
is_exact_reassignment: false,
410+
is_exact_assignment: false,
411411
is_callee: false,
412412
is_id_ref: true,
413413
..self.ctx
@@ -750,7 +750,7 @@ where
750750

751751
self.with_child(n.span.ctxt, ScopeKind::Block, |child| {
752752
let ctx = Ctx {
753-
is_exact_reassignment: true,
753+
is_exact_assignment: true,
754754
is_delete_arg: false,
755755
in_left_of_for_loop: true,
756756
..child.ctx
@@ -777,7 +777,7 @@ where
777777
self.with_child(n.span.ctxt, ScopeKind::Block, |child| {
778778
let ctx = Ctx {
779779
in_left_of_for_loop: true,
780-
is_exact_reassignment: true,
780+
is_exact_assignment: true,
781781
is_delete_arg: false,
782782
..child.ctx
783783
};
@@ -884,7 +884,7 @@ where
884884
{
885885
let ctx = Ctx {
886886
is_exact_arg: false,
887-
is_exact_reassignment: false,
887+
is_exact_assignment: false,
888888
is_callee: false,
889889
is_id_ref: false,
890890
..self.ctx
@@ -895,7 +895,7 @@ where
895895
if let MemberProp::Computed(c) = &e.prop {
896896
let ctx = Ctx {
897897
is_exact_arg: false,
898-
is_exact_reassignment: false,
898+
is_exact_assignment: false,
899899
is_callee: false,
900900
is_delete_arg: false,
901901
..self.ctx
@@ -1067,7 +1067,7 @@ where
10671067
fn visit_prop(&mut self, n: &Prop) {
10681068
let ctx = Ctx {
10691069
is_exact_arg: false,
1070-
is_exact_reassignment: false,
1070+
is_exact_assignment: false,
10711071
is_id_ref: true,
10721072
..self.ctx
10731073
};
@@ -1151,7 +1151,7 @@ where
11511151
if let SuperProp::Computed(c) = &e.prop {
11521152
let ctx = Ctx {
11531153
is_exact_arg: false,
1154-
is_exact_reassignment: false,
1154+
is_exact_assignment: false,
11551155
is_delete_arg: false,
11561156
is_id_ref: false,
11571157
..self.ctx
@@ -1217,7 +1217,7 @@ where
12171217
fn visit_unary_expr(&mut self, n: &UnaryExpr) {
12181218
let ctx = Ctx {
12191219
in_update_arg: false,
1220-
is_exact_reassignment: false,
1220+
is_exact_assignment: false,
12211221
is_delete_arg: n.op == op!("delete"),
12221222
..self.ctx
12231223
};
@@ -1228,7 +1228,7 @@ where
12281228
fn visit_update_expr(&mut self, n: &UpdateExpr) {
12291229
let ctx = Ctx {
12301230
in_update_arg: true,
1231-
is_exact_reassignment: true,
1231+
is_exact_assignment: true,
12321232
is_delete_arg: false,
12331233
..self.ctx
12341234
};

1 commit comments

Comments
 (1)

github-actions[bot] commented on Oct 19, 2023

@github-actions[bot]

Benchmark

Benchmark suite Current: 13106e0 Previous: 8d7894c Ratio
es/full/bugs-1 284560 ns/iter (± 3390) 288688 ns/iter (± 5595) 0.99
es/full/minify/libraries/antd 1383193821 ns/iter (± 13560992) 1383350684 ns/iter (± 11788999) 1.00
es/full/minify/libraries/d3 295542397 ns/iter (± 3617008) 296319155 ns/iter (± 2253807) 1.00
es/full/minify/libraries/echarts 1109847825 ns/iter (± 6939487) 1109579854 ns/iter (± 4405740) 1.00
es/full/minify/libraries/jquery 89467381 ns/iter (± 771486) 88987302 ns/iter (± 168124) 1.01
es/full/minify/libraries/lodash 104195677 ns/iter (± 241040) 104271650 ns/iter (± 479429) 1.00
es/full/minify/libraries/moment 52319062 ns/iter (± 148940) 52421190 ns/iter (± 99243) 1.00
es/full/minify/libraries/react 18788957 ns/iter (± 43939) 18862789 ns/iter (± 70621) 1.00
es/full/minify/libraries/terser 230107110 ns/iter (± 944521) 229662182 ns/iter (± 736719) 1.00
es/full/minify/libraries/three 408363859 ns/iter (± 2399893) 407653860 ns/iter (± 2586256) 1.00
es/full/minify/libraries/typescript 2736354242 ns/iter (± 6486448) 2779689950 ns/iter (± 9224924) 0.98
es/full/minify/libraries/victory 589740453 ns/iter (± 4873132) 594793169 ns/iter (± 2543440) 0.99
es/full/minify/libraries/vue 125492488 ns/iter (± 193039) 125814658 ns/iter (± 204118) 1.00
es/full/codegen/es3 33588 ns/iter (± 151) 34569 ns/iter (± 56) 0.97
es/full/codegen/es5 33671 ns/iter (± 189) 34621 ns/iter (± 165) 0.97
es/full/codegen/es2015 33457 ns/iter (± 91) 34559 ns/iter (± 95) 0.97
es/full/codegen/es2016 33536 ns/iter (± 111) 34608 ns/iter (± 147) 0.97
es/full/codegen/es2017 33644 ns/iter (± 96) 34592 ns/iter (± 151) 0.97
es/full/codegen/es2018 33506 ns/iter (± 151) 34644 ns/iter (± 90) 0.97
es/full/codegen/es2019 33438 ns/iter (± 96) 34592 ns/iter (± 201) 0.97
es/full/codegen/es2020 33415 ns/iter (± 59) 34597 ns/iter (± 108) 0.97
es/full/all/es3 176370241 ns/iter (± 1138533) 176204224 ns/iter (± 709095) 1.00
es/full/all/es5 170723592 ns/iter (± 1033427) 168609182 ns/iter (± 473897) 1.01
es/full/all/es2015 127382600 ns/iter (± 419235) 127574293 ns/iter (± 613526) 1.00
es/full/all/es2016 126985337 ns/iter (± 951262) 126459668 ns/iter (± 1749259) 1.00
es/full/all/es2017 126074714 ns/iter (± 616334) 125908328 ns/iter (± 795191) 1.00
es/full/all/es2018 123968281 ns/iter (± 678247) 123950332 ns/iter (± 1267294) 1.00
es/full/all/es2019 124023376 ns/iter (± 930039) 122742495 ns/iter (± 511746) 1.01
es/full/all/es2020 119918511 ns/iter (± 511295) 119476634 ns/iter (± 426357) 1.00
es/full/parser 559131 ns/iter (± 3507) 559583 ns/iter (± 3375) 1.00
es/full/base/fixer 18015 ns/iter (± 242) 18925 ns/iter (± 90) 0.95
es/full/base/resolver_and_hygiene 83844 ns/iter (± 260) 83590 ns/iter (± 136) 1.00

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

Please sign in to comment.