Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reduce memory allocation while generating sourcemaps #63

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cached_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ mod tests {
source_map: SourceMap::new(
None,
";AACA".to_string(),
vec!["index.js".to_string()],
vec!["// DELETE IT\nconsole.log(1)".to_string()],
vec!["index.js".into()],
vec!["// DELETE IT\nconsole.log(1)".into()],
vec![],
),
})
Expand Down
60 changes: 42 additions & 18 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use rustc_hash::FxHashMap as HashMap;
use std::{borrow::BorrowMut, cell::RefCell, sync::Arc};
use std::{
borrow::{BorrowMut, Cow},
cell::RefCell,
sync::Arc,
};

use crate::{
source::{Mapping, OriginalLocation},
Expand All @@ -17,37 +21,43 @@ pub fn get_map<S: StreamChunks>(
stream: &S,
options: &MapOptions,
) -> Option<SourceMap> {
let mut mappings = Vec::new();
let mut sources = Vec::new();
let mut sources_content = Vec::new();
let mut names = Vec::new();
let mut mappings = Vec::with_capacity(stream.mappings_size_hint());
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();
stream.stream_chunks(
&MapOptions {
columns: options.columns,
final_source: true,
},
// on_chunk
&mut |_, mapping| {
mappings.push(mapping);
},
// on_source
&mut |source_index, source: &str, source_content: Option<&str>| {
let source_index = source_index as usize;
sources.reserve(source_index - sources.len() + 1);
while sources.len() <= source_index {
sources.push("".to_string());
sources.push("".into());
}
sources[source_index] = source.to_owned();
sources[source_index] = source.to_string().into();
if let Some(source_content) = source_content {
sources.reserve(source_index - sources_content.len() + 1);
while sources_content.len() <= source_index {
sources_content.push("".to_string());
sources_content.push("".into());
}
sources_content[source_index] = source_content.to_owned();
sources_content[source_index] = source_content.to_string().into();
}
},
// on_name
&mut |name_index, name: &str| {
let name_index = name_index as usize;
names.reserve(name_index - names.len() + 1);
while names.len() <= name_index {
names.push("".to_string());
names.push("".into());
}
names[name_index] = name.to_owned();
names[name_index] = name.to_string().into();
},
);
let mappings = encode_mappings(&mappings, options);
Expand All @@ -57,6 +67,11 @@ pub fn get_map<S: StreamChunks>(

/// [StreamChunks] abstraction, see [webpack-sources source.streamChunks](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
pub trait StreamChunks {
/// Estimate the number of mappings in the chunk
fn mappings_size_hint(&self) -> usize {
0
}

/// [StreamChunks] abstraction
fn stream_chunks(
&self,
Expand Down Expand Up @@ -1302,16 +1317,25 @@ pub fn stream_chunks_of_combined_source_map(
&mut |chunk, mapping| {
let mut inner_source_map_line_data =
inner_source_map_line_data.borrow_mut();
while inner_source_map_line_data.len()
<= mapping.generated_line as usize
{
inner_source_map_line_data.push(SourceMapLineData {
mappings_data: Default::default(),
chunks: vec![],
});
let inner_source_map_line_data_len =
inner_source_map_line_data.len();
let mapping_generated_line_len = mapping.generated_line as usize;
if inner_source_map_line_data_len <= mapping_generated_line_len {
inner_source_map_line_data.reserve(
mapping_generated_line_len - inner_source_map_line_data_len + 1,
);
while inner_source_map_line_data.len()
<= mapping_generated_line_len
{
inner_source_map_line_data.push(SourceMapLineData {
mappings_data: Default::default(),
chunks: vec![],
});
}
}
let data = &mut inner_source_map_line_data
[mapping.generated_line as usize - 1];
data.mappings_data.reserve(5);
data.mappings_data.push(mapping.generated_column as i64);
data.mappings_data.push(
mapping
Expand Down
4 changes: 4 additions & 0 deletions src/replace_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for ReplaceSource<T> {
}

impl<T: Source> StreamChunks for ReplaceSource<T> {
fn mappings_size_hint(&self) -> usize {
self.replacements.lock().len() * 2
}

fn stream_chunks(
&self,
options: &crate::MapOptions,
Expand Down
79 changes: 47 additions & 32 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,26 +175,26 @@ impl MapOptions {
pub struct SourceMap {
file: Option<String>,
mappings: String,
sources: Vec<String>,
sources_content: Vec<String>,
names: Vec<String>,
sources: Vec<Cow<'static, str>>,
sources_content: Vec<Cow<'static, str>>,
names: Vec<Cow<'static, str>>,
}

impl SourceMap {
/// Create a [SourceMap].
pub fn new(
file: Option<String>,
mappings: String,
sources: impl IntoIterator<Item = String>,
sources_content: impl IntoIterator<Item = String>,
names: impl IntoIterator<Item = String>,
sources: Vec<Cow<'static, str>>,
sources_content: Vec<Cow<'static, str>>,
names: Vec<Cow<'static, str>>,
) -> Self {
Self {
file,
mappings,
sources: sources.into_iter().collect(),
sources_content: sources_content.into_iter().collect(),
names: names.into_iter().collect(),
sources,
sources_content,
names,
}
}

Expand All @@ -219,63 +219,72 @@ impl SourceMap {
}

/// Get the sources field in [SourceMap].
pub fn sources(&self) -> &[String] {
pub fn sources(&self) -> &[Cow<'static, str>] {
&self.sources
}

/// Get the mutable sources field in [SourceMap].
pub fn sources_mut(&mut self) -> &mut [String] {
pub fn sources_mut(&mut self) -> &mut [Cow<'static, str>] {
&mut self.sources
}

/// Get the source by index from sources field in [SourceMap].
pub fn get_source(&self, index: usize) -> Option<&str> {
self.sources.get(index).map(|s| s.as_str())
self.sources.get(index).map(|s| s.as_ref())
}

/// Get the mutable source by index from sources field in [SourceMap].
pub fn get_source_mut(&mut self, index: usize) -> Option<&mut str> {
self.sources.get_mut(index).map(|s| s.as_mut_str())
pub fn get_source_mut(
&mut self,
index: usize,
) -> Option<&mut Cow<'static, str>> {
self.sources.get_mut(index)
}

/// Get the sourcesContent field in [SourceMap].
pub fn sources_content(&self) -> &[String] {
pub fn sources_content(&self) -> &[Cow<'static, str>] {
&self.sources_content
}

/// Get the mutable sourcesContent field in [SourceMap].
pub fn sources_content_mut(&mut self) -> &mut [String] {
pub fn sources_content_mut(&mut self) -> &mut [Cow<'static, str>] {
&mut self.sources_content
}

/// Get the source content by index from sourcesContent field in [SourceMap].
pub fn get_source_content(&self, index: usize) -> Option<&str> {
self.sources_content.get(index).map(|s| s.as_str())
self.sources_content.get(index).map(|s| s.as_ref())
}

/// Get the mutable source content by index from sourcesContent field in [SourceMap].
pub fn get_source_content_mut(&mut self, index: usize) -> Option<&mut str> {
self.sources_content.get_mut(index).map(|s| s.as_mut_str())
pub fn get_source_content_mut(
&mut self,
index: usize,
) -> Option<&mut Cow<'static, str>> {
self.sources_content.get_mut(index)
}

/// Get the names field in [SourceMap].
pub fn names(&self) -> &[String] {
pub fn names(&self) -> &[Cow<'static, str>] {
&self.names
}

/// Get the names field in [SourceMap].
pub fn names_mut(&mut self) -> &mut [String] {
pub fn names_mut(&mut self) -> &mut [Cow<'static, str>] {
&mut self.names
}

/// Get the name by index from names field in [SourceMap].
pub fn get_name(&self, index: usize) -> Option<&str> {
self.names.get(index).map(|s| s.as_str())
self.names.get(index).map(|s| s.as_ref())
}

/// Get the mutable name by index from names field in [SourceMap].
pub fn get_name_mut(&mut self, index: usize) -> Option<&mut str> {
self.names.get_mut(index).map(|s| s.as_mut_str())
pub fn get_name_mut(
&mut self,
index: usize,
) -> Option<&mut Cow<'static, str>> {
self.names.get_mut(index)
}
}

Expand All @@ -284,13 +293,13 @@ struct RawSourceMap {
pub version: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
pub file: Option<String>,
pub sources: Option<Vec<Option<String>>>,
pub sources: Option<Vec<Option<Cow<'static, str>>>>,
#[serde(rename = "sourceRoot", skip_serializing_if = "Option::is_none")]
pub source_root: Option<String>,
#[serde(rename = "sourcesContent", skip_serializing_if = "Option::is_none")]
pub sources_content: Option<Vec<Option<String>>>,
pub sources_content: Option<Vec<Option<Cow<'static, str>>>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub names: Option<Vec<Option<String>>>,
pub names: Option<Vec<Option<Cow<'static, str>>>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mappings: Option<String>,
}
Expand Down Expand Up @@ -370,24 +379,30 @@ impl TryFrom<RawSourceMap> for SourceMap {
if is_valid {
x
} else {
format!("{source_root}/{x}")
format!("{source_root}/{x}").into()
}
})
.collect()
}
_ => sources.into_iter().map(Option::unwrap_or_default).collect(),
_ => sources
.into_iter()
.map(Option::unwrap_or_default)
.map(Cow::from)
.collect(),
};
let sources_content = raw
.sources_content
.unwrap_or_default()
.into_iter()
.map(|v| v.unwrap_or_default())
.map(Cow::from)
.collect();
let names = raw
.names
.unwrap_or_default()
.into_iter()
.map(|v| v.unwrap_or_default())
.map(Cow::from)
.collect();
Ok(Self {
file: raw.file,
Expand Down Expand Up @@ -506,9 +521,9 @@ mod tests {
let map = SourceMap::new(
None,
";;".to_string(),
["a.js".to_string()],
["".to_string(), "".to_string(), "".to_string()],
["".to_string(), "".to_string()],
vec!["a.js".into()],
vec!["".into(), "".into(), "".into()],
vec!["".into(), "".into()],
)
.to_json()
.unwrap();
Expand Down
Loading
Loading