Skip to content

Commit 107624e

Browse files
committed
perf
1 parent 9be6f5e commit 107624e

File tree

6 files changed

+705
-688
lines changed

6 files changed

+705
-688
lines changed

benches/bench.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rspack_sources::{
1818

1919
use bench_complex_replace_source::benchmark_complex_replace_source;
2020
use bench_source_map::{
21-
benchmark_parse_source_map_from_json, benchmark_source_map_clone,
21+
benchmark_parse_source_map_from_json, benchmark_source_map_as_borrowed,
2222
benchmark_stringify_source_map_to_json,
2323
};
2424

@@ -253,7 +253,7 @@ fn bench_rspack_sources(criterion: &mut Criterion) {
253253
benchmark_parse_source_map_from_json,
254254
);
255255

256-
group.bench_function("source_map_clone", benchmark_source_map_clone);
256+
group.bench_function("source_map_as_borrowed", benchmark_source_map_as_borrowed);
257257

258258
group.bench_function(
259259
"stringify_source_map_to_json",

benches/bench_source_map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ pub fn benchmark_parse_source_map_from_json(b: &mut Bencher) {
1919
})
2020
}
2121

22-
pub fn benchmark_source_map_clone(b: &mut Bencher) {
22+
pub fn benchmark_source_map_as_borrowed(b: &mut Bencher) {
2323
let source = SourceMap::from_json(ANTD_MIN_JS_MAP).unwrap();
2424
b.iter(|| {
25-
let _ = black_box(source.clone());
25+
let _ = black_box(source.as_borrowed());
2626
})
2727
}
2828

src/cached_source.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,17 @@ impl Source for CachedSource {
119119
fn map(&self, options: &MapOptions) -> Option<SourceMap> {
120120
if options.columns {
121121
self.0.with_dependent(|owner, dependent| {
122-
dependent.cached_colomns_map.get_or_init(|| {
123-
let map = owner.inner.map(options);
124-
unsafe { std::mem::transmute::<Option<SourceMap>, Option<SourceMap<'static>>>(map) }
125-
})
122+
dependent
123+
.cached_colomns_map
124+
.get_or_init(|| owner.inner.map(options).map(|m| m.into_owned()))
126125
.as_ref()
127126
.map(|m| m.as_borrowed())
128127
})
129128
} else {
130129
self.0.with_dependent(|owner, dependent| {
131-
dependent.cached_line_only_map.get_or_init(|| {
132-
let map = owner.inner.map(options);
133-
unsafe { std::mem::transmute::<Option<SourceMap>, Option<SourceMap<'static>>>(map) }
134-
})
130+
dependent
131+
.cached_line_only_map
132+
.get_or_init(|| owner.inner.map(options).map(|m| m.into_owned()))
135133
.as_ref()
136134
.map(|m| m.as_borrowed())
137135
})

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod raw_source;
1212
mod replace_source;
1313
mod rope;
1414
mod source;
15+
mod source_map;
1516
mod source_map_source;
1617
mod with_indices;
1718

@@ -24,8 +25,8 @@ pub use replace_source::{ReplaceSource, ReplacementEnforce};
2425
pub use rope::Rope;
2526
pub use source::{
2627
BoxSource, MapOptions, Mapping, OriginalLocation, Source, SourceExt,
27-
SourceMap,
2828
};
29+
pub use source_map::SourceMap;
2930
pub use source_map_source::{
3031
SourceMapSource, SourceMapSourceOptions, WithoutOriginalOptions,
3132
};

0 commit comments

Comments
 (0)