Skip to content

Commit

Permalink
perf(es/minifier): Do heavy operation only if required (#9902)
Browse files Browse the repository at this point in the history
**Description:**

In `store_var_for_inlining`, we had called `inline_with_multi_replacer` needlessly. This PR fixes it.
  • Loading branch information
kdy1 authored Jan 19, 2025
1 parent 47ea8de commit 2687231
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/old-foxes-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_ecma_minifier: minor
swc_core: minor
---

perf(es/minifier): Do heavy operation only if required
14 changes: 13 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Optimizer<'_> {
let is_inline_enabled =
self.options.reduce_vars || self.options.collapse_vars || self.options.inline != 0;

self.vars.inline_with_multi_replacer(init);
let mut inlined_into_init = false;

let id = ident.to_id();

Expand All @@ -107,6 +107,9 @@ impl Optimizer<'_> {
&& !usage.used_as_ref
{
if let Expr::Array(arr) = init {
self.vars.inline_with_multi_replacer(arr);
inlined_into_init = true;

if arr.elems.len() < 32
&& arr.elems.iter().all(|e| match e {
Some(ExprOrSpread { spread: None, expr }) => match &**expr {
Expand Down Expand Up @@ -257,6 +260,11 @@ impl Optimizer<'_> {
_ => false,
}
{
if !inlined_into_init {
inlined_into_init = true;
self.vars.inline_with_multi_replacer(init);
}

self.mode.store(id.clone(), &*init);

let VarUsageInfo {
Expand Down Expand Up @@ -498,6 +506,10 @@ impl Optimizer<'_> {
return;
}

if !inlined_into_init {
self.vars.inline_with_multi_replacer(init);
}

report_change!(
"inline: Decided to inline var '{}' because it's used only once",
ident
Expand Down

0 comments on commit 2687231

Please sign in to comment.