Skip to content

Commit 858e92a

Browse files
authored
perf(es/minifier): Replace rayon with chili (#9829)
**Description:** <img width="1383" alt="image" src="https://github.com/user-attachments/assets/d6c5b36e-3341-49a2-8ead-50b33af9d128" /> ``` es/minifier/libs/es/minifier/libs/rspack time: [916.51 ms 918.15 ms 920.06 ms] change: [-2.9454% -2.6510% -2.3679%] (p = 0.00 < 0.05) Performance has improved. Found 1 outliers among 10 measurements (10.00%) 1 (10.00%) high mild ```
1 parent d381e2f commit 858e92a

File tree

12 files changed

+82
-173
lines changed

12 files changed

+82
-173
lines changed

Diff for: .changeset/cool-beans-check.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_minifier: patch
4+
swc_ecma_transforms_base: patch
5+
swc_ecma_transforms_optimization: patch
6+
---
7+
8+
perf(es/minifier): Replace `rayon` with `chili`

Diff for: .changeset/red-coats-melt.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
swc_core: patch
3-
swc_ecma_utils: patch
3+
swc_ecma_utils: major
44
swc_ecma_lints: patch
55
---
66

Diff for: Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: crates/swc_ecma_minifier/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ swc_ecma_transforms_optimization = { version = "6.0.0", path = "../swc_ecma_tran
6868
swc_ecma_usage_analyzer = { version = "6.0.0", path = "../swc_ecma_usage_analyzer" }
6969
swc_ecma_utils = { version = "6.0.1", path = "../swc_ecma_utils" }
7070
swc_ecma_visit = { version = "5.0.0", path = "../swc_ecma_visit" }
71+
swc_parallel = { version = "1.0.0", path = "../swc_parallel", default-features = false }
7172
swc_timer = { version = "1.0.0", path = "../swc_timer" }
7273

7374
[dev-dependencies]

Diff for: crates/swc_ecma_minifier/src/compress/pure/ctx.rs

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use super::Pure;
44

55
#[derive(Default, Clone, Copy)]
66
pub(super) struct Ctx {
7-
pub par_depth: u8,
8-
97
pub in_delete: bool,
108

119
/// `true` if we are in `arg` of `++arg` or `--arg`.

Diff for: crates/swc_ecma_minifier/src/compress/pure/mod.rs

+23-81
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#![allow(clippy::needless_update)]
22

3-
#[cfg(feature = "concurrent")]
4-
use rayon::prelude::*;
5-
use swc_common::{pass::Repeated, util::take::Take, SyntaxContext, DUMMY_SP, GLOBALS};
3+
use swc_common::{pass::Repeated, util::take::Take, SyntaxContext, DUMMY_SP};
64
use swc_ecma_ast::*;
75
use swc_ecma_transforms_optimization::debug_assert_valid;
86
use swc_ecma_usage_analyzer::marks::Marks;
9-
use swc_ecma_utils::ExprCtx;
7+
use swc_ecma_utils::{
8+
parallel::{cpu_count, Parallel, ParallelExt},
9+
ExprCtx,
10+
};
1011
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith};
1112
#[cfg(feature = "debug")]
1213
use tracing::{debug, span, Level};
@@ -76,6 +77,21 @@ struct Pure<'a> {
7677
changed: bool,
7778
}
7879

80+
impl Parallel for Pure<'_> {
81+
fn create(&self) -> Self {
82+
Self {
83+
expr_ctx: self.expr_ctx.clone(),
84+
..*self
85+
}
86+
}
87+
88+
fn merge(&mut self, other: Self) {
89+
if other.changed {
90+
self.changed = true;
91+
}
92+
}
93+
}
94+
7995
impl Repeated for Pure<'_> {
8096
fn changed(&self) -> bool {
8197
self.changed
@@ -168,83 +184,9 @@ impl Pure<'_> {
168184
where
169185
N: for<'aa> VisitMutWith<Pure<'aa>> + Send + Sync,
170186
{
171-
let mut changed = false;
172-
if !cfg!(target_arch = "wasm32") && (!cfg!(feature = "debug") || !cfg!(debug_assertions)) {
173-
#[cfg(feature = "concurrent")]
174-
{
175-
GLOBALS.with(|globals| {
176-
changed = nodes
177-
.par_iter_mut()
178-
.with_min_len(
179-
(*crate::HEAVY_TASK_PARALLELS)
180-
* (f32::log2(self.ctx.par_depth as f32).floor() as usize),
181-
)
182-
.map(|node| {
183-
GLOBALS.set(globals, || {
184-
let mut v = Pure {
185-
expr_ctx: self.expr_ctx.clone(),
186-
ctx: Ctx {
187-
par_depth: self.ctx.par_depth + 1,
188-
..self.ctx
189-
},
190-
changed: false,
191-
..*self
192-
};
193-
node.visit_mut_with(&mut v);
194-
195-
v.changed
196-
})
197-
})
198-
.reduce(|| false, |a, b| a || b);
199-
})
200-
}
201-
202-
#[cfg(not(feature = "concurrent"))]
203-
{
204-
GLOBALS.with(|globals| {
205-
changed = nodes
206-
.iter_mut()
207-
.map(|node| {
208-
GLOBALS.set(globals, || {
209-
let mut v = Pure {
210-
expr_ctx: self.expr_ctx.clone(),
211-
ctx: Ctx {
212-
par_depth: self.ctx.par_depth + 1,
213-
..self.ctx
214-
},
215-
changed: false,
216-
..*self
217-
};
218-
node.visit_mut_with(&mut v);
219-
220-
v.changed
221-
})
222-
})
223-
.reduce(|a, b| a || b)
224-
.unwrap_or(false);
225-
})
226-
}
227-
} else {
228-
changed = nodes
229-
.iter_mut()
230-
.map(|node| {
231-
let mut v = Pure {
232-
expr_ctx: self.expr_ctx.clone(),
233-
ctx: Ctx {
234-
par_depth: self.ctx.par_depth,
235-
..self.ctx
236-
},
237-
changed: false,
238-
..*self
239-
};
240-
node.visit_mut_with(&mut v);
241-
242-
v.changed
243-
})
244-
.reduce(|a, b| a || b)
245-
.unwrap_or(false);
246-
}
247-
self.changed |= changed;
187+
self.maybe_par(cpu_count(), nodes, |v, node| {
188+
node.visit_mut_with(v);
189+
});
248190
}
249191
}
250192

Diff for: crates/swc_ecma_transforms_base/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ swc_ecma_ast = { version = "5.0.1", path = "../swc_ecma_ast" }
3434
swc_ecma_parser = { version = "6.0.1", path = "../swc_ecma_parser" }
3535
swc_ecma_utils = { version = "6.0.1", path = "../swc_ecma_utils" }
3636
swc_ecma_visit = { version = "5.0.0", path = "../swc_ecma_visit" }
37+
swc_parallel = { version = "1.0.0", path = "../swc_parallel", default-features = false }
3738

3839
[dev-dependencies]
3940
codspeed-criterion-compat = { workspace = true }

Diff for: crates/swc_ecma_transforms_base/src/perf.rs

+24-67
Original file line numberDiff line numberDiff line change
@@ -51,43 +51,20 @@ where
5151
N: Send + Sync + VisitWith<Self>,
5252
{
5353
if nodes.len() >= threshold {
54-
GLOBALS.with(|globals| {
55-
HELPERS.with(|helpers| {
56-
let helpers = helpers.data();
57-
58-
HANDLER.with(|handler| {
59-
use rayon::prelude::*;
60-
61-
let visitor = nodes
62-
.into_par_iter()
63-
.map(|node| {
64-
let helpers = Helpers::from_data(helpers);
65-
66-
GLOBALS.set(globals, || {
67-
HELPERS.set(&helpers, || {
68-
HANDLER.set(handler, || {
69-
let mut visitor = Parallel::create(&*self);
70-
node.visit_with(&mut visitor);
71-
72-
visitor
73-
})
74-
})
75-
})
76-
})
77-
.reduce(
78-
|| Parallel::create(&*self),
79-
|mut a, b| {
80-
Parallel::merge(&mut a, b);
81-
82-
a
83-
},
84-
);
85-
86-
Parallel::merge(self, visitor);
87-
})
54+
HELPERS.with(|helpers| {
55+
let helpers = helpers.data();
56+
57+
HANDLER.with(|handler| {
58+
self.maybe_par(threshold, nodes, |visitor, node| {
59+
let helpers = Helpers::from_data(helpers);
60+
HELPERS.set(&helpers, || {
61+
HANDLER.set(handler, || {
62+
node.visit_with(visitor);
63+
});
64+
});
65+
});
8866
})
8967
});
90-
9168
return;
9269
}
9370

@@ -113,38 +90,18 @@ where
11390
N: Send + Sync + VisitMutWith<Self>,
11491
{
11592
if nodes.len() >= threshold {
116-
GLOBALS.with(|globals| {
117-
HELPERS.with(|helpers| {
118-
let helpers = helpers.data();
119-
HANDLER.with(|handler| {
120-
use rayon::prelude::*;
121-
122-
let visitor = nodes
123-
.into_par_iter()
124-
.map(|node| {
125-
let helpers = Helpers::from_data(helpers);
126-
GLOBALS.set(globals, || {
127-
HELPERS.set(&helpers, || {
128-
HANDLER.set(handler, || {
129-
let mut visitor = Parallel::create(&*self);
130-
node.visit_mut_with(&mut visitor);
131-
132-
visitor
133-
})
134-
})
135-
})
136-
})
137-
.reduce(
138-
|| Parallel::create(&*self),
139-
|mut a, b| {
140-
Parallel::merge(&mut a, b);
141-
142-
a
143-
},
144-
);
145-
146-
Parallel::merge(self, visitor);
147-
})
93+
HELPERS.with(|helpers| {
94+
let helpers = helpers.data();
95+
96+
HANDLER.with(|handler| {
97+
self.maybe_par(threshold, nodes, |visitor, node| {
98+
let helpers = Helpers::from_data(helpers);
99+
HELPERS.set(&helpers, || {
100+
HANDLER.set(handler, || {
101+
node.visit_mut_with(visitor);
102+
});
103+
});
104+
});
148105
})
149106
});
150107

Diff for: crates/swc_ecma_transforms_optimization/src/inline_globals.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,19 @@ impl VisitMut for InlineGlobals {
183183
}
184184

185185
fn visit_mut_prop_or_spreads(&mut self, n: &mut Vec<PropOrSpread>) {
186-
self.visit_mut_par(cpu_count() * 8, n);
186+
self.visit_mut_par(cpu_count(), n);
187187
}
188188

189189
fn visit_mut_expr_or_spreads(&mut self, n: &mut Vec<ExprOrSpread>) {
190-
self.visit_mut_par(cpu_count() * 8, n);
190+
self.visit_mut_par(cpu_count(), n);
191191
}
192192

193193
fn visit_mut_opt_vec_expr_or_spreads(&mut self, n: &mut Vec<Option<ExprOrSpread>>) {
194-
self.visit_mut_par(cpu_count() * 8, n);
194+
self.visit_mut_par(cpu_count(), n);
195195
}
196196

197197
fn visit_mut_exprs(&mut self, n: &mut Vec<Box<Expr>>) {
198-
self.visit_mut_par(cpu_count() * 8, n);
198+
self.visit_mut_par(cpu_count(), n);
199199
}
200200
}
201201

Diff for: crates/swc_ecma_transforms_optimization/src/simplify/branch/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1219,31 +1219,31 @@ impl VisitMut for Remover {
12191219
}
12201220

12211221
fn visit_mut_prop_or_spreads(&mut self, n: &mut Vec<PropOrSpread>) {
1222-
self.maybe_par(cpu_count() * 8, n, |v, n| {
1222+
self.maybe_par(cpu_count(), n, |v, n| {
12231223
n.visit_mut_with(v);
12241224
})
12251225
}
12261226

12271227
fn visit_mut_expr_or_spreads(&mut self, n: &mut Vec<ExprOrSpread>) {
1228-
self.maybe_par(cpu_count() * 8, n, |v, n| {
1228+
self.maybe_par(cpu_count(), n, |v, n| {
12291229
n.visit_mut_with(v);
12301230
})
12311231
}
12321232

12331233
fn visit_mut_opt_vec_expr_or_spreads(&mut self, n: &mut Vec<Option<ExprOrSpread>>) {
1234-
self.maybe_par(cpu_count() * 8, n, |v, n| {
1234+
self.maybe_par(cpu_count(), n, |v, n| {
12351235
n.visit_mut_with(v);
12361236
})
12371237
}
12381238

12391239
fn visit_mut_exprs(&mut self, n: &mut Vec<Box<Expr>>) {
1240-
self.maybe_par(cpu_count() * 8, n, |v, n| {
1240+
self.maybe_par(cpu_count(), n, |v, n| {
12411241
n.visit_mut_with(v);
12421242
})
12431243
}
12441244

12451245
fn visit_mut_var_declarators(&mut self, n: &mut Vec<VarDeclarator>) {
1246-
self.maybe_par(cpu_count() * 8, n, |v, n| {
1246+
self.maybe_par(cpu_count(), n, |v, n| {
12471247
n.visit_mut_with(v);
12481248
})
12491249
}
@@ -1261,7 +1261,7 @@ impl Remover {
12611261

12621262
let mut new_stmts = Vec::with_capacity(stmts.len());
12631263

1264-
self.maybe_par(cpu_count() * 8, &mut *stmts, |visitor, stmt| {
1264+
self.maybe_par(cpu_count(), &mut *stmts, |visitor, stmt| {
12651265
visitor.normal_block = true;
12661266
stmt.visit_mut_with(visitor);
12671267
});

0 commit comments

Comments
 (0)