Skip to content

Commit

Permalink
Use rayon
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed May 4, 2021
1 parent 25d5639 commit 86d18a7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
17 changes: 13 additions & 4 deletions native/babel-compat/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(type_name_of_val)]

use rayon::prelude::*;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::sync::Arc;
Expand Down Expand Up @@ -33,7 +34,7 @@ mod typescript;
pub struct Context {
pub fm: Arc<SourceFile>,
pub cm: Lrc<SourceMap>,
pub comments: Arc<dyn Comments>,
pub comments: Arc<dyn Comments + Send + Sync>,
}

impl Context {
Expand Down Expand Up @@ -128,19 +129,27 @@ impl Context {
}

pub trait Babelify {
type Output: Serialize + DeserializeOwned;
type Output: Serialize + DeserializeOwned + Send;

fn parallel(_cnt: usize) -> bool {
false
}

fn babelify(self, ctx: &Context) -> Self::Output;
}

impl<T> Babelify for Vec<T>
where
T: Babelify,
T: Babelify + Send,
{
type Output = Vec<T::Output>;

fn babelify(self, ctx: &Context) -> Self::Output {
self.into_iter().map(|v| v.babelify(ctx)).collect()
if T::parallel(self.len()) {
self.into_par_iter().map(|v| v.babelify(ctx)).collect()
} else {
self.into_iter().map(|v| v.babelify(ctx)).collect()
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion native/babel-compat/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ pub enum ModuleItemOutput {
impl Babelify for ModuleItem {
type Output = ModuleItemOutput;

fn parallel(cnt: usize) -> bool {
cnt >= 32
}

fn babelify(self, ctx: &Context) -> Self::Output {
match self {
ModuleItem::ModuleDecl(d) => ModuleItemOutput::ModuleDecl(d.babelify(ctx).into()),
Expand Down Expand Up @@ -168,7 +172,7 @@ fn extract_all_comments(program: &Program, ctx: &Context) -> Vec<Comment> {
}

struct CommentCollector {
comments: Arc<dyn Comments>,
comments: Arc<dyn Comments + Send + Sync>,
collected: Vec<Comment>,
}

Expand Down
4 changes: 4 additions & 0 deletions native/babel-compat/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ impl Babelify for BlockStmt {
impl Babelify for Stmt {
type Output = Statement;

fn parallel(cnt: usize) -> bool {
32 >= cnt
}

fn babelify(self, ctx: &Context) -> Self::Output {
match self {
Stmt::Block(s) => Statement::Block(s.babelify(ctx)),
Expand Down

0 comments on commit 86d18a7

Please sign in to comment.