Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(es/minifier): Do not visit var init multiple times #9039

Merged
merged 8 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2955,20 +2955,44 @@ impl VisitMut for Optimizer<'_> {
return false;
}

var.visit_mut_with(self);
true
});

{
// We loop with index to avoid borrow checker issue.
// We use splice so we cannot use for _ in vars
let mut idx = 0;

while idx < vars.len() {
let var = &mut vars[idx];
var.visit_mut_with(self);

if var.name.is_invalid() {
vars.remove(idx);
continue;
}

let new = self.hoist_props_of_var(var);

if let Some(new) = new {
idx += new.len();
vars.splice(idx..=idx, new);
} else {
idx += 1;
}
}
}

vars.retain_mut(|var| {
if var.name.is_invalid() {
// It will be inlined.
self.changed = true;
return false;
}

debug_assert_valid(&*var);

true
});

self.hoist_props_of_vars(vars);
debug_assert_valid(&*vars);

let uses_eval = self.data.scopes.get(&self.ctx.scope).unwrap().has_eval_call;

Expand Down
31 changes: 7 additions & 24 deletions crates/swc_ecma_minifier/src/compress/optimize/props.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,29 @@
use swc_common::{util::take::Take, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{contains_this_expr, private_ident, prop_name_eq, ExprExt};
use swc_ecma_visit::VisitMutWith;

use super::{unused::PropertyAccessOpts, Optimizer};
use crate::util::deeply_contains_this_expr;

/// Methods related to the option `hoist_props`.
impl Optimizer<'_> {
pub(super) fn hoist_props_of_vars(&mut self, n: &mut Vec<VarDeclarator>) {
pub(super) fn hoist_props_of_var(
&mut self,
n: &mut VarDeclarator,
) -> Option<Vec<VarDeclarator>> {
if !self.options.hoist_props {
log_abort!("hoist_props: option is disabled");
return;
return None;
}
if self.ctx.is_exported {
log_abort!("hoist_props: Exported variable is not hoisted");
return;
return None;
}
if self.ctx.in_top_level() && !self.options.top_level() {
log_abort!("hoist_props: Top-level variable is not hoisted");
return;
}

let mut new = Vec::with_capacity(n.len());
for mut n in n.take() {
if let Some(init) = &mut n.init {
init.visit_mut_with(self);
}

let new_vars = self.hoist_props_of_var(&mut n);

if let Some(new_vars) = new_vars {
new.extend(new_vars);
} else {
new.push(n);
}
return None;
}

*n = new;
}

fn hoist_props_of_var(&mut self, n: &mut VarDeclarator) -> Option<Vec<VarDeclarator>> {
if let Pat::Ident(name) = &mut n.name {
if name.id.span.ctxt == self.marks.top_level_ctxt
&& self.options.top_retain.contains(&name.id.sym)
Expand Down
Loading