From 8789184d447eb09e1b0dddc6156ac150d4cbf0d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Mon, 5 Sep 2022 16:05:44 +0900 Subject: [PATCH] perf(es/minifier): Cache the list of bindings for DCE (#5750) --- crates/swc_ecma_minifier/src/compress/mod.rs | 20 ++++++++-------- .../src/simplify/dce/mod.rs | 23 ++++++++++++------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/crates/swc_ecma_minifier/src/compress/mod.rs b/crates/swc_ecma_minifier/src/compress/mod.rs index d23b8e8d087..46803b4eb8c 100644 --- a/crates/swc_ecma_minifier/src/compress/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/mod.rs @@ -386,19 +386,19 @@ where if self.options.unused { let _timer = timer!("remove dead code"); + let mut visitor = swc_ecma_transforms_optimization::simplify::dce::dce( + swc_ecma_transforms_optimization::simplify::dce::Config { + module_mark: None, + top_level: self.options.top_level(), + top_retain: self.options.top_retain.clone(), + }, + self.marks.unresolved_mark, + ); + loop { #[cfg(feature = "debug")] let start = n.dump(); - let mut visitor = swc_ecma_transforms_optimization::simplify::dce::dce( - swc_ecma_transforms_optimization::simplify::dce::Config { - module_mark: None, - top_level: self.options.top_level(), - top_retain: self.options.top_retain.clone(), - }, - self.marks.unresolved_mark, - ); - n.apply(&mut visitor); self.changed |= visitor.changed(); @@ -417,6 +417,8 @@ where } else { break; } + + visitor.reset(); } } diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs b/crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs index 24f771c6467..de1f0ed1ebf 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs @@ -39,10 +39,11 @@ pub fn dce( config, changed: false, pass: 0, - data: Default::default(), - in_block_stmt: false, in_fn: false, + in_block_stmt: false, var_decl_kind: None, + data: Default::default(), + bindings: Default::default(), }) } @@ -86,6 +87,8 @@ struct TreeShaker { var_decl_kind: Option, data: Arc, + + bindings: Arc>, } impl CompilerPass for TreeShaker { @@ -96,9 +99,6 @@ impl CompilerPass for TreeShaker { #[derive(Default)] struct Data { - /// Bindings in the module. - bindings: AHashSet, - used_names: AHashMap, /// Variable usage graph @@ -535,6 +535,7 @@ impl Parallel for TreeShaker { expr_ctx: self.expr_ctx.clone(), data: self.data.clone(), config: self.config.clone(), + bindings: self.bindings.clone(), ..*self } } @@ -606,7 +607,7 @@ impl TreeShaker { return false; } - self.data.bindings.contains(&name) + self.bindings.contains(&name) && self .data .used_names @@ -795,8 +796,11 @@ impl VisitMut for TreeShaker { fn visit_mut_module(&mut self, m: &mut Module) { let _tracing = span!(Level::ERROR, "tree-shaker", pass = self.pass).entered(); + if self.bindings.is_empty() { + self.bindings = Arc::new(collect_decls(&*m)) + } + let mut data = Data { - bindings: collect_decls(&*m), ..Default::default() }; @@ -822,8 +826,11 @@ impl VisitMut for TreeShaker { fn visit_mut_script(&mut self, m: &mut Script) { let _tracing = span!(Level::ERROR, "tree-shaker", pass = self.pass).entered(); + if self.bindings.is_empty() { + self.bindings = Arc::new(collect_decls(&*m)) + } + let mut data = Data { - bindings: collect_decls(&*m), ..Default::default() };