Skip to content

Commit

Permalink
perf(es/minifier): Invert cache to be really a cache (#9910)
Browse files Browse the repository at this point in the history
**Description:**

Previous PR may decrease the performance if the result of `collect_infects_to` is empty.

**Related issue:**

 - Inverts #9909
  • Loading branch information
kdy1 authored Jan 21, 2025
1 parent 4a3be8d commit 8bfb0e5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/selfish-coats-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_minifier: patch
---

perf(es/minifier): Invert cache to be really a cache
36 changes: 27 additions & 9 deletions crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use swc_common::{collections::AHashMap, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_utils::{find_pat_ids, ExprCtx, ExprExt, IsEmpty, StmtExt, Type, Value};
use swc_ecma_utils::{
find_pat_ids, ident::IdentLike, ExprCtx, ExprExt, IsEmpty, StmtExt, Type, Value,
};
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
use swc_timer::timer;

Expand Down Expand Up @@ -294,15 +296,19 @@ where
};

if let Some(left) = left {
let v = self.data.var_or_default(left.clone());
let mut v = None;
for id in collect_infects_from(
&n.right,
AliasConfig {
marks: self.marks,
..Default::default()
},
) {
v.add_infects_to(id.clone());
if v.is_none() {
v = Some(self.data.var_or_default(left.to_id()));
}

v.as_mut().unwrap().add_infects_to(id.clone());
}
}
}
Expand Down Expand Up @@ -742,15 +748,19 @@ where
self.used_recursively.remove(&id);

{
let v = self.data.var_or_default(id);
let mut v = None;
for id in collect_infects_from(
&n.function,
AliasConfig {
marks: self.marks,
..Default::default()
},
) {
v.add_infects_to(id.clone());
if v.is_none() {
v = Some(self.data.var_or_default(n.ident.to_id()));
}

v.as_mut().unwrap().add_infects_to(id.clone());
}
}
}
Expand All @@ -768,15 +778,19 @@ where
n.visit_children_with(self);

{
let v = self.data.var_or_default(n_id.to_id());
let mut v = None;
for id in collect_infects_from(
&n.function,
AliasConfig {
marks: self.marks,
..Default::default()
},
) {
v.add_infects_to(id);
if v.is_none() {
v = Some(self.data.var_or_default(n_id.to_id()));
}

v.as_mut().unwrap().add_infects_to(id);
}
}
self.used_recursively.remove(&n_id.to_id());
Expand Down Expand Up @@ -1261,15 +1275,19 @@ where

for decl in &n.decls {
if let (Pat::Ident(var), Some(init)) = (&decl.name, decl.init.as_deref()) {
let v = self.data.var_or_default(var.to_id());
let mut v = None;
for id in collect_infects_from(
init,
AliasConfig {
marks: self.marks,
..Default::default()
},
) {
v.add_infects_to(id.clone());
if v.is_none() {
v = Some(self.data.var_or_default(var.to_id()));
}

v.as_mut().unwrap().add_infects_to(id.clone());
}
}
}
Expand Down

0 comments on commit 8bfb0e5

Please sign in to comment.