From 678e9a708bc08141ec90966c53b83a662b6e0892 Mon Sep 17 00:00:00 2001 From: Fy <1114550440@qq.com> Date: Thu, 23 Apr 2026 19:37:52 +0800 Subject: [PATCH] test(bench): add concat_source_add microbenchmark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds two new criterion benches that build a ConcatSource by repeatedly calling `add()` — one with 500 children (mirroring rspack's concatenated_module rendering), one with 16 (mirroring runtime module assembly). The existing bench suite only exercises `.map().to_json()` on a pre-built ConcatSource, which leaves `add()`'s hot path unmeasured. These cover that gap so future regressions in the construction path get caught. --- benches/bench.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/benches/bench.rs b/benches/bench.rs index 4e053e6e..e2ab5c3a 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -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::{ @@ -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 = (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()); + } + 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 = (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()); + } + std::hint::black_box(concat); + }) +} + fn bench_rspack_sources(criterion: &mut Criterion) { let mut group = criterion.benchmark_group("rspack_sources"); @@ -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,