Skip to content
Merged
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
42 changes: 40 additions & 2 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ pub use criterion::*;
pub use codspeed_criterion_compat::*;

use rspack_sources::{
BoxSource, CachedSource, ConcatSource, MapOptions, ObjectPool, Source,
SourceExt, SourceMap, SourceMapSource, SourceMapSourceOptions,
BoxSource, CachedSource, ConcatSource, MapOptions, ObjectPool,
RawStringSource, Source, SourceExt, SourceMap, SourceMapSource,
SourceMapSourceOptions,
};

use bench_complex_replace_source::{
Expand Down Expand Up @@ -142,6 +143,38 @@ fn benchmark_cached_source_hash(b: &mut Bencher) {
})
}

fn benchmark_concat_source_add_many(b: &mut Bencher) {
// Mimic rspack's concatenated_module / runtime hot path: build a ConcatSource
// by adding many small children sequentially. 500 matches the scale of a
// typical concatenated module (rspack chains 300+ adds per module).
let pieces: Vec<BoxSource> = (0..500)
.map(|i| RawStringSource::from(format!("// piece {i}\n")).boxed())
.collect();

b.iter(|| {
let mut concat = ConcatSource::default();
for piece in &pieces {
concat.add(piece.clone());
}
Comment thread
JSerFeng marked this conversation as resolved.
std::hint::black_box(concat);
})
}

fn benchmark_concat_source_add_few(b: &mut Bencher) {
// Smaller scale: closer to runtime module assembly (~10-15 adds per chunk).
let pieces: Vec<BoxSource> = (0..16)
.map(|i| RawStringSource::from(format!("// piece {i}\n")).boxed())
.collect();

b.iter(|| {
let mut concat = ConcatSource::default();
for piece in &pieces {
concat.add(piece.clone());
}
Comment thread
JSerFeng marked this conversation as resolved.
std::hint::black_box(concat);
})
}

fn bench_rspack_sources(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("rspack_sources");

Expand All @@ -154,6 +187,11 @@ fn bench_rspack_sources(criterion: &mut Criterion) {

group.bench_function("cached_source_hash", benchmark_cached_source_hash);

group
.bench_function("concat_source_add_many", benchmark_concat_source_add_many);
group
.bench_function("concat_source_add_few", benchmark_concat_source_add_few);

group.bench_function(
"complex_replace_source_map",
benchmark_complex_replace_source_map,
Expand Down
Loading