From 0705452eb7ffb829364ea61b3f0ed17515ff113b Mon Sep 17 00:00:00 2001 From: Boshen Date: Tue, 7 May 2024 23:41:30 +0800 Subject: [PATCH] feat(sourcemap): add "rayon" feature --- Cargo.lock | 1 + Cargo.toml | 2 ++ crates/oxc_sourcemap/Cargo.toml | 10 ++++++-- crates/oxc_sourcemap/src/encode.rs | 38 +++++++++++++++++++++++------- tasks/benchmark/Cargo.toml | 2 +- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a9cd15306e3ba..1818bc05fe3dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1620,6 +1620,7 @@ name = "oxc_sourcemap" version = "0.12.5" dependencies = [ "base64-simd", + "cfg-if", "rayon", "rustc-hash", "serde", diff --git a/Cargo.toml b/Cargo.toml index afd1ad2e90bdc..0737b7af4ac92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -171,6 +171,8 @@ similar = "2.5.0" textwrap = "0.16.0" unicode-width = "0.1.12" saphyr = "0.0.1" +base64-simd = "0.8" +cfg-if = "1.0.0" [workspace.metadata.cargo-shear] ignored = ["napi", "oxc_traverse"] diff --git a/crates/oxc_sourcemap/Cargo.toml b/crates/oxc_sourcemap/Cargo.toml index 696ac0e4caa6e..0779dcad0824e 100644 --- a/crates/oxc_sourcemap/Cargo.toml +++ b/crates/oxc_sourcemap/Cargo.toml @@ -20,7 +20,13 @@ doctest = false [dependencies] rustc-hash = { workspace = true } -rayon = { workspace = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } -base64-simd = "0.8" +base64-simd = { workspace = true } +cfg-if = { workspace = true } + +rayon = { workspace = true, optional = true } + +[features] +default = [] +rayon = ["dep:rayon"] diff --git a/crates/oxc_sourcemap/src/encode.rs b/crates/oxc_sourcemap/src/encode.rs index 3ddd29c55ab0d..66c49ceb35dd7 100644 --- a/crates/oxc_sourcemap/src/encode.rs +++ b/crates/oxc_sourcemap/src/encode.rs @@ -4,6 +4,7 @@ use crate::error::{Error, Result}; /// - Quote `source_content` at parallel. /// - If you using `ConcatSourceMapBuilder`, serialize `tokens` to vlq `mappings` at parallel. use crate::{token::TokenChunk, SourceMap, Token}; +#[cfg(feature = "rayon")] use rayon::prelude::*; // Here using `serde_json::to_string` to serialization `names/source_contents/sources`. @@ -40,11 +41,21 @@ pub fn encode(sourcemap: &SourceMap) -> Result { // Quote `source_content` at parallel. if let Some(source_contents) = &sourcemap.source_contents { buf.push_str("],\"sourcesContent\":["); - let quote_source_contents = source_contents - .par_iter() - .map(|x| serde_json::to_string(x.as_ref())) - .collect::, serde_json::Error>>() - .map_err(Error::from)?; + cfg_if::cfg_if! { + if #[cfg(feature = "rayon")] { + let quote_source_contents = source_contents + .par_iter() + .map(|x| serde_json::to_string(x.as_ref())) + .collect::, serde_json::Error>>() + .map_err(Error::from)?; + } else { + let quote_source_contents = source_contents + .iter() + .map(|x| serde_json::to_string(x.as_ref())) + .collect::, serde_json::Error>>() + .map_err(Error::from)?; + } + }; buf.push_str("e_source_contents.join(",")); } if let Some(x_google_ignore_list) = &sourcemap.x_google_ignore_list { @@ -70,10 +81,19 @@ fn serialize_sourcemap_mappings(sm: &SourceMap) -> String { }, |token_chunks| { // Serialize `tokens` to vlq `mappings` at parallel. - token_chunks - .par_iter() - .map(|token_chunk| serialize_mappings(&sm.tokens, token_chunk)) - .collect::() + cfg_if::cfg_if! { + if #[cfg(feature = "rayon")] { + token_chunks + .par_iter() + .map(|token_chunk| serialize_mappings(&sm.tokens, token_chunk)) + .collect::() + } else { + token_chunks + .iter() + .map(|token_chunk| serialize_mappings(&sm.tokens, token_chunk)) + .collect::() + } + } }, ) } diff --git a/tasks/benchmark/Cargo.toml b/tasks/benchmark/Cargo.toml index b2c65305f49b3..40534aa23b39b 100644 --- a/tasks/benchmark/Cargo.toml +++ b/tasks/benchmark/Cargo.toml @@ -75,7 +75,7 @@ oxc_span = { workspace = true, optional = true } oxc_tasks_common = { workspace = true, optional = true } oxc_transformer = { workspace = true, optional = true } oxc_codegen = { workspace = true, optional = true } -oxc_sourcemap = { workspace = true, optional = true } +oxc_sourcemap = { workspace = true, features = ["rayon"], optional = true } criterion = { package = "criterion2", version = "0.8.0", default-features = false } serde = { workspace = true, optional = true }