Skip to content

Commit

Permalink
perf(es/minifier): Improve parallelism and cache friendliness (#9813)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Jan 1, 2025
1 parent 6a31920 commit f8dff56
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ resolver = "2"
glob = "0.3.0"
hashbrown = "0.14.5"
hex = "0.4.3"
hstr = "0.2.12"
hstr = "0.2.13"
indexmap = "2.0.0"
is-macro = "0.3.5"
js-sys = "0.3.59"
Expand Down
11 changes: 9 additions & 2 deletions crates/swc_atoms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ macro_rules! impl_from {
};
}

impl From<hstr::Atom> for Atom {
#[inline(always)]
fn from(s: hstr::Atom) -> Self {
Atom(s)
}
}

impl PartialEq<str> for Atom {
fn eq(&self, other: &str) -> bool {
&**self == other
Expand Down Expand Up @@ -174,15 +181,15 @@ impl<'de> serde::de::Deserialize<'de> for Atom {
#[macro_export]
macro_rules! atom {
($s:tt) => {{
$crate::Atom::new($crate::hstr::atom!($s))
$crate::Atom::from($crate::hstr::atom!($s))
}};
}

/// Creates an [Atom] from a constant.
#[macro_export]
macro_rules! lazy_atom {
($s:tt) => {{
$crate::Atom::new($crate::hstr::atom!($s))
$crate::Atom::from($crate::hstr::atom!($s))
}};
}

Expand Down
9 changes: 5 additions & 4 deletions crates/swc_ecma_minifier/src/compress/pure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,16 @@ impl Pure<'_> {
N: for<'aa> VisitMutWith<Pure<'aa>> + Send + Sync,
{
let mut changed = false;
if !cfg!(target_arch = "wasm32")
&& (!cfg!(feature = "debug") || !cfg!(debug_assertions))
&& nodes.len() >= *crate::HEAVY_TASK_PARALLELS
{
if !cfg!(target_arch = "wasm32") && (!cfg!(feature = "debug") || !cfg!(debug_assertions)) {
#[cfg(feature = "concurrent")]
{
GLOBALS.with(|globals| {
changed = nodes
.par_iter_mut()
.with_min_len(
(*crate::HEAVY_TASK_PARALLELS)
* (f32::log2(self.ctx.par_depth as f32).floor() as usize),
)
.map(|node| {
GLOBALS.set(globals, || {
let mut v = Pure {
Expand Down
13 changes: 6 additions & 7 deletions crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{borrow::Cow, sync::Arc};
use indexmap::IndexSet;
use petgraph::{algo::tarjan_scc, Direction::Incoming};
use rustc_hash::FxHashSet;
use swc_atoms::JsWord;
use swc_atoms::{atom, JsWord};
use swc_common::{
collections::{AHashMap, AHashSet, ARandomState},
pass::{CompilerPass, Repeated},
Expand Down Expand Up @@ -127,9 +127,9 @@ impl Data {
}

/// Add an edge to dependency graph
fn add_dep_edge(&mut self, from: Id, to: Id, assign: bool) {
let from = self.node(&from);
let to = self.node(&to);
fn add_dep_edge(&mut self, from: &Id, to: &Id, assign: bool) {
let from = self.node(from);
let to = self.node(to);

match self.graph.edge_weight_mut(from, to) {
Some(info) => {
Expand Down Expand Up @@ -311,7 +311,7 @@ impl Analyzer<'_> {

/// Mark `id` as used
fn add(&mut self, id: Id, assign: bool) {
if id.0 == "arguments" {
if id.0 == atom!("arguments") {
self.scope.found_arguemnts = true;
}

Expand All @@ -335,8 +335,7 @@ impl Analyzer<'_> {

while let Some(s) = scope {
for component in &s.ast_path {
self.data
.add_dep_edge(component.clone(), id.clone(), assign)
self.data.add_dep_edge(component, &id, assign);
}

if s.kind == ScopeKind::Fn && !s.ast_path.is_empty() {
Expand Down

0 comments on commit f8dff56

Please sign in to comment.