Skip to content

Commit

Permalink
perf(es/minifier): Make more things parallel and reduce call stacks (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Jun 13, 2022
1 parent 925f4d1 commit 3580638
Show file tree
Hide file tree
Showing 43 changed files with 674 additions and 438 deletions.
37 changes: 5 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ members = [
]

[profile.release]
codegen-units = 1
lto = true

# We use CARGO_PROFILE_RELEASE_LTO for production builds
# lto = "fat"
Expand All @@ -32,7 +32,7 @@ codegen-units = 1
# opt-level = 'z'

[profile.bench]
codegen-units = 1
lto = true
debug = true

# Without this, printing diff consumes more than a minute.
Expand All @@ -44,5 +44,5 @@ opt-level = 3
opt-level = 3

[patch.crates-io]
cranelift-codegen = {git = "https://github.com/kdy1/wasmtime", branch = "tls"}
cranelift-entity = {git = "https://github.com/kdy1/wasmtime", branch = "tls"}
cranelift-codegen = { git = "https://github.com/kdy1/wasmtime", branch = "tls" }
cranelift-entity = { git = "https://github.com/kdy1/wasmtime", branch = "tls" }
1 change: 1 addition & 0 deletions crates/swc_ecma_minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ahash = "0.7.6"
arrayvec = "0.7.2"
backtrace = { version = "0.3.61", optional = true }
indexmap = "1.7.0"
num_cpus = "1.13.1"
once_cell = "1.10.0"
parking_lot = "0.12.0"
pretty_assertions = { version = "1.1", optional = true }
Expand Down
65 changes: 37 additions & 28 deletions crates/swc_ecma_minifier/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use rustc_hash::{FxHashMap, FxHashSet};
use std::{collections::HashSet, hash::BuildHasherDefault};

use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
use swc_atoms::{js_word, JsWord};
use swc_common::{collections::AHashSet, SyntaxContext};
use swc_ecma_ast::*;
Expand Down Expand Up @@ -163,35 +165,41 @@ pub(crate) struct ProgramData {
impl ProgramData {
pub(crate) fn expand_infected(
&self,
ids: impl IntoIterator<Item = Id>,
ids: FxHashSet<Id>,
max_num: usize,
) -> Result<FxHashSet<Id>, ()> {
let mut result = FxHashSet::default();
self.expand_infected_inner(ids, max_num, &mut result)?;
Ok(result)
}

fn expand_infected_inner(
&self,
ids: impl IntoIterator<Item = Id>,
max_num: usize,
result: &mut FxHashSet<Id>,
) -> Result<(), ()> {
for id in ids {
if !result.insert(id.clone()) {
continue;
}
if result.len() >= max_num {
return Err(());
}

if let Some(info) = self.vars.get(&id) {
let ids = info.infects.clone();
self.expand_infected_inner(ids, max_num, result)?;
let init =
HashSet::with_capacity_and_hasher(max_num, BuildHasherDefault::<FxHasher>::default());
ids.into_iter().try_fold(init, |mut res, id| {
let mut ids = Vec::with_capacity(max_num);
ids.push(id);
let mut ranges = vec![0..1usize];
loop {
let range = ranges.remove(0);
for index in range {
let iid = ids.get(index).unwrap();
if !res.insert(iid.clone()) {
continue;
}
if res.len() >= max_num {
return Err(());
}
if let Some(info) = self.vars.get(iid) {
let infects = &info.infects;
if !infects.is_empty() {
let old_len = ids.len();
ids.extend_from_slice(infects.as_slice());
let new_len = ids.len();
ranges.push(old_len..new_len);
}
}
}
if ranges.is_empty() {
break;
}
}
}

Ok(())
Ok(res)
})
}

pub(crate) fn contains_unresolved(&self, e: &Expr) -> bool {
Expand Down Expand Up @@ -633,7 +641,8 @@ where
e.visit_children_with(&mut *self.with_ctx(ctx));

if let Expr::Ident(i) = e {
if cfg!(feature = "debug") {
#[cfg(feature = "debug")]
{
// debug!(
// "Usage: `{}``; update = {:?}, assign_lhs = {:?} ",
// i,
Expand Down
63 changes: 37 additions & 26 deletions crates/swc_ecma_minifier/src/compress/hoist_decls.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rayon::prelude::*;
use swc_common::{collections::AHashSet, pass::Repeated, util::take::Take, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{find_pat_ids, StmtLike};
Expand All @@ -6,7 +7,7 @@ use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith};
use super::util::drop_invalid_stmts;
use crate::{
analyzer::{ProgramData, UsageAnalyzer},
util::{is_hoisted_var_decl_without_init, sort::is_sorted_by_key, IsModuleItem, ModuleItemExt},
util::{is_hoisted_var_decl_without_init, sort::is_sorted_by, IsModuleItem, ModuleItemExt},
};

pub(super) struct DeclHoisterConfig {
Expand Down Expand Up @@ -46,33 +47,43 @@ impl Hoister<'_> {
Vec<T>: for<'aa> VisitMutWith<Hoister<'aa>> + VisitWith<UsageAnalyzer>,
{
stmts.visit_mut_children_with(self);
let len = stmts.len();
let should_hoist = !is_sorted_by(
stmts.iter().map(|stmt| match stmt.as_stmt() {
Some(stmt) => match stmt {
Stmt::Decl(Decl::Fn(..)) if self.config.hoist_fns => 1,
Stmt::Decl(Decl::Var(var)) if self.config.hoist_vars => {
let ids: Vec<Id> = find_pat_ids(&var.decls);

let should_hoist = !is_sorted_by_key(stmts.iter(), |stmt| match stmt.as_stmt() {
Some(stmt) => match stmt {
Stmt::Decl(Decl::Fn(..)) if self.config.hoist_fns => 1,
Stmt::Decl(Decl::Var(var)) if self.config.hoist_vars => {
let ids: Vec<Id> = find_pat_ids(&var.decls);

if ids.iter().any(|id| {
self.data
.vars
.get(id)
.map(|v| !v.used_above_decl)
.unwrap_or(false)
}) {
2
} else {
3
if ids.iter().any(|id| {
self.data
.vars
.get(id)
.map(|v| !v.used_above_decl)
.unwrap_or(false)
}) {
2
} else {
3
}
}
}
_ => 3,
},
None => 3,
}) || (self.config.hoist_vars
&& stmts.windows(2).any(|stmts| {
is_hoisted_var_decl_without_init(&stmts[0])
&& is_hoisted_var_decl_without_init(&stmts[1])
}));
_ => 3,
},
None => 3,
}),
PartialOrd::partial_cmp,
) || (self.config.hoist_vars
&& if len >= *crate::LIGHT_TASK_PARALLELS {
stmts.par_chunks(2).any(|stmts| {
is_hoisted_var_decl_without_init(&stmts[0])
&& is_hoisted_var_decl_without_init(&stmts[1])
})
} else {
stmts.windows(2).any(|stmts| {
is_hoisted_var_decl_without_init(&stmts[0])
&& is_hoisted_var_decl_without_init(&stmts[1])
})
});

if !should_hoist {
return;
Expand Down

0 comments on commit 3580638

Please sign in to comment.