11#![ allow( missing_docs) ]
22
3+ use std:: collections:: HashMap ;
4+
35#[ cfg( not( codspeed) ) ]
46pub use criterion:: * ;
57
68#[ cfg( codspeed) ]
79pub use codspeed_criterion_compat:: * ;
810
911use rspack_sources:: {
10- CachedSource , ConcatSource , MapOptions , ReplaceSource , Source , SourceMap ,
11- SourceMapSource , SourceMapSourceOptions ,
12+ BoxSource , CachedSource , ConcatSource , MapOptions , ReplaceSource , Source ,
13+ SourceExt , SourceMap , SourceMapSource , SourceMapSourceOptions ,
1214} ;
1315
1416const HELLOWORLD_JS : & str = include_str ! ( concat!(
@@ -195,6 +197,61 @@ fn benchmark_replace_large_minified_source(b: &mut Bencher) {
195197 } ) ;
196198}
197199
200+ fn benchmark_concat_generate_string_with_cache_as_key ( b : & mut Bencher ) {
201+ let sms_minify = SourceMapSource :: new ( SourceMapSourceOptions {
202+ value : HELLOWORLD_MIN_JS ,
203+ name : "helloworld.min.js" ,
204+ source_map : SourceMap :: from_json ( HELLOWORLD_MIN_JS_MAP ) . unwrap ( ) ,
205+ original_source : Some ( HELLOWORLD_JS . to_string ( ) ) ,
206+ inner_source_map : Some ( SourceMap :: from_json ( HELLOWORLD_JS_MAP ) . unwrap ( ) ) ,
207+ remove_original_source : false ,
208+ } ) ;
209+ let sms_rollup = SourceMapSource :: new ( SourceMapSourceOptions {
210+ value : BUNDLE_JS ,
211+ name : "bundle.js" ,
212+ source_map : SourceMap :: from_json ( BUNDLE_JS_MAP ) . unwrap ( ) ,
213+ original_source : None ,
214+ inner_source_map : None ,
215+ remove_original_source : false ,
216+ } ) ;
217+ let concat = ConcatSource :: new ( [ sms_minify, sms_rollup] ) ;
218+ let cached = CachedSource :: new ( concat) . boxed ( ) ;
219+
220+ b. iter ( || {
221+ let mut m = HashMap :: < BoxSource , ( ) > :: new ( ) ;
222+ m. insert ( cached. clone ( ) , ( ) ) ;
223+ let _ = black_box ( || m. get ( & cached) ) ;
224+ let _ = black_box ( || m. get ( & cached) ) ;
225+ } )
226+ }
227+
228+ fn benchmark_concat_generate_string_as_key ( b : & mut Bencher ) {
229+ let sms_minify = SourceMapSource :: new ( SourceMapSourceOptions {
230+ value : HELLOWORLD_MIN_JS ,
231+ name : "helloworld.min.js" ,
232+ source_map : SourceMap :: from_json ( HELLOWORLD_MIN_JS_MAP ) . unwrap ( ) ,
233+ original_source : Some ( HELLOWORLD_JS . to_string ( ) ) ,
234+ inner_source_map : Some ( SourceMap :: from_json ( HELLOWORLD_JS_MAP ) . unwrap ( ) ) ,
235+ remove_original_source : false ,
236+ } ) ;
237+ let sms_rollup = SourceMapSource :: new ( SourceMapSourceOptions {
238+ value : BUNDLE_JS ,
239+ name : "bundle.js" ,
240+ source_map : SourceMap :: from_json ( BUNDLE_JS_MAP ) . unwrap ( ) ,
241+ original_source : None ,
242+ inner_source_map : None ,
243+ remove_original_source : false ,
244+ } ) ;
245+ let concat = ConcatSource :: new ( [ sms_minify, sms_rollup] ) . boxed ( ) ;
246+
247+ b. iter ( || {
248+ let mut m = HashMap :: < BoxSource , ( ) > :: new ( ) ;
249+ m. insert ( concat. clone ( ) , ( ) ) ;
250+ let _ = black_box ( || m. get ( & concat) ) ;
251+ let _ = black_box ( || m. get ( & concat) ) ;
252+ } )
253+ }
254+
198255fn bench_rspack_sources ( criterion : & mut Criterion ) {
199256 let mut group = criterion. benchmark_group ( "rspack_sources" ) ;
200257 group. bench_function (
@@ -213,6 +270,14 @@ fn bench_rspack_sources(criterion: &mut Criterion) {
213270 "replace_large_minified_source" ,
214271 benchmark_replace_large_minified_source,
215272 ) ;
273+ group. bench_function (
274+ "concat_generate_string_with_cache_as_key" ,
275+ benchmark_concat_generate_string_with_cache_as_key,
276+ ) ;
277+ group. bench_function (
278+ "concat_generate_string_as_key" ,
279+ benchmark_concat_generate_string_as_key,
280+ ) ;
216281 group. finish ( ) ;
217282}
218283
0 commit comments