Skip to content

Commit d86a37a

Browse files
authored
chore: bench source map clone/parse/stringify (#176)
1 parent e1bd85d commit d86a37a

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

benches/bench.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(missing_docs)]
22

33
mod bench_complex_replace_source;
4+
mod bench_source_map;
45

56
use std::collections::HashMap;
67

@@ -16,6 +17,10 @@ use rspack_sources::{
1617
};
1718

1819
use bench_complex_replace_source::benchmark_complex_replace_source;
20+
use bench_source_map::{
21+
benchmark_parse_source_map_from_json, benchmark_source_map_clone,
22+
benchmark_stringify_source_map_to_json,
23+
};
1924

2025
const HELLOWORLD_JS: &str = include_str!(concat!(
2126
env!("CARGO_MANIFEST_DIR"),
@@ -41,14 +46,6 @@ const BUNDLE_JS_MAP: &str = include_str!(concat!(
4146
env!("CARGO_MANIFEST_DIR"),
4247
"/benches/fixtures/transpile-rollup/files/bundle.js.map"
4348
));
44-
const ANTD_MIN_JS: &str = include_str!(concat!(
45-
env!("CARGO_MANIFEST_DIR"),
46-
"/benches/fixtures/antd-mini/antd.min.js"
47-
));
48-
const ANTD_MIN_JS_MAP: &str = include_str!(concat!(
49-
env!("CARGO_MANIFEST_DIR"),
50-
"/benches/fixtures/antd-mini/antd.min.js.map"
51-
));
5249

5350
fn benchmark_concat_generate_string(b: &mut Bencher) {
5451
let sms_minify = SourceMapSource::new(SourceMapSourceOptions {
@@ -251,6 +248,18 @@ fn bench_rspack_sources(criterion: &mut Criterion) {
251248
group
252249
.bench_function("complex_replace_source", benchmark_complex_replace_source);
253250

251+
group.bench_function(
252+
"parse_source_map_from_json",
253+
benchmark_parse_source_map_from_json,
254+
);
255+
256+
group.bench_function("source_map_clone", benchmark_source_map_clone);
257+
258+
group.bench_function(
259+
"stringify_source_map_to_json",
260+
benchmark_stringify_source_map_to_json,
261+
);
262+
254263
group.finish();
255264
}
256265

benches/bench_source_map.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![allow(missing_docs)]
2+
3+
#[cfg(not(codspeed))]
4+
pub use criterion::*;
5+
6+
#[cfg(codspeed)]
7+
pub use codspeed_criterion_compat::*;
8+
9+
use rspack_sources::SourceMap;
10+
11+
const ANTD_MIN_JS_MAP: &str = include_str!(concat!(
12+
env!("CARGO_MANIFEST_DIR"),
13+
"/benches/fixtures/antd-mini/antd.min.js.map"
14+
));
15+
16+
pub fn benchmark_parse_source_map_from_json(b: &mut Bencher) {
17+
b.iter(|| {
18+
black_box(SourceMap::from_json(black_box(ANTD_MIN_JS_MAP)).unwrap())
19+
})
20+
}
21+
22+
pub fn benchmark_source_map_clone(b: &mut Bencher) {
23+
let source = SourceMap::from_json(ANTD_MIN_JS_MAP).unwrap();
24+
b.iter(|| {
25+
let _ = black_box(source.clone());
26+
})
27+
}
28+
29+
pub fn benchmark_stringify_source_map_to_json(b: &mut Bencher) {
30+
let source = SourceMap::from_json(ANTD_MIN_JS_MAP).unwrap();
31+
b.iter(|| {
32+
let _ = black_box(source.to_json().unwrap());
33+
})
34+
}

src/source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ impl SourceMap {
419419
}
420420

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

0 commit comments

Comments
 (0)