Skip to content

Commit c0faba8

Browse files
committed
perf: cached source
1 parent 500104a commit c0faba8

File tree

4 files changed

+209
-81
lines changed

4 files changed

+209
-81
lines changed

src/cached_source.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ self_cell::self_cell!(
7474
impl { Debug }
7575
);
7676

77+
/// A wrapper around any [`Source`] that caches expensive computations to improve performance.
7778
pub struct CachedSource(CachedSourceCell);
7879

7980
impl CachedSource {
@@ -121,14 +122,18 @@ impl Source for CachedSource {
121122
dependent.cached_colomns_map.get_or_init(|| {
122123
let map = owner.inner.map(options);
123124
unsafe { std::mem::transmute::<Option<SourceMap>, Option<SourceMap<'static>>>(map) }
124-
}).clone()
125+
})
126+
.as_ref()
127+
.map(|m| m.as_borrowed())
125128
})
126129
} else {
127130
self.0.with_dependent(|owner, dependent| {
128131
dependent.cached_line_only_map.get_or_init(|| {
129132
let map = owner.inner.map(options);
130133
unsafe { std::mem::transmute::<Option<SourceMap>, Option<SourceMap<'static>>>(map) }
131-
}).clone()
134+
})
135+
.as_ref()
136+
.map(|m| m.as_borrowed())
132137
})
133138
}
134139
}
@@ -255,7 +260,7 @@ mod tests {
255260
value: "\nconsole.log(1);\n".to_string(),
256261
name: "index.js".to_string(),
257262
source_map: SourceMap::new(
258-
";AACA".into(),
263+
";AACA",
259264
vec!["index.js".into()],
260265
vec!["// DELETE IT\nconsole.log(1)".into()],
261266
vec![],

src/helpers.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ pub fn get_map<'a, S: StreamChunks>(
6363
},
6464
);
6565
let mappings = mappings_encoder.drain();
66-
(!mappings.is_empty()).then(|| {
67-
SourceMap::new(Cow::Owned(mappings), sources, sources_content, names)
68-
})
66+
(!mappings.is_empty())
67+
.then(|| SourceMap::new(mappings, sources, sources_content, names))
6968
}
7069

7170
/// [StreamChunks] abstraction, see [webpack-sources source.streamChunks](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
@@ -1241,7 +1240,7 @@ pub fn stream_and_get_source_and_map<'a, S: StreamChunks>(
12411240
None
12421241
} else {
12431242
Some(SourceMap::new(
1244-
Cow::Owned(mappings),
1243+
mappings.to_string(),
12451244
sources,
12461245
sources_content,
12471246
names,

0 commit comments

Comments
 (0)