feat(sourcemap): add "rayon" feature (#3198)

This commit is contained in:
Boshen 2024-05-07 23:47:36 +08:00 committed by GitHub
parent 9b93a17429
commit 7363e14335
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 41 additions and 12 deletions

1
Cargo.lock generated
View file

@ -1620,6 +1620,7 @@ name = "oxc_sourcemap"
version = "0.12.5"
dependencies = [
"base64-simd",
"cfg-if",
"rayon",
"rustc-hash",
"serde",

View file

@ -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"]

View file

@ -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"]

View file

@ -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<String> {
// 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::<std::result::Result<Vec<_>, 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::<std::result::Result<Vec<_>, 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::<std::result::Result<Vec<_>, serde_json::Error>>()
.map_err(Error::from)?;
}
};
buf.push_str(&quote_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::<String>()
cfg_if::cfg_if! {
if #[cfg(feature = "rayon")] {
token_chunks
.par_iter()
.map(|token_chunk| serialize_mappings(&sm.tokens, token_chunk))
.collect::<String>()
} else {
token_chunks
.iter()
.map(|token_chunk| serialize_mappings(&sm.tokens, token_chunk))
.collect::<String>()
}
}
},
)
}

View file

@ -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 }