Skip to content

Commit 7e2869d

Browse files
committed
pass object pool to map
1 parent 6ddd2b1 commit 7e2869d

12 files changed

+247
-110
lines changed

benches/bench.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ pub use criterion::*;
1313
pub use codspeed_criterion_compat::*;
1414

1515
use rspack_sources::{
16-
BoxSource, CachedSource, ConcatSource, MapOptions, Source, SourceExt,
17-
SourceMap, SourceMapSource, SourceMapSourceOptions,
16+
BoxSource, CachedSource, ConcatSource, MapOptions, ObjectPool, Source,
17+
SourceExt, SourceMap, SourceMapSource, SourceMapSourceOptions,
1818
};
1919

2020
use bench_complex_replace_source::{
@@ -79,7 +79,7 @@ fn benchmark_concat_generate_string(b: &mut Bencher) {
7979

8080
b.iter(|| {
8181
concat
82-
.map(&MapOptions::default())
82+
.map(&ObjectPool::default(), &MapOptions::default())
8383
.unwrap()
8484
.to_json()
8585
.unwrap();
@@ -108,7 +108,7 @@ fn benchmark_concat_generate_string_with_cache(b: &mut Bencher) {
108108

109109
b.iter(|| {
110110
cached
111-
.map(&MapOptions::default())
111+
.map(&ObjectPool::default(), &MapOptions::default())
112112
.unwrap()
113113
.to_json()
114114
.unwrap();

benches/bench_complex_replace_source.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ pub use criterion::*;
99
pub use codspeed_criterion_compat::*;
1010

1111
use rspack_sources::{
12-
BoxSource, MapOptions, OriginalSource, ReplaceSource, SourceExt,
12+
BoxSource, MapOptions, ObjectPool, OriginalSource, ReplaceSource, SourceExt,
1313
};
14+
use simd_json::borrowed::Object;
1415

1516
static LARGE_REPLACE_SOURCE: LazyLock<BoxSource> = LazyLock::new(|| {
1617
let mut source = ReplaceSource::new(
@@ -36724,7 +36725,7 @@ pub fn benchmark_complex_replace_source_map(b: &mut Bencher) {
3672436725
let source = LARGE_REPLACE_SOURCE.clone();
3672536726

3672636727
b.iter(|| {
36727-
black_box(source.map(&MapOptions::default()));
36728+
black_box(source.map(&ObjectPool::default(), &MapOptions::default()));
3672836729
});
3672936730
}
3673036731

benches/benchmark_repetitive_react_components.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ pub use criterion::*;
99
pub use codspeed_criterion_compat::*;
1010

1111
use rspack_sources::{
12-
BoxSource, ConcatSource, MapOptions, OriginalSource, RawStringSource,
13-
ReplaceSource, ReplacementEnforce, Source, SourceExt, SourceMap,
14-
SourceMapSource, SourceMapSourceOptions,
12+
BoxSource, ConcatSource, MapOptions, ObjectPool, OriginalSource,
13+
RawStringSource, ReplaceSource, ReplacementEnforce, Source, SourceExt,
14+
SourceMap, SourceMapSource, SourceMapSourceOptions,
1515
};
1616

1717
static REPETITIVE_1K_REACT_COMPONENTS_SOURCE: LazyLock<BoxSource> =
@@ -3505,7 +3505,7 @@ pub fn benchmark_repetitive_react_components_map(b: &mut Bencher) {
35053505
let source = REPETITIVE_1K_REACT_COMPONENTS_SOURCE.clone();
35063506

35073507
b.iter(|| {
3508-
black_box(source.map(&MapOptions::default()));
3508+
black_box(source.map(&ObjectPool::default(), &MapOptions::default()));
35093509
});
35103510
}
35113511

src/cached_source.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,22 @@ impl Source for CachedSource {
100100
*self.cache.size.get_or_init(|| self.inner.size())
101101
}
102102

103-
fn map(&self, options: &MapOptions) -> Option<SourceMap> {
103+
fn map(
104+
&self,
105+
object_pool: &ObjectPool,
106+
options: &MapOptions,
107+
) -> Option<SourceMap> {
104108
if options.columns {
105109
self
106110
.cache
107111
.columns_map
108-
.get_or_init(|| self.inner.map(options))
112+
.get_or_init(|| self.inner.map(object_pool, options))
109113
.clone()
110114
} else {
111115
self
112116
.cache
113117
.line_only_map
114-
.get_or_init(|| self.inner.map(options))
118+
.get_or_init(|| self.inner.map(object_pool, options))
115119
.clone()
116120
}
117121
}
@@ -124,8 +128,8 @@ impl Source for CachedSource {
124128
impl StreamChunks for CachedSource {
125129
fn stream_chunks<'a>(
126130
&'a self,
127-
options: &MapOptions,
128131
object_pool: &'a ObjectPool,
132+
options: &MapOptions,
129133
on_chunk: crate::helpers::OnChunk<'_, 'a>,
130134
on_source: crate::helpers::OnSource<'_, 'a>,
131135
on_name: crate::helpers::OnName<'_, 'a>,
@@ -242,7 +246,9 @@ mod tests {
242246
})
243247
.boxed(),
244248
]);
245-
let map = source.map(&Default::default()).unwrap();
249+
let map = source
250+
.map(&ObjectPool::default(), &Default::default())
251+
.unwrap();
246252
assert_eq!(map.mappings(), ";;AACA");
247253
}
248254

@@ -257,11 +263,11 @@ mod tests {
257263
source.source();
258264
source.buffer();
259265
source.size();
260-
source.map(&map_options);
266+
source.map(&ObjectPool::default(), &map_options);
261267

262268
assert_eq!(
263269
*clone.cache.columns_map.get().unwrap(),
264-
source.map(&map_options)
270+
source.map(&ObjectPool::default(), &map_options)
265271
);
266272
}
267273

@@ -318,8 +324,8 @@ mod tests {
318324
let mut on_source_count = 0;
319325
let mut on_name_count = 0;
320326
let generated_info = source.stream_chunks(
321-
&map_options,
322327
&ObjectPool::default(),
328+
&map_options,
323329
&mut |_chunk, _mapping| {
324330
on_chunk_count += 1;
325331
},
@@ -333,8 +339,8 @@ mod tests {
333339

334340
let cached_source = CachedSource::new(source);
335341
cached_source.stream_chunks(
336-
&map_options,
337342
&ObjectPool::default(),
343+
&map_options,
338344
&mut |_chunk, _mapping| {},
339345
&mut |_source_index, _source, _source_content| {},
340346
&mut |_name_index, _name| {},
@@ -344,8 +350,8 @@ mod tests {
344350
let mut cached_on_source_count = 0;
345351
let mut cached_on_name_count = 0;
346352
let cached_generated_info = cached_source.stream_chunks(
347-
&map_options,
348353
&ObjectPool::default(),
354+
&map_options,
349355
&mut |_chunk, _mapping| {
350356
cached_on_chunk_count += 1;
351357
},

src/concat_source.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,12 @@ impl Source for ConcatSource {
200200
.sum()
201201
}
202202

203-
fn map(&self, options: &MapOptions) -> Option<SourceMap> {
204-
get_map(self, options)
203+
fn map(
204+
&self,
205+
object_pool: &ObjectPool,
206+
options: &MapOptions,
207+
) -> Option<SourceMap> {
208+
get_map(object_pool, self, options)
205209
}
206210

207211
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
@@ -231,8 +235,8 @@ impl Eq for ConcatSource {}
231235
impl StreamChunks for ConcatSource {
232236
fn stream_chunks<'a>(
233237
&'a self,
234-
options: &MapOptions,
235238
object_pool: &'a ObjectPool,
239+
options: &MapOptions,
236240
on_chunk: OnChunk<'_, 'a>,
237241
on_source: OnSource<'_, 'a>,
238242
on_name: OnName<'_, 'a>,
@@ -241,8 +245,8 @@ impl StreamChunks for ConcatSource {
241245

242246
if children.len() == 1 {
243247
return children[0].stream_chunks(
244-
options,
245248
object_pool,
249+
options,
246250
on_chunk,
247251
on_source,
248252
on_name,
@@ -267,8 +271,8 @@ impl StreamChunks for ConcatSource {
267271
generated_line,
268272
generated_column,
269273
} = item.stream_chunks(
270-
options,
271274
object_pool,
275+
options,
272276
&mut |chunk, mapping| {
273277
let line = mapping.generated_line + current_line_offset;
274278
let column = if mapping.generated_line == 1 {
@@ -479,7 +483,9 @@ mod tests {
479483
assert_eq!(source.size(), 62);
480484
assert_eq!(source.source().into_string_lossy(), expected_source);
481485
assert_eq!(
482-
source.map(&MapOptions::new(false)).unwrap(),
486+
source
487+
.map(&ObjectPool::default(), &MapOptions::new(false))
488+
.unwrap(),
483489
SourceMap::from_json(
484490
r#"{
485491
"version": 3,
@@ -495,7 +501,9 @@ mod tests {
495501
.unwrap()
496502
);
497503
assert_eq!(
498-
source.map(&MapOptions::default()).unwrap(),
504+
source
505+
.map(&ObjectPool::default(), &MapOptions::default())
506+
.unwrap(),
499507
SourceMap::from_json(
500508
r#"{
501509
"version": 3,
@@ -529,7 +537,9 @@ mod tests {
529537
assert_eq!(source.size(), 62);
530538
assert_eq!(source.source().into_string_lossy(), expected_source);
531539
assert_eq!(
532-
source.map(&MapOptions::new(false)).unwrap(),
540+
source
541+
.map(&ObjectPool::default(), &MapOptions::new(false))
542+
.unwrap(),
533543
SourceMap::from_json(
534544
r#"{
535545
"version": 3,
@@ -545,7 +555,9 @@ mod tests {
545555
.unwrap()
546556
);
547557
assert_eq!(
548-
source.map(&MapOptions::default()).unwrap(),
558+
source
559+
.map(&ObjectPool::default(), &MapOptions::default())
560+
.unwrap(),
549561
SourceMap::from_json(
550562
r#"{
551563
"version": 3,
@@ -579,7 +591,9 @@ mod tests {
579591
assert_eq!(source.size(), 62);
580592
assert_eq!(source.source().into_string_lossy(), expected_source);
581593
assert_eq!(
582-
source.map(&MapOptions::new(false)).unwrap(),
594+
source
595+
.map(&ObjectPool::default(), &MapOptions::new(false))
596+
.unwrap(),
583597
SourceMap::from_json(
584598
r#"{
585599
"version": 3,
@@ -595,7 +609,9 @@ mod tests {
595609
.unwrap()
596610
);
597611
assert_eq!(
598-
source.map(&MapOptions::default()).unwrap(),
612+
source
613+
.map(&ObjectPool::default(), &MapOptions::default())
614+
.unwrap(),
599615
SourceMap::from_json(
600616
r#"{
601617
"version": 3,
@@ -647,7 +663,9 @@ mod tests {
647663
assert_eq!(source.source().into_string_lossy(), expected_source);
648664
assert_eq!(source.buffer(), expected_source.as_bytes());
649665

650-
let map = source.map(&MapOptions::new(false)).unwrap();
666+
let map = source
667+
.map(&ObjectPool::default(), &MapOptions::new(false))
668+
.unwrap();
651669
assert_eq!(map, expected_map1);
652670

653671
// TODO: test hash
@@ -662,8 +680,9 @@ mod tests {
662680
]);
663681

664682
let result_text = source.source();
665-
let result_map = source.map(&MapOptions::default());
666-
let result_list_map = source.map(&MapOptions::new(false));
683+
let result_map = source.map(&ObjectPool::default(), &MapOptions::default());
684+
let result_list_map =
685+
source.map(&ObjectPool::default(), &MapOptions::new(false));
667686

668687
assert_eq!(
669688
result_text.into_string_lossy(),
@@ -687,7 +706,9 @@ mod tests {
687706
]);
688707

689708
assert_eq!(
690-
source.map(&MapOptions::default()).unwrap(),
709+
source
710+
.map(&ObjectPool::default(), &MapOptions::default())
711+
.unwrap(),
691712
SourceMap::from_json(
692713
r#"{
693714
"mappings": "AAAA,K,CCAA,M;ADAA;;ACAA",
@@ -713,7 +734,9 @@ mod tests {
713734
RawStringSource::from("c"),
714735
]);
715736
assert_eq!(source.source().into_string_lossy(), "abc");
716-
assert!(source.map(&MapOptions::default()).is_none());
737+
assert!(source
738+
.map(&ObjectPool::default(), &MapOptions::default())
739+
.is_none());
717740
}
718741

719742
#[test]

src/helpers.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,21 @@ use crate::{
2020
};
2121

2222
pub fn get_map<'a, S: StreamChunks>(
23+
object_pool: &'a ObjectPool,
2324
stream: &'a S,
24-
options: &'a MapOptions,
25+
options: &MapOptions,
2526
) -> Option<SourceMap> {
2627
let mut mappings_encoder = create_encoder(options.columns);
2728
let mut sources: Vec<String> = Vec::new();
2829
let mut sources_content: Vec<Arc<str>> = Vec::new();
2930
let mut names: Vec<String> = Vec::new();
3031

3132
stream.stream_chunks(
33+
object_pool,
3234
&MapOptions {
3335
columns: options.columns,
3436
final_source: true,
3537
},
36-
&ObjectPool::default(),
3738
// on_chunk
3839
&mut |_, mapping| {
3940
mappings_encoder.encode(&mapping);
@@ -71,8 +72,8 @@ pub trait StreamChunks {
7172
/// [StreamChunks] abstraction
7273
fn stream_chunks<'a>(
7374
&'a self,
74-
options: &MapOptions,
7575
object_pool: &'a ObjectPool,
76+
options: &MapOptions,
7677
on_chunk: OnChunk<'_, 'a>,
7778
on_source: OnSource<'_, 'a>,
7879
on_name: OnName<'_, 'a>,
@@ -1232,8 +1233,8 @@ pub fn stream_and_get_source_and_map<'a, S: StreamChunks>(
12321233
let mut names: Vec<String> = Vec::new();
12331234

12341235
let generated_info = input_source.stream_chunks(
1235-
options,
12361236
object_pool,
1237+
options,
12371238
&mut |chunk, mapping| {
12381239
mappings_encoder.encode(&mapping);
12391240
on_chunk(chunk, mapping);

0 commit comments

Comments
 (0)