From 4c9e5c01ef91d01c95a6e928daa9e99839eabedf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 23 Apr 2022 19:53:21 +0900 Subject: [PATCH] perf(es/minifier): Add some fast-path to the `MultiReplacer` (#4408) --- .../swc_ecma_minifier/examples/minify-all.rs | 4 +-- .../scripts/instrument/all.sh | 2 +- .../src/compress/optimize/util.rs | 33 ++++++++++++------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/crates/swc_ecma_minifier/examples/minify-all.rs b/crates/swc_ecma_minifier/examples/minify-all.rs index b99058e4b26..400c617fab0 100644 --- a/crates/swc_ecma_minifier/examples/minify-all.rs +++ b/crates/swc_ecma_minifier/examples/minify-all.rs @@ -23,8 +23,8 @@ fn main() { testing::run_test2(false, |cm, handler| { GLOBALS.with(|globals| { - files - .into_par_iter() + let _ = files + .into_iter() .map(|path| -> Result<_> { GLOBALS.set(globals, || { let fm = cm.load_file(&path).expect("failed to load file"); diff --git a/crates/swc_ecma_minifier/scripts/instrument/all.sh b/crates/swc_ecma_minifier/scripts/instrument/all.sh index 43471edcd04..9ed816793a4 100755 --- a/crates/swc_ecma_minifier/scripts/instrument/all.sh +++ b/crates/swc_ecma_minifier/scripts/instrument/all.sh @@ -8,4 +8,4 @@ export RUST_LOG=off -cargo profile instruments -t time --example minify-all --release -- $@ \ No newline at end of file +cargo profile instruments -t time --example minify-all --features swc_common/perf --release -- $@ \ No newline at end of file diff --git a/crates/swc_ecma_minifier/src/compress/optimize/util.rs b/crates/swc_ecma_minifier/src/compress/optimize/util.rs index e11b7a052be..ef7d951e560 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/util.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/util.rs @@ -250,8 +250,15 @@ impl VisitMut for MultiReplacer<'_> { } fn visit_mut_expr(&mut self, e: &mut Expr) { + if self.vars.is_empty() { + return; + } e.visit_mut_children_with(self); + if self.vars.is_empty() { + return; + } + if matches!(self.mode, MultiReplacerMode::Normal) { if let Expr::Ident(i) = e { if let Some(new) = self.var(&i.to_id()) { @@ -286,19 +293,21 @@ impl VisitMut for MultiReplacer<'_> { fn visit_mut_prop(&mut self, p: &mut Prop) { p.visit_mut_children_with(self); - if let Prop::Shorthand(i) = p { - if let Some(value) = self.var(&i.to_id()) { - debug!("multi-replacer: Replaced `{}` as shorthand", i); - *self.worked = true; - self.changed = true; + if matches!(self.mode, MultiReplacerMode::Normal) { + if let Prop::Shorthand(i) = p { + if let Some(value) = self.var(&i.to_id()) { + debug!("multi-replacer: Replaced `{}` as shorthand", i); + *self.worked = true; + self.changed = true; - *p = Prop::KeyValue(KeyValueProp { - key: PropName::Ident(Ident::new( - i.sym.clone(), - i.span.with_ctxt(Default::default()), - )), - value, - }); + *p = Prop::KeyValue(KeyValueProp { + key: PropName::Ident(Ident::new( + i.sym.clone(), + i.span.with_ctxt(Default::default()), + )), + value, + }); + } } } }