Skip to content

Commit 2687231

Browse files
authored
perf(es/minifier): Do heavy operation only if required (#9902)
**Description:** In `store_var_for_inlining`, we had called `inline_with_multi_replacer` needlessly. This PR fixes it.
1 parent 47ea8de commit 2687231

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

.changeset/old-foxes-train.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_ecma_minifier: minor
3+
swc_core: minor
4+
---
5+
6+
perf(es/minifier): Do heavy operation only if required

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl Optimizer<'_> {
8989
let is_inline_enabled =
9090
self.options.reduce_vars || self.options.collapse_vars || self.options.inline != 0;
9191

92-
self.vars.inline_with_multi_replacer(init);
92+
let mut inlined_into_init = false;
9393

9494
let id = ident.to_id();
9595

@@ -107,6 +107,9 @@ impl Optimizer<'_> {
107107
&& !usage.used_as_ref
108108
{
109109
if let Expr::Array(arr) = init {
110+
self.vars.inline_with_multi_replacer(arr);
111+
inlined_into_init = true;
112+
110113
if arr.elems.len() < 32
111114
&& arr.elems.iter().all(|e| match e {
112115
Some(ExprOrSpread { spread: None, expr }) => match &**expr {
@@ -257,6 +260,11 @@ impl Optimizer<'_> {
257260
_ => false,
258261
}
259262
{
263+
if !inlined_into_init {
264+
inlined_into_init = true;
265+
self.vars.inline_with_multi_replacer(init);
266+
}
267+
260268
self.mode.store(id.clone(), &*init);
261269

262270
let VarUsageInfo {
@@ -498,6 +506,10 @@ impl Optimizer<'_> {
498506
return;
499507
}
500508

509+
if !inlined_into_init {
510+
self.vars.inline_with_multi_replacer(init);
511+
}
512+
501513
report_change!(
502514
"inline: Decided to inline var '{}' because it's used only once",
503515
ident

0 commit comments

Comments
 (0)