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: cow map #134

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
40 changes: 35 additions & 5 deletions src/cached_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,43 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for CachedSource<T> {
self.source().len()
}

fn map(&self, options: &MapOptions) -> Option<SourceMap> {
fn map(&self, options: &MapOptions) -> Option<Cow<SourceMap>> {
if let Some(map) = self.cached_maps.get(options) {
map.clone()
match map.as_ref() {
Some(map) => {
#[allow(unsafe_code)]
// SAFETY: We guarantee that once a `SourceMap` is stored in the cache, it will never be removed.
// Therefore, even if we force its lifetime to be longer, the reference remains valid.
// This is based on the following assumptions:
// 1. `SourceMap` will be valid for the entire duration of the application.
// 2. The cached `SourceMap` will not be manually removed or replaced, ensuring the reference's safety.
let map = unsafe {
std::mem::transmute::<&SourceMap, &'static SourceMap>(map)
};
Some(Cow::Borrowed(map))
}
None => None,
}
} else {
let map = self.inner.map(options);
self.cached_maps.insert(options.clone(), map.clone());
map
self
.cached_maps
.insert(options.clone(), map.map(|m| m.into_owned()));
match self.cached_maps.get(options).unwrap().as_ref() {
Some(map) => {
#[allow(unsafe_code)]
// SAFETY: We guarantee that once a `SourceMap` is stored in the cache, it will never be removed.
// Therefore, even if we force its lifetime to be longer, the reference remains valid.
// This is based on the following assumptions:
// 1. `SourceMap` will be valid for the entire duration of the application.
// 2. The cached `SourceMap` will not be manually removed or replaced, ensuring the reference's safety.
let map = unsafe {
std::mem::transmute::<&SourceMap, &'static SourceMap>(map)
};
Some(Cow::Borrowed(map))
}
None => None,
}
}
}

Expand Down Expand Up @@ -254,7 +284,7 @@ mod tests {
);
assert_eq!(
*clone.cached_maps.get(&map_options).unwrap().value(),
source.map(&map_options)
source.map(&map_options).map(|map| map.into_owned())
);
}

Expand Down
22 changes: 11 additions & 11 deletions src/concat_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::{
/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
/// );
/// assert_eq!(
/// source.map(&MapOptions::new(false)).unwrap(),
/// source.map(&MapOptions::new(false)).unwrap().into_owned(),
/// SourceMap::from_json(
/// r#"{
/// "version": 3,
Expand Down Expand Up @@ -127,8 +127,8 @@ impl Source for ConcatSource {
self.children().iter().map(|child| child.size()).sum()
}

fn map(&self, options: &MapOptions) -> Option<SourceMap> {
get_map(self, options)
fn map(&self, options: &MapOptions) -> Option<Cow<SourceMap>> {
get_map(self, options).map(Cow::Owned)
}

fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
Expand Down Expand Up @@ -347,7 +347,7 @@ mod tests {
assert_eq!(source.size(), 62);
assert_eq!(source.source(), expected_source);
assert_eq!(
source.map(&MapOptions::new(false)).unwrap(),
source.map(&MapOptions::new(false)).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"version": 3,
Expand All @@ -363,7 +363,7 @@ mod tests {
.unwrap()
);
assert_eq!(
source.map(&MapOptions::default()).unwrap(),
source.map(&MapOptions::default()).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"version": 3,
Expand Down Expand Up @@ -397,7 +397,7 @@ mod tests {
assert_eq!(source.size(), 62);
assert_eq!(source.source(), expected_source);
assert_eq!(
source.map(&MapOptions::new(false)).unwrap(),
source.map(&MapOptions::new(false)).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"version": 3,
Expand All @@ -413,7 +413,7 @@ mod tests {
.unwrap()
);
assert_eq!(
source.map(&MapOptions::default()).unwrap(),
source.map(&MapOptions::default()).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"version": 3,
Expand Down Expand Up @@ -447,7 +447,7 @@ mod tests {
assert_eq!(source.size(), 62);
assert_eq!(source.source(), expected_source);
assert_eq!(
source.map(&MapOptions::new(false)).unwrap(),
source.map(&MapOptions::new(false)).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"version": 3,
Expand All @@ -463,7 +463,7 @@ mod tests {
.unwrap()
);
assert_eq!(
source.map(&MapOptions::default()).unwrap(),
source.map(&MapOptions::default()).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"version": 3,
Expand Down Expand Up @@ -513,7 +513,7 @@ mod tests {
assert_eq!(source.buffer(), expected_source.as_bytes());

let map = source.map(&MapOptions::new(false)).unwrap();
assert_eq!(map, expected_map1);
assert_eq!(map.into_owned(), expected_map1);

// TODO: test hash
}
Expand Down Expand Up @@ -549,7 +549,7 @@ mod tests {
]);

assert_eq!(
source.map(&MapOptions::default()).unwrap(),
source.map(&MapOptions::default()).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"mappings": "AAAA,K,CCAA,M;ADAA;;ACAA",
Expand Down
4 changes: 2 additions & 2 deletions src/original_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ impl Source for OriginalSource {
self.value.len()
}

fn map(&self, options: &MapOptions) -> Option<SourceMap> {
get_map(self, options)
fn map(&self, options: &MapOptions) -> Option<Cow<SourceMap>> {
get_map(self, options).map(Cow::Owned)
}

fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
Expand Down
6 changes: 3 additions & 3 deletions src/raw_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl Source for RawSource {
}
}

fn map(&self, _: &MapOptions) -> Option<SourceMap> {
fn map(&self, _: &MapOptions) -> Option<Cow<SourceMap>> {
None
}

Expand Down Expand Up @@ -274,7 +274,7 @@ impl Source for RawStringSource {
self.0.len()
}

fn map(&self, _: &MapOptions) -> Option<SourceMap> {
fn map(&self, _: &MapOptions) -> Option<Cow<SourceMap>> {
None
}

Expand Down Expand Up @@ -373,7 +373,7 @@ impl Source for RawBufferSource {
self.value.len()
}

fn map(&self, _: &MapOptions) -> Option<SourceMap> {
fn map(&self, _: &MapOptions) -> Option<Cow<SourceMap>> {
None
}

Expand Down
4 changes: 2 additions & 2 deletions src/replace_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for ReplaceSource<T> {
self.source().len()
}

fn map(&self, options: &crate::MapOptions) -> Option<SourceMap> {
fn map(&self, options: &crate::MapOptions) -> Option<Cow<SourceMap>> {
let replacements = self.replacements.lock().unwrap();
if replacements.is_empty() {
return self.inner.map(options);
}
drop(replacements);
get_map(self, options)
get_map(self, options).map(Cow::Owned)
}

fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
Expand Down
8 changes: 4 additions & 4 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub trait Source:
fn size(&self) -> usize;

/// Get the [SourceMap].
fn map(&self, options: &MapOptions) -> Option<SourceMap>;
fn map(&self, options: &MapOptions) -> Option<Cow<SourceMap>>;

/// Update hash based on the source.
fn update_hash(&self, state: &mut dyn Hasher) {
Expand All @@ -63,7 +63,7 @@ impl Source for BoxSource {
self.as_ref().size()
}

fn map(&self, options: &MapOptions) -> Option<SourceMap> {
fn map(&self, options: &MapOptions) -> Option<Cow<SourceMap>> {
self.as_ref().map(options)
}

Expand Down Expand Up @@ -370,8 +370,8 @@ impl SourceMap {
}

/// Generate source map to a json string.
pub fn to_json(self) -> Result<String> {
let json = simd_json::serde::to_string(&self)?;
pub fn to_json(&self) -> Result<String> {
let json = simd_json::serde::to_string(self)?;
Ok(json)
}

Expand Down
53 changes: 31 additions & 22 deletions src/source_map_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ impl Source for SourceMapSource {
self.value.len()
}

fn map(&self, options: &MapOptions) -> Option<SourceMap> {
fn map(&self, options: &MapOptions) -> Option<Cow<SourceMap>> {
if self.inner_source_map.is_none() {
return Some(self.source_map.clone());
return Some(Cow::Borrowed(&self.source_map));
}
get_map(self, options)
get_map(self, options).map(Cow::Owned)
}

fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
Expand Down Expand Up @@ -215,23 +215,27 @@ mod tests {
name: "text",
source_map: source_r_map.clone(),
original_source: Some(inner_source.source().to_string()),
inner_source_map: inner_source.map(&MapOptions::default()),
inner_source_map: inner_source
.map(&MapOptions::default())
.map(|map| map.into_owned()),
remove_original_source: false,
});
let sms2 = SourceMapSource::new(SourceMapSourceOptions {
value: source_r_code,
name: "text",
source_map: source_r_map,
original_source: Some(inner_source.source().to_string()),
inner_source_map: inner_source.map(&MapOptions::default()),
inner_source_map: inner_source
.map(&MapOptions::default())
.map(|map| map.into_owned()),
remove_original_source: true,
});
let expected_content =
"Translated: Hallo Welt\nist ein test Text\nAnderer Text";
assert_eq!(sms1.source(), expected_content);
assert_eq!(sms2.source(), expected_content);
assert_eq!(
sms1.map(&MapOptions::default()).unwrap(),
sms1.map(&MapOptions::default()).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"mappings": "YAAAA,K,CAAMC;AACN,O,MAAU;ACCC,O,CAAM",
Expand All @@ -247,7 +251,7 @@ mod tests {
.unwrap(),
);
assert_eq!(
sms2.map(&MapOptions::default()).unwrap(),
sms2.map(&MapOptions::default()).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"mappings": "YAAAA,K,CAAMC;AACN,O,MAAU",
Expand Down Expand Up @@ -300,7 +304,7 @@ mod tests {
let source = ConcatSource::new(sources);
assert_eq!(source.source(), "hi world\nhi world\nhi world\n");
assert_eq!(
source.map(&MapOptions::default()).unwrap(),
source.map(&MapOptions::default()).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"mappings": "AAAA;;ACAA,CAAC,CAAI",
Expand All @@ -313,7 +317,7 @@ mod tests {
.unwrap()
);
assert_eq!(
source.map(&MapOptions::new(false)).unwrap(),
source.map(&MapOptions::new(false)).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"mappings": "AAAA;;ACAA",
Expand Down Expand Up @@ -423,11 +427,15 @@ mod tests {
}

test_cached!(source, |s: &dyn Source| s.source().to_string());
test_cached!(source, |s: &dyn Source| s.map(&MapOptions::default()));
test_cached!(source, |s: &dyn Source| s.map(&MapOptions {
columns: false,
final_source: true
}));
test_cached!(source, |s: &dyn Source| s
.map(&MapOptions::default())
.map(|m| m.into_owned()));
test_cached!(source, |s: &dyn Source| s
.map(&MapOptions {
columns: false,
final_source: true
})
.map(|m| m.into_owned()));
}

#[test]
Expand Down Expand Up @@ -458,7 +466,7 @@ mod tests {
remove_original_source: false,
});
assert_eq!(
source.map(&MapOptions::default()).unwrap(),
source.map(&MapOptions::default()).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"mappings": "AAAA",
Expand Down Expand Up @@ -502,7 +510,7 @@ mod tests {
assert_eq!(source.source(), "Message: H W!");
assert_eq!(source.size(), 13);
assert_eq!(
source.map(&MapOptions::default()).unwrap(),
source.map(&MapOptions::default()).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"mappings": "AAAAA,SCAA,ECAMC,C",
Expand Down Expand Up @@ -555,7 +563,7 @@ mod tests {
});
let map = source.map(&MapOptions::default()).unwrap();
assert_eq!(
map,
map.into_owned(),
SourceMap::from_json(
r#"{
"version": 3,
Expand All @@ -574,7 +582,7 @@ mod tests {
let source = SourceMapSource::new(WithoutOriginalOptions {
value: "console.log('a')\n",
name: "a.js",
source_map: original.map(&MapOptions::new(false)).unwrap(),
source_map: original.map(&MapOptions::new(false)).unwrap().into_owned(),
});
let source = ConcatSource::new([
RawSource::from("\n").boxed(),
Expand Down Expand Up @@ -608,7 +616,8 @@ mod tests {
)
.unwrap();
let inner_source_map =
inner_source.map(&MapOptions::default()).map(|mut map| {
inner_source.map(&MapOptions::default()).map(|map| {
let mut map = map.into_owned();
map.set_source_root(Some("/path/to/folder/".to_string()));
map
});
Expand All @@ -621,7 +630,7 @@ mod tests {
remove_original_source: false,
});
assert_eq!(
sms.map(&MapOptions::default()).unwrap(),
sms.map(&MapOptions::default()).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"mappings": "YAAAA,K,CAAMC;AACN,O,MAAU;ACCC,O,CAAM",
Expand Down Expand Up @@ -668,7 +677,7 @@ mod tests {
remove_original_source: false,
});
assert_eq!(
source.map(&MapOptions::new(false)).unwrap(),
source.map(&MapOptions::new(false)).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"mappings": "AAAA",
Expand Down Expand Up @@ -710,7 +719,7 @@ mod tests {
remove_original_source: false,
});
assert_eq!(
source.map(&MapOptions::default()).unwrap(),
source.map(&MapOptions::default()).unwrap().into_owned(),
SourceMap::from_json(
r#"{
"version": 3,
Expand Down
Loading
Loading