perf(es/minifier): Make MultiReplacer single-pass (#5761)

This commit is contained in:
Donny/강동윤 2022-09-06 14:36:53 +09:00 committed by GitHub
parent 8493c88b06
commit 27e9254280
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -150,7 +150,6 @@ impl VisitMut for Remapper {
pub(crate) struct MultiReplacer<'a> {
vars: &'a mut FxHashMap<Id, Box<Expr>>,
changed: bool,
clone: bool,
mode: MultiReplacerMode,
worked: &'a mut bool,
@ -174,7 +173,6 @@ impl<'a> MultiReplacer<'a> {
) -> Self {
MultiReplacer {
vars,
changed: false,
clone,
mode,
worked,
@ -182,12 +180,14 @@ impl<'a> MultiReplacer<'a> {
}
fn var(&mut self, i: &Id) -> Option<Box<Expr>> {
let e = if self.clone {
let mut e = if self.clone {
self.vars.get(i).cloned()?
} else {
self.vars.remove(i)?
};
e.visit_mut_children_with(self);
match &*e {
Expr::Ident(Ident {
sym: js_word!("eval"),
@ -213,7 +213,6 @@ impl VisitMut for MultiReplacer<'_> {
if let Some(new) = self.var(&i.to_id()) {
debug!("multi-replacer: Replaced `{}`", i);
*self.worked = true;
self.changed = true;
**e = *new;
}
@ -237,7 +236,6 @@ impl VisitMut for MultiReplacer<'_> {
if let Some(new) = self.var(&i.to_id()) {
debug!("multi-replacer: Replaced `{}`", i);
*self.worked = true;
self.changed = true;
*e = *new;
}
@ -246,21 +244,15 @@ impl VisitMut for MultiReplacer<'_> {
}
fn visit_mut_module_items(&mut self, items: &mut Vec<ModuleItem>) {
loop {
self.changed = false;
if self.vars.is_empty() {
break;
}
items.visit_mut_children_with(self);
if self.vars.is_empty() {
return;
}
items.visit_mut_children_with(self);
if !self.changed {
#[cfg(feature = "debug")]
{
let keys = self.vars.iter().map(|(k, _)| k.clone()).collect::<Vec<_>>();
debug!("Dropping {:?}", keys);
}
break;
}
#[cfg(feature = "debug")]
if !self.vars.is_empty() {
let keys = self.vars.iter().map(|(k, _)| k.clone()).collect::<Vec<_>>();
debug!("Dropping {:?}", keys);
}
}
@ -272,7 +264,6 @@ impl VisitMut for MultiReplacer<'_> {
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(