perf(es/minifier): Add some fast-path to the MultiReplacer (#4408)

This commit is contained in:
Donny/강동윤 2022-04-23 19:53:21 +09:00 committed by GitHub
parent 5a8250610a
commit 4c9e5c01ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 15 deletions

View File

@ -23,8 +23,8 @@ fn main() {
testing::run_test2(false, |cm, handler| { testing::run_test2(false, |cm, handler| {
GLOBALS.with(|globals| { GLOBALS.with(|globals| {
files let _ = files
.into_par_iter() .into_iter()
.map(|path| -> Result<_> { .map(|path| -> Result<_> {
GLOBALS.set(globals, || { GLOBALS.set(globals, || {
let fm = cm.load_file(&path).expect("failed to load file"); let fm = cm.load_file(&path).expect("failed to load file");

View File

@ -8,4 +8,4 @@
export RUST_LOG=off export RUST_LOG=off
cargo profile instruments -t time --example minify-all --release -- $@ cargo profile instruments -t time --example minify-all --features swc_common/perf --release -- $@

View File

@ -250,8 +250,15 @@ impl VisitMut for MultiReplacer<'_> {
} }
fn visit_mut_expr(&mut self, e: &mut Expr) { fn visit_mut_expr(&mut self, e: &mut Expr) {
if self.vars.is_empty() {
return;
}
e.visit_mut_children_with(self); e.visit_mut_children_with(self);
if self.vars.is_empty() {
return;
}
if matches!(self.mode, MultiReplacerMode::Normal) { if matches!(self.mode, MultiReplacerMode::Normal) {
if let Expr::Ident(i) = e { if let Expr::Ident(i) = e {
if let Some(new) = self.var(&i.to_id()) { 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) { fn visit_mut_prop(&mut self, p: &mut Prop) {
p.visit_mut_children_with(self); p.visit_mut_children_with(self);
if let Prop::Shorthand(i) = p { if matches!(self.mode, MultiReplacerMode::Normal) {
if let Some(value) = self.var(&i.to_id()) { if let Prop::Shorthand(i) = p {
debug!("multi-replacer: Replaced `{}` as shorthand", i); if let Some(value) = self.var(&i.to_id()) {
*self.worked = true; debug!("multi-replacer: Replaced `{}` as shorthand", i);
self.changed = true; *self.worked = true;
self.changed = true;
*p = Prop::KeyValue(KeyValueProp { *p = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(Ident::new( key: PropName::Ident(Ident::new(
i.sym.clone(), i.sym.clone(),
i.span.with_ctxt(Default::default()), i.span.with_ctxt(Default::default()),
)), )),
value, value,
}); });
}
} }
} }
} }