From 8bfb0e5edd51934dff37fe70136b73d5301a4e41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 21 Jan 2025 20:29:34 +0900 Subject: [PATCH] perf(es/minifier): Invert cache to be really a cache (#9910) **Description:** Previous PR may decrease the performance if the result of `collect_infects_to` is empty. **Related issue:** - Inverts https://github.com/swc-project/swc/pull/9909 --- .changeset/selfish-coats-shave.md | 6 ++++ .../src/analyzer/mod.rs | 36 ++++++++++++++----- 2 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 .changeset/selfish-coats-shave.md diff --git a/.changeset/selfish-coats-shave.md b/.changeset/selfish-coats-shave.md new file mode 100644 index 000000000000..fd38e86a0753 --- /dev/null +++ b/.changeset/selfish-coats-shave.md @@ -0,0 +1,6 @@ +--- +swc_core: patch +swc_ecma_minifier: patch +--- + +perf(es/minifier): Invert cache to be really a cache diff --git a/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs b/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs index 5d34248aefc5..aa373c872deb 100644 --- a/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs +++ b/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs @@ -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; @@ -294,7 +296,7 @@ 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 { @@ -302,7 +304,11 @@ where ..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()); } } } @@ -742,7 +748,7 @@ 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 { @@ -750,7 +756,11 @@ where ..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()); } } } @@ -768,7 +778,7 @@ 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 { @@ -776,7 +786,11 @@ where ..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()); @@ -1261,7 +1275,7 @@ 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 { @@ -1269,7 +1283,11 @@ where ..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()); } } }