Skip to content

Commit

Permalink
perf: better source map struct
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Dec 3, 2024
1 parent 3c00567 commit 9f819cc
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 154 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ path = "benches/bench.rs"
harness = false

[dependencies]
serde = { version = "1", features = ["derive"] }
serde = { version = "1", features = ["derive", "rc"] }
serde_json = "1"
dyn-clone = "1"
rustc-hash = "1"
Expand Down
3 changes: 1 addition & 2 deletions src/cached_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ mod tests {
value: "\nconsole.log(1);\n".to_string(),
name: "index.js".to_string(),
source_map: SourceMap::new(
None,
";AACA".to_string(),
";AACA",
vec!["index.js".into()],
vec!["// DELETE IT\nconsole.log(1)".into()],
vec![],
Expand Down
21 changes: 10 additions & 11 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ pub fn get_map<'a, S: StreamChunks<'a>>(
options: &'a MapOptions,
) -> Option<SourceMap> {
let mut mappings_encoder = create_encoder(options.columns);
let mut sources: Vec<Cow<'static, str>> = Vec::new();
let mut sources_content: Vec<Cow<'static, str>> = Vec::new();
let mut names: Vec<Cow<'static, str>> = Vec::new();
let mut sources: Vec<String> = Vec::new();
let mut sources_content: Vec<String> = Vec::new();
let mut names: Vec<String> = Vec::new();

stream.stream_chunks(
&MapOptions {
Expand All @@ -40,12 +40,12 @@ pub fn get_map<'a, S: StreamChunks<'a>>(
&mut |source_index, source, source_content| {
let source_index = source_index as usize;
if sources.len() <= source_index {
sources.resize(source_index + 1, Cow::Borrowed(""));
sources.resize(source_index + 1, "".to_string());
}
sources[source_index] = source.to_string().into();
if let Some(source_content) = source_content {
if sources_content.len() <= source_index {
sources_content.resize(source_index + 1, Cow::Borrowed(""));
sources_content.resize(source_index + 1, "".to_string());
}
sources_content[source_index] = source_content.to_string().into();
}
Expand All @@ -54,14 +54,14 @@ pub fn get_map<'a, S: StreamChunks<'a>>(
&mut |name_index, name| {
let name_index = name_index as usize;
if names.len() <= name_index {
names.resize(name_index + 1, Cow::Borrowed(""));
names.resize(name_index + 1, "".to_string());
}
names[name_index] = name.to_string().into();
},
);
let mappings = mappings_encoder.drain();
(!mappings.is_empty())
.then(|| SourceMap::new(None, mappings, sources, sources_content, names))
.then(|| SourceMap::new(mappings, sources, sources_content, names))
}

/// [StreamChunks] abstraction, see [webpack-sources source.streamChunks](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
Expand Down Expand Up @@ -1149,9 +1149,9 @@ pub fn stream_and_get_source_and_map<'a, S: StreamChunks<'a>>(
on_name: OnName<'_, 'a>,
) -> (GeneratedInfo, Option<SourceMap>) {
let mut mappings_encoder = create_encoder(options.columns);
let mut sources: Vec<Cow<'static, str>> = Vec::new();
let mut sources_content: Vec<Cow<'static, str>> = Vec::new();
let mut names: Vec<Cow<'static, str>> = Vec::new();
let mut sources: Vec<String> = Vec::new();
let mut sources_content: Vec<String> = Vec::new();
let mut names: Vec<String> = Vec::new();

let generated_info = input_source.stream_chunks(
options,
Expand Down Expand Up @@ -1188,7 +1188,6 @@ pub fn stream_and_get_source_and_map<'a, S: StreamChunks<'a>>(
None
} else {
Some(SourceMap::new(
None,
mappings,
sources,
sources_content,
Expand Down
Loading

0 comments on commit 9f819cc

Please sign in to comment.