Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(es/minifier): Do heavy operation only if required #9902

Merged
merged 2 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading