Skip to content

Commit

Permalink
feat(sourcemap): add "rayon" feature (#3198)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed May 7, 2024
1 parent 9b93a17 commit 7363e14
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
10 changes: 8 additions & 2 deletions crates/oxc_sourcemap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
38 changes: 29 additions & 9 deletions crates/oxc_sourcemap/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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 {
Expand All @@ -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>()
}
}
},
)
}
Expand Down
2 changes: 1 addition & 1 deletion tasks/benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 7363e14

Please sign in to comment.