Skip to content

Commit

Permalink
perf(sourcemap): search lines count at parallel (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Apr 9, 2024
1 parent c9d018d commit 267fc2a
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ dunce = "1.0.4"
futures = "0.3.30"
index_vec = "0.1.3"
insta = "1.38.0"
memchr = "2.7.2"
mimalloc = "0.1.39"
napi = { version = "3.0.0-alpha.0", features = ["async"] }
napi-build = { version = "2.1.2" }
Expand Down
7 changes: 6 additions & 1 deletion crates/rolldown/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,15 @@ impl Chunk {
rendered_module,
rendered_content,
sourcemap,
lines_count,
} = module_render_output;
concat_source.add_source(Box::new(RawSource::new(format!("// {module_pretty_path}",))));
if let Some(sourcemap) = sourcemap {
concat_source.add_source(Box::new(SourceMapSource::new(rendered_content, sourcemap)));
concat_source.add_source(Box::new(SourceMapSource::new(
rendered_content,
sourcemap,
lines_count,
)));
} else {
concat_source.add_source(Box::new(RawSource::new(rendered_content)));
}
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/src/types/module_render_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ pub struct ModuleRenderOutput<'a> {
pub rendered_module: RenderedModule,
pub rendered_content: String,
pub sourcemap: Option<SourceMap>,
pub lines_count: u32,
}
4 changes: 3 additions & 1 deletion crates/rolldown/src/utils/render_normal_module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rolldown_common::{NormalModule, RenderedModule};
use rolldown_oxc_utils::{OxcCompiler, OxcProgram};
use rolldown_sourcemap::collapse_sourcemaps;
use rolldown_sourcemap::{collapse_sourcemaps, lines_count};

use crate::{types::module_render_output::ModuleRenderOutput, SharedOptions};

Expand All @@ -25,6 +25,8 @@ pub fn render_normal_module<'a>(
module_path: module.resource_id.expect_file().as_str(),
module_pretty_path: &module.pretty_path,
rendered_module: RenderedModule { code: None },
// Search lines count from rendered content has a little overhead, so make it at parallel.
lines_count: lines_count(&render_output.source_text),
rendered_content: render_output.source_text,
sourcemap: if options.sourcemap.is_hidden() {
None
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown_sourcemap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ doctest = false
test = false

[dependencies]
memchr = { workspace = true }
oxc = { workspace = true }
rustc-hash = { workspace = true }
sugar_path = { workspace = true }
20 changes: 16 additions & 4 deletions crates/rolldown_sourcemap/src/concat_sourcemap.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// cSpell:disable
use oxc::sourcemap::{ConcatSourceMapBuilder, SourceMap};

use crate::lines_count;

pub trait Source {
fn sourcemap(&self) -> Option<&SourceMap>;
fn content(&self) -> &String;
fn lines_count(&self) -> u32;
#[allow(clippy::wrong_self_convention)]
fn into_concat_source(
&self,
Expand Down Expand Up @@ -32,6 +35,10 @@ impl Source for RawSource {
&self.content
}

fn lines_count(&self) -> u32 {
lines_count(&self.content)
}

fn into_concat_source(
&self,
final_source: &mut String,
Expand All @@ -45,11 +52,12 @@ impl Source for RawSource {
pub struct SourceMapSource {
content: String,
sourcemap: SourceMap,
lines_count: u32,
}

impl SourceMapSource {
pub fn new(content: String, sourcemap: SourceMap) -> Self {
Self { content, sourcemap }
pub fn new(content: String, sourcemap: SourceMap, lines_count: u32) -> Self {
Self { content, sourcemap, lines_count }
}
}

Expand All @@ -62,6 +70,10 @@ impl Source for SourceMapSource {
&self.content
}

fn lines_count(&self) -> u32 {
self.lines_count
}

#[allow(clippy::cast_possible_truncation)]
fn into_concat_source(
&self,
Expand Down Expand Up @@ -110,7 +122,7 @@ impl ConcatSource {
source.into_concat_source(&mut final_source, &mut sourcemap_builder, line_offset);
if index < source_len - 1 {
final_source.push('\n');
line_offset += source.content().matches('\n').count() as u32 + 1; // +1 for the newline
line_offset += source.lines_count() + 1; // +1 for the newline
}
}

Expand Down Expand Up @@ -141,7 +153,7 @@ mod tests {
"names":[]
}"#
)
.unwrap().into(),
.unwrap().into(),3
)));

let (content, map) = {
Expand Down
3 changes: 2 additions & 1 deletion crates/rolldown_sourcemap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use sugar_path::SugarPath;
// cSpell:disable
pub use concat_sourcemap::{ConcatSource, RawSource, SourceMapSource};
pub use oxc::sourcemap::{SourceMap, SourcemapVisualizer};

mod lines_count;
pub use lines_count::lines_count;
use oxc::sourcemap::SourceMapBuilder;
mod concat_sourcemap;

Expand Down
14 changes: 14 additions & 0 deletions crates/rolldown_sourcemap/src/lines_count.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use memchr::memmem;

#[allow(clippy::cast_possible_truncation)]
#[inline]
pub fn lines_count(str: &str) -> u32 {
memmem::find_iter(str.as_bytes(), "\n").count() as u32
}

#[test]
fn test() {
assert_eq!(lines_count("a\nb\nc"), 2);
assert_eq!(lines_count("a\nb\nc\n"), 3);
assert_eq!(lines_count("a"), 0);
}
4 changes: 3 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
"vitepress",
"Yinan",
"Yunfei",
"sourcemaps"
"sourcemaps",
"memmem",
"memchr"
]
}

0 comments on commit 267fc2a

Please sign in to comment.