Skip to content

Commit ed74839

Browse files
authored
perf(es/minifier): Parallelize handling of class members (#9900)
**Description:** These methods were missing and it makes the minification of some files needlessly slow.
1 parent 2d87b89 commit ed74839

File tree

11 files changed

+262
-203
lines changed

11 files changed

+262
-203
lines changed

.changeset/calm-frogs-search.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_lints: patch
4+
swc_ecma_transforms_base: patch
5+
swc_ecma_transforms_optimization: patch
6+
swc_ecma_minifier: patch
7+
---
8+
9+
perf(es/minifier): Parallelize handling of class members

crates/swc_ecma_lints/src/rules/const_assign.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ impl Visit for ConstAssign<'_> {
108108
self.check(&Ident::from(n));
109109
}
110110

111+
fn visit_class_members(&mut self, members: &[ClassMember]) {
112+
self.maybe_par(cpu_count(), members, |v, member| {
113+
member.visit_with(v);
114+
});
115+
}
116+
111117
fn visit_expr_or_spreads(&mut self, n: &[ExprOrSpread]) {
112118
self.maybe_par(cpu_count(), n, |v, n| {
113119
n.visit_with(v);

crates/swc_ecma_lints/src/rules/no_dupe_args.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ impl Visit for NoDupeArgs {
111111
f.visit_children_with(self);
112112
}
113113

114+
fn visit_class_members(&mut self, members: &[ClassMember]) {
115+
self.maybe_par(cpu_count(), members, |v, member| {
116+
member.visit_with(v);
117+
});
118+
}
119+
114120
fn visit_constructor(&mut self, f: &Constructor) {
115121
check!(&f.params);
116122

crates/swc_ecma_minifier/src/compress/optimize/util.rs

Lines changed: 80 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -308,24 +308,6 @@ enum FinalizerMode {
308308
impl VisitMut for Finalizer<'_> {
309309
noop_visit_mut_type!();
310310

311-
fn visit_mut_callee(&mut self, e: &mut Callee) {
312-
e.visit_mut_children_with(self);
313-
314-
if let Callee::Expr(e) = e {
315-
self.check(e, FinalizerMode::Callee);
316-
}
317-
}
318-
319-
fn visit_mut_member_expr(&mut self, e: &mut MemberExpr) {
320-
e.visit_mut_children_with(self);
321-
322-
if let MemberProp::Computed(ref mut prop) = e.prop {
323-
if let Expr::Lit(Lit::Num(..)) = &*prop.expr {
324-
self.check(&mut e.obj, FinalizerMode::MemberAccess);
325-
}
326-
}
327-
}
328-
329311
fn visit_mut_bin_expr(&mut self, e: &mut BinExpr) {
330312
e.visit_mut_children_with(self);
331313

@@ -342,65 +324,17 @@ impl VisitMut for Finalizer<'_> {
342324
}
343325
}
344326

345-
fn visit_mut_var_declarators(&mut self, n: &mut Vec<VarDeclarator>) {
346-
n.visit_mut_children_with(self);
347-
348-
n.retain(|v| !v.name.is_invalid());
349-
}
350-
351-
fn visit_mut_var_declarator(&mut self, n: &mut VarDeclarator) {
352-
n.visit_mut_children_with(self);
353-
354-
if n.init.is_none() {
355-
if let Pat::Ident(i) = &n.name {
356-
if self.vars_to_remove.contains(&i.to_id()) {
357-
n.name.take();
358-
}
359-
}
360-
}
361-
}
362-
363-
fn visit_mut_opt_var_decl_or_expr(&mut self, n: &mut Option<VarDeclOrExpr>) {
364-
n.visit_mut_children_with(self);
365-
366-
if let Some(VarDeclOrExpr::VarDecl(v)) = n {
367-
if v.decls.is_empty() {
368-
*n = None;
369-
}
370-
}
371-
}
372-
373-
fn visit_mut_stmt(&mut self, n: &mut Stmt) {
374-
n.visit_mut_children_with(self);
327+
fn visit_mut_callee(&mut self, e: &mut Callee) {
328+
e.visit_mut_children_with(self);
375329

376-
if let Stmt::Decl(Decl::Var(v)) = n {
377-
if v.decls.is_empty() {
378-
n.take();
379-
}
330+
if let Callee::Expr(e) = e {
331+
self.check(e, FinalizerMode::Callee);
380332
}
381333
}
382334

383-
fn visit_mut_prop_or_spreads(&mut self, n: &mut Vec<PropOrSpread>) {
384-
self.maybe_par(*HEAVY_TASK_PARALLELS, n, |v, n| {
385-
n.visit_mut_with(v);
386-
});
387-
}
388-
389-
fn visit_mut_expr_or_spreads(&mut self, n: &mut Vec<ExprOrSpread>) {
390-
self.maybe_par(*HEAVY_TASK_PARALLELS, n, |v, n| {
391-
n.visit_mut_with(v);
392-
});
393-
}
394-
395-
fn visit_mut_opt_vec_expr_or_spreads(&mut self, n: &mut Vec<Option<ExprOrSpread>>) {
396-
self.maybe_par(*HEAVY_TASK_PARALLELS, n, |v, n| {
397-
n.visit_mut_with(v);
398-
});
399-
}
400-
401-
fn visit_mut_exprs(&mut self, n: &mut Vec<Box<Expr>>) {
402-
self.maybe_par(*HEAVY_TASK_PARALLELS, n, |v, n| {
403-
n.visit_mut_with(v);
335+
fn visit_mut_class_members(&mut self, members: &mut Vec<ClassMember>) {
336+
self.maybe_par(*HEAVY_TASK_PARALLELS, members, |v, member| {
337+
member.visit_mut_with(v);
404338
});
405339
}
406340

@@ -436,17 +370,89 @@ impl VisitMut for Finalizer<'_> {
436370
n.visit_mut_children_with(self);
437371
}
438372

439-
fn visit_mut_stmts(&mut self, n: &mut Vec<Stmt>) {
373+
fn visit_mut_expr_or_spreads(&mut self, n: &mut Vec<ExprOrSpread>) {
374+
self.maybe_par(*HEAVY_TASK_PARALLELS, n, |v, n| {
375+
n.visit_mut_with(v);
376+
});
377+
}
378+
379+
fn visit_mut_exprs(&mut self, n: &mut Vec<Box<Expr>>) {
440380
self.maybe_par(*HEAVY_TASK_PARALLELS, n, |v, n| {
441381
n.visit_mut_with(v);
442382
});
443383
}
444384

385+
fn visit_mut_member_expr(&mut self, e: &mut MemberExpr) {
386+
e.visit_mut_children_with(self);
387+
388+
if let MemberProp::Computed(ref mut prop) = e.prop {
389+
if let Expr::Lit(Lit::Num(..)) = &*prop.expr {
390+
self.check(&mut e.obj, FinalizerMode::MemberAccess);
391+
}
392+
}
393+
}
394+
445395
fn visit_mut_module_items(&mut self, n: &mut Vec<ModuleItem>) {
446396
self.maybe_par(*HEAVY_TASK_PARALLELS, n, |v, n| {
447397
n.visit_mut_with(v);
448398
});
449399
}
400+
401+
fn visit_mut_opt_var_decl_or_expr(&mut self, n: &mut Option<VarDeclOrExpr>) {
402+
n.visit_mut_children_with(self);
403+
404+
if let Some(VarDeclOrExpr::VarDecl(v)) = n {
405+
if v.decls.is_empty() {
406+
*n = None;
407+
}
408+
}
409+
}
410+
411+
fn visit_mut_opt_vec_expr_or_spreads(&mut self, n: &mut Vec<Option<ExprOrSpread>>) {
412+
self.maybe_par(*HEAVY_TASK_PARALLELS, n, |v, n| {
413+
n.visit_mut_with(v);
414+
});
415+
}
416+
417+
fn visit_mut_prop_or_spreads(&mut self, n: &mut Vec<PropOrSpread>) {
418+
self.maybe_par(*HEAVY_TASK_PARALLELS, n, |v, n| {
419+
n.visit_mut_with(v);
420+
});
421+
}
422+
423+
fn visit_mut_stmt(&mut self, n: &mut Stmt) {
424+
n.visit_mut_children_with(self);
425+
426+
if let Stmt::Decl(Decl::Var(v)) = n {
427+
if v.decls.is_empty() {
428+
n.take();
429+
}
430+
}
431+
}
432+
433+
fn visit_mut_stmts(&mut self, n: &mut Vec<Stmt>) {
434+
self.maybe_par(*HEAVY_TASK_PARALLELS, n, |v, n| {
435+
n.visit_mut_with(v);
436+
});
437+
}
438+
439+
fn visit_mut_var_declarator(&mut self, n: &mut VarDeclarator) {
440+
n.visit_mut_children_with(self);
441+
442+
if n.init.is_none() {
443+
if let Pat::Ident(i) = &n.name {
444+
if self.vars_to_remove.contains(&i.to_id()) {
445+
n.name.take();
446+
}
447+
}
448+
}
449+
}
450+
451+
fn visit_mut_var_declarators(&mut self, n: &mut Vec<VarDeclarator>) {
452+
n.visit_mut_children_with(self);
453+
454+
n.retain(|v| !v.name.is_invalid());
455+
}
450456
}
451457

452458
pub(crate) struct NormalMultiReplacer<'a> {

crates/swc_ecma_minifier/src/compress/pure/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl Pure<'_> {
184184
where
185185
N: for<'aa> VisitMutWith<Pure<'aa>> + Send + Sync,
186186
{
187-
self.maybe_par(cpu_count() * 8, nodes, |v, node| {
187+
self.maybe_par(cpu_count() * 2, nodes, |v, node| {
188188
node.visit_mut_with(v);
189189
});
190190
}
@@ -243,20 +243,6 @@ impl VisitMut for Pure<'_> {
243243
self.drop_arguments_of_symbol_call(e);
244244
}
245245

246-
fn visit_mut_opt_call(&mut self, opt_call: &mut OptCall) {
247-
{
248-
let ctx = Ctx {
249-
is_callee: true,
250-
..self.ctx
251-
};
252-
opt_call.callee.visit_mut_with(&mut *self.with_ctx(ctx));
253-
}
254-
255-
opt_call.args.visit_mut_with(self);
256-
257-
self.eval_spread_array(&mut opt_call.args);
258-
}
259-
260246
fn visit_mut_class_member(&mut self, m: &mut ClassMember) {
261247
m.visit_mut_children_with(self);
262248

@@ -268,7 +254,7 @@ impl VisitMut for Pure<'_> {
268254
}
269255

270256
fn visit_mut_class_members(&mut self, m: &mut Vec<ClassMember>) {
271-
m.visit_mut_children_with(self);
257+
self.visit_par(m);
272258

273259
m.retain(|m| {
274260
if let ClassMember::Empty(..) = m {
@@ -673,6 +659,20 @@ impl VisitMut for Pure<'_> {
673659
e.args.visit_mut_with(self);
674660
}
675661

662+
fn visit_mut_opt_call(&mut self, opt_call: &mut OptCall) {
663+
{
664+
let ctx = Ctx {
665+
is_callee: true,
666+
..self.ctx
667+
};
668+
opt_call.callee.visit_mut_with(&mut *self.with_ctx(ctx));
669+
}
670+
671+
opt_call.args.visit_mut_with(self);
672+
673+
self.eval_spread_array(&mut opt_call.args);
674+
}
675+
676676
fn visit_mut_opt_var_decl_or_expr(&mut self, n: &mut Option<VarDeclOrExpr>) {
677677
n.visit_mut_children_with(self);
678678

crates/swc_ecma_minifier/src/util/base54.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,24 @@ impl Visit for CharFreqAnalyzer<'_> {
326326

327327
visit_obj_and_computed!();
328328

329+
fn visit_class_members(&mut self, members: &[ClassMember]) {
330+
self.maybe_par(cpu_count(), members, |v, member| {
331+
member.visit_with(v);
332+
});
333+
}
334+
335+
fn visit_expr_or_spreads(&mut self, n: &[ExprOrSpread]) {
336+
self.maybe_par(cpu_count(), n, |v, n| {
337+
n.visit_with(v);
338+
});
339+
}
340+
341+
fn visit_exprs(&mut self, exprs: &[Box<Expr>]) {
342+
self.maybe_par(cpu_count(), exprs, |v, expr| {
343+
expr.visit_with(v);
344+
});
345+
}
346+
329347
fn visit_ident(&mut self, i: &Ident) {
330348
if i.ctxt == self.unresolved_ctxt && i.sym != "arguments" {
331349
return;
@@ -339,31 +357,9 @@ impl Visit for CharFreqAnalyzer<'_> {
339357
self.freq.scan(&i.sym, -1);
340358
}
341359

342-
fn visit_prop_name(&mut self, n: &PropName) {
343-
match n {
344-
PropName::Ident(_) => {}
345-
PropName::Str(_) => {}
346-
PropName::Num(_) => {}
347-
PropName::Computed(e) => e.visit_with(self),
348-
PropName::BigInt(_) => {}
349-
}
350-
}
351-
352360
/// This is preserved anyway
353361
fn visit_module_export_name(&mut self, _: &ModuleExportName) {}
354362

355-
fn visit_expr_or_spreads(&mut self, n: &[ExprOrSpread]) {
356-
self.maybe_par(cpu_count(), n, |v, n| {
357-
n.visit_with(v);
358-
});
359-
}
360-
361-
fn visit_exprs(&mut self, exprs: &[Box<Expr>]) {
362-
self.maybe_par(cpu_count(), exprs, |v, expr| {
363-
expr.visit_with(v);
364-
});
365-
}
366-
367363
fn visit_module_items(&mut self, items: &[ModuleItem]) {
368364
self.maybe_par(cpu_count(), items, |v, item| {
369365
item.visit_with(v);
@@ -376,6 +372,16 @@ impl Visit for CharFreqAnalyzer<'_> {
376372
});
377373
}
378374

375+
fn visit_prop_name(&mut self, n: &PropName) {
376+
match n {
377+
PropName::Ident(_) => {}
378+
PropName::Str(_) => {}
379+
PropName::Num(_) => {}
380+
PropName::Computed(e) => e.visit_with(self),
381+
PropName::BigInt(_) => {}
382+
}
383+
}
384+
379385
fn visit_prop_or_spreads(&mut self, n: &[PropOrSpread]) {
380386
self.maybe_par(cpu_count(), n, |v, n| {
381387
n.visit_with(v);

crates/swc_ecma_transforms_base/src/rename/ops.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,12 @@ where
522522
}
523523
}
524524

525+
fn visit_mut_class_members(&mut self, members: &mut Vec<ClassMember>) {
526+
self.maybe_par(cpu_count(), members, |v, member| {
527+
member.visit_mut_with(v);
528+
});
529+
}
530+
525531
fn visit_mut_prop_or_spreads(&mut self, n: &mut Vec<PropOrSpread>) {
526532
self.maybe_par(cpu_count() * 100, n, |v, n| {
527533
n.visit_mut_with(v);

0 commit comments

Comments
 (0)