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/transforms): Remove wrong parallelism #7889

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
241 changes: 5 additions & 236 deletions crates/swc_ecma_transforms_macros/src/parallel.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(non_snake_case)]

use pmutil::q;
use proc_macro2::{Span, TokenStream};
use syn::{Expr, Ident, ImplItem, ImplItemFn, ItemImpl, Meta, Type};
Expand Down Expand Up @@ -29,11 +31,9 @@ pub fn expand(attr: TokenStream, mut item: ItemImpl) -> ItemImpl {
mode,
"module_items",
explode,
100,
)));
item.items.push(ImplItem::Fn(make_par_visit_method(
mode, "stmts", explode, 100,
)));
item.items
.push(ImplItem::Fn(make_par_visit_method(mode, "stmts", explode)));

item
}
Expand Down Expand Up @@ -85,20 +85,15 @@ fn explode_hook_method_name(explode: bool, suffix: &str) -> Option<Ident> {
}
}

fn make_par_visit_method(mode: Mode, suffix: &str, explode: bool, threshold: usize) -> ImplItemFn {
fn make_par_visit_method(mode: Mode, suffix: &str, explode: bool) -> ImplItemFn {
let method_name = Ident::new(&format!("{}_{}", mode.prefix(), suffix), Span::call_site());
let hook = post_visit_hook(mode, suffix);
let explode_method_name = explode_hook_method_name(explode, suffix);

let threshold = q!(Vars { threshold }, {
(swc_ecma_transforms_base::perf::cpu_count() * threshold)
});

match (mode, explode_method_name) {
(Mode::Fold, Some(explode_method_name)) => q!(
Vars {
NodeType: node_type(suffix),
threshold,
method_name,
hook,
explode_method_name,
Expand All @@ -109,63 +104,6 @@ fn make_par_visit_method(mode: Mode, suffix: &str, explode: bool, threshold: usi
use swc_ecma_transforms_base::perf::{ParExplode, Parallel};
use swc_ecma_visit::FoldWith;

#[cfg(feature = "rayon")]
if nodes.len() >= threshold || option_env!("SWC_FORCE_CONCURRENT") == Some("1")
{
use rayon::prelude::*;

let (visitor, mut nodes) = ::swc_common::GLOBALS.with(|globals| {
swc_ecma_transforms_base::helpers::HELPERS.with(|helpers| {
HANDLER.with(|handler| {
nodes
.into_par_iter()
.map(|node| {
::swc_common::GLOBALS.set(&globals, || {
swc_ecma_transforms_base::helpers::HELPERS.set(
helpers,
|| {
HANDLER.set(handler, || {
let mut visitor =
Parallel::create(&*self);
let node = node.fold_with(&mut visitor);
let mut nodes = Vec::with_capacity(4);

ParExplode::explode_method_name(
&mut visitor,
&mut nodes,
);

nodes.push(node);

(visitor, nodes)
})
},
)
})
})
.reduce(
|| (Parallel::create(&*self), vec![]),
|mut a, b| {
Parallel::merge(&mut a.0, b.0);

a.1.extend(b.1);

a
},
)
})
})
});

Parallel::merge(self, visitor);

{
hook;
}

return nodes;
}

let mut buf = Vec::with_capacity(nodes.len());

for node in nodes {
Expand All @@ -189,7 +127,6 @@ fn make_par_visit_method(mode: Mode, suffix: &str, explode: bool, threshold: usi
(Mode::Fold, None) => q!(
Vars {
NodeType: node_type(suffix),
threshold,
method_name,
hook,
},
Expand All @@ -199,65 +136,6 @@ fn make_par_visit_method(mode: Mode, suffix: &str, explode: bool, threshold: usi
use swc_ecma_transforms_base::perf::Parallel;
use swc_ecma_visit::FoldWith;

#[cfg(feature = "rayon")]
if nodes.len() >= threshold || option_env!("SWC_FORCE_CONCURRENT") == Some("1")
{
use rayon::prelude::*;

let (visitor, mut nodes) = ::swc_common::GLOBALS.with(|globals| {
swc_ecma_transforms_base::helpers::HELPERS.with(|helpers| {
HANDLER.with(|handler| {
nodes
.into_par_iter()
.map(|node| {
::swc_common::GLOBALS.set(&globals, || {
swc_ecma_transforms_base::helpers::HELPERS.set(
helpers,
|| {
HANDLER.set(handler, || {
let mut visitor =
Parallel::create(&*self);
let node = node.fold_with(&mut visitor);

(visitor, node)
})
},
)
})
})
.fold(
|| (Parallel::create(&*self), vec![]),
|mut a, b| {
Parallel::merge(&mut a.0, b.0);

a.1.push(b.1);

a
},
)
.reduce(
|| (Parallel::create(&*self), vec![]),
|mut a, b| {
Parallel::merge(&mut a.0, b.0);

a.1.extend(b.1);

a
},
)
})
})
});

Parallel::merge(self, visitor);

{
hook;
}

return nodes;
}

let mut nodes = nodes.fold_children_with(self);
{
hook;
Expand All @@ -272,7 +150,6 @@ fn make_par_visit_method(mode: Mode, suffix: &str, explode: bool, threshold: usi
(Mode::VisitMut, Some(explode_method_name)) => q!(
Vars {
NodeType: node_type(suffix),
threshold,
method_name,
hook,
explode_method_name
Expand All @@ -285,66 +162,6 @@ fn make_par_visit_method(mode: Mode, suffix: &str, explode: bool, threshold: usi
use swc_ecma_transforms_base::perf::{ParExplode, Parallel};
use swc_ecma_visit::VisitMutWith;

#[cfg(feature = "rayon")]
if nodes.len() >= threshold || option_env!("SWC_FORCE_CONCURRENT") == Some("1")
{
::swc_common::GLOBALS.with(|globals| {
swc_ecma_transforms_base::helpers::HELPERS.with(|helpers| {
HANDLER.with(|handler| {
use rayon::prelude::*;

let (visitor, new_nodes) = take(nodes)
.into_par_iter()
.map(|mut node| {
::swc_common::GLOBALS.set(&globals, || {
swc_ecma_transforms_base::helpers::HELPERS.set(
helpers,
|| {
HANDLER.set(handler, || {
let mut visitor =
Parallel::create(&*self);
node.visit_mut_with(&mut visitor);

let mut nodes = Vec::with_capacity(4);

ParExplode::explode_method_name(
&mut visitor,
&mut nodes,
);

nodes.push(node);

(visitor, nodes)
})
},
)
})
})
.reduce(
|| (Parallel::create(&*self), vec![]),
|mut a, b| {
Parallel::merge(&mut a.0, b.0);

a.1.extend(b.1);

a
},
);

Parallel::merge(self, visitor);

{
hook;
}

*nodes = new_nodes;
})
})
});

return;
}

let mut buf = Vec::with_capacity(nodes.len());

for mut node in take(nodes) {
Expand All @@ -367,7 +184,6 @@ fn make_par_visit_method(mode: Mode, suffix: &str, explode: bool, threshold: usi
(Mode::VisitMut, None) => q!(
Vars {
NodeType: node_type(suffix),
threshold,
method_name,
hook,
},
Expand All @@ -377,53 +193,6 @@ fn make_par_visit_method(mode: Mode, suffix: &str, explode: bool, threshold: usi
use swc_ecma_transforms_base::perf::Parallel;
use swc_ecma_visit::VisitMutWith;

#[cfg(feature = "rayon")]
if nodes.len() >= threshold || option_env!("SWC_FORCE_CONCURRENT") == Some("1")
{
::swc_common::GLOBALS.with(|globals| {
swc_ecma_transforms_base::helpers::HELPERS.with(|helpers| {
HANDLER.with(|handler| {
use rayon::prelude::*;

let visitor = nodes
.into_par_iter()
.map(|node| {
::swc_common::GLOBALS.set(&globals, || {
swc_ecma_transforms_base::helpers::HELPERS.set(
helpers,
|| {
HANDLER.set(handler, || {
let mut visitor =
Parallel::create(&*self);
node.visit_mut_with(&mut visitor);

visitor
})
},
)
})
})
.reduce(
|| Parallel::create(&*self),
|mut a, b| {
Parallel::merge(&mut a, b);

a
},
);

Parallel::merge(self, visitor);

{
hook;
}
})
})
});

return;
}

nodes.visit_mut_children_with(self);
{
hook;
Expand Down
22 changes: 18 additions & 4 deletions crates/swc_ecma_transforms_optimization/src/inline_globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use swc_common::{
sync::Lrc,
};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::perf::Parallel;
use swc_ecma_transforms_macros::parallel;
use swc_ecma_utils::{collect_decls, NodeIgnoringSpan};
use swc_ecma_transforms_base::perf::{ParVisitMut, Parallel};
use swc_ecma_utils::{collect_decls, parallel::cpu_count, NodeIgnoringSpan};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};

/// The key will be compared using [EqIgnoreSpan::eq_ignore_span], and matched
Expand Down Expand Up @@ -63,7 +62,6 @@ impl Parallel for InlineGlobals {
fn merge(&mut self, _: Self) {}
}

#[parallel]
impl VisitMut for InlineGlobals {
noop_visit_mut_type!();

Expand Down Expand Up @@ -208,6 +206,22 @@ impl VisitMut for InlineGlobals {

script.visit_mut_children_with(self);
}

fn visit_mut_prop_or_spreads(&mut self, n: &mut Vec<PropOrSpread>) {
self.visit_mut_par(cpu_count() * 8, n);
}

fn visit_mut_expr_or_spreads(&mut self, n: &mut Vec<ExprOrSpread>) {
self.visit_mut_par(cpu_count() * 8, n);
}

fn visit_mut_opt_vec_expr_or_spreads(&mut self, n: &mut Vec<Option<ExprOrSpread>>) {
self.visit_mut_par(cpu_count() * 8, n);
}

fn visit_mut_exprs(&mut self, n: &mut Vec<Box<Expr>>) {
self.visit_mut_par(cpu_count() * 8, n);
}
}

#[cfg(test)]
Expand Down
2 changes: 0 additions & 2 deletions crates/swc_ecma_transforms_optimization/src/json_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use serde_json::Value;
use swc_common::{util::take::Take, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::perf::Parallel;
use swc_ecma_transforms_macros::parallel;
use swc_ecma_utils::{calc_literal_cost, member_expr, ExprFactory};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};

Expand Down Expand Up @@ -54,7 +53,6 @@ impl Default for JsonParse {
}
}

#[parallel]
impl VisitMut for JsonParse {
noop_visit_mut_type!();

Expand Down