Skip to content

Commit 6ddd2b1

Browse files
committed
refactor
1 parent c95cb83 commit 6ddd2b1

15 files changed

+181
-228
lines changed

benches/bench.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use bench_source_map::{
2828

2929
use benchmark_repetitive_react_components::{
3030
benchmark_repetitive_react_components_map,
31-
benchmark_repetitive_react_components_map_with_object_pool_scope,
3231
benchmark_repetitive_react_components_source,
3332
};
3433

@@ -188,11 +187,6 @@ fn bench_rspack_sources(criterion: &mut Criterion) {
188187
benchmark_repetitive_react_components_map,
189188
);
190189

191-
group.bench_function(
192-
"repetitive_react_components_map_with_object_pool_scope",
193-
benchmark_repetitive_react_components_map_with_object_pool_scope,
194-
);
195-
196190
group.bench_function(
197191
"repetitive_react_components_source",
198192
benchmark_repetitive_react_components_source,

benches/benchmark_repetitive_react_components.rs

Lines changed: 3 additions & 18 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-
with_object_pool_scope, BoxSource, ConcatSource, MapOptions, OriginalSource,
13-
RawStringSource, ReplaceSource, ReplacementEnforce, Source, SourceExt,
14-
SourceMap, SourceMapSource, SourceMapSourceOptions,
12+
BoxSource, ConcatSource, MapOptions, OriginalSource, RawStringSource,
13+
ReplaceSource, ReplacementEnforce, Source, SourceExt, SourceMap,
14+
SourceMapSource, SourceMapSourceOptions,
1515
};
1616

1717
static REPETITIVE_1K_REACT_COMPONENTS_SOURCE: LazyLock<BoxSource> =
@@ -3516,18 +3516,3 @@ pub fn benchmark_repetitive_react_components_source(b: &mut Bencher) {
35163516
black_box(source.source());
35173517
});
35183518
}
3519-
3520-
pub fn benchmark_repetitive_react_components_map_with_object_pool_scope(
3521-
b: &mut Bencher,
3522-
) {
3523-
let source = REPETITIVE_1K_REACT_COMPONENTS_SOURCE.clone();
3524-
3525-
with_object_pool_scope(|| {
3526-
// Warm up the object pool
3527-
black_box(source.map(&MapOptions::default()));
3528-
3529-
b.iter(|| {
3530-
black_box(source.map(&MapOptions::default()));
3531-
});
3532-
})
3533-
}

src/cached_source.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::{
1111
stream_and_get_source_and_map, stream_chunks_of_raw_source,
1212
stream_chunks_of_source_map, StreamChunks,
1313
},
14+
object_pool::ObjectPool,
1415
rope::Rope,
1516
source::SourceValue,
1617
BoxSource, MapOptions, Source, SourceExt, SourceMap,
@@ -124,6 +125,7 @@ impl StreamChunks for CachedSource {
124125
fn stream_chunks<'a>(
125126
&'a self,
126127
options: &MapOptions,
128+
object_pool: &'a ObjectPool,
127129
on_chunk: crate::helpers::OnChunk<'_, 'a>,
128130
on_source: crate::helpers::OnSource<'_, 'a>,
129131
on_name: crate::helpers::OnName<'_, 'a>,
@@ -138,7 +140,13 @@ impl StreamChunks for CachedSource {
138140
let source = self.rope();
139141
if let Some(map) = map {
140142
stream_chunks_of_source_map(
141-
source, map, on_chunk, on_source, on_name, options,
143+
options,
144+
object_pool,
145+
source,
146+
map,
147+
on_chunk,
148+
on_source,
149+
on_name,
142150
)
143151
} else {
144152
stream_chunks_of_raw_source(
@@ -148,8 +156,9 @@ impl StreamChunks for CachedSource {
148156
}
149157
None => {
150158
let (generated_info, map) = stream_and_get_source_and_map(
151-
&self.inner,
152159
options,
160+
object_pool,
161+
&self.inner,
153162
on_chunk,
154163
on_source,
155164
on_name,
@@ -310,6 +319,7 @@ mod tests {
310319
let mut on_name_count = 0;
311320
let generated_info = source.stream_chunks(
312321
&map_options,
322+
&ObjectPool::default(),
313323
&mut |_chunk, _mapping| {
314324
on_chunk_count += 1;
315325
},
@@ -324,6 +334,7 @@ mod tests {
324334
let cached_source = CachedSource::new(source);
325335
cached_source.stream_chunks(
326336
&map_options,
337+
&ObjectPool::default(),
327338
&mut |_chunk, _mapping| {},
328339
&mut |_source_index, _source, _source_content| {},
329340
&mut |_name_index, _name| {},
@@ -334,6 +345,7 @@ mod tests {
334345
let mut cached_on_name_count = 0;
335346
let cached_generated_info = cached_source.stream_chunks(
336347
&map_options,
348+
&ObjectPool::default(),
337349
&mut |_chunk, _mapping| {
338350
cached_on_chunk_count += 1;
339351
},

src/concat_source.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_hash::FxHashMap as HashMap;
1010
use crate::{
1111
helpers::{get_map, GeneratedInfo, OnChunk, OnName, OnSource, StreamChunks},
1212
linear_map::LinearMap,
13+
object_pool::ObjectPool,
1314
source::{Mapping, OriginalLocation},
1415
BoxSource, MapOptions, RawStringSource, Rope, Source, SourceExt, SourceMap,
1516
SourceValue,
@@ -231,14 +232,21 @@ impl StreamChunks for ConcatSource {
231232
fn stream_chunks<'a>(
232233
&'a self,
233234
options: &MapOptions,
235+
object_pool: &'a ObjectPool,
234236
on_chunk: OnChunk<'_, 'a>,
235237
on_source: OnSource<'_, 'a>,
236238
on_name: OnName<'_, 'a>,
237239
) -> crate::helpers::GeneratedInfo {
238240
let children = self.optimized_children();
239241

240242
if children.len() == 1 {
241-
return children[0].stream_chunks(options, on_chunk, on_source, on_name);
243+
return children[0].stream_chunks(
244+
options,
245+
object_pool,
246+
on_chunk,
247+
on_source,
248+
on_name,
249+
);
242250
}
243251
let mut current_line_offset = 0;
244252
let mut current_column_offset = 0;
@@ -260,6 +268,7 @@ impl StreamChunks for ConcatSource {
260268
generated_column,
261269
} = item.stream_chunks(
262270
options,
271+
object_pool,
263272
&mut |chunk, mapping| {
264273
let line = mapping.generated_line + current_line_offset;
265274
let column = if mapping.generated_line == 1 {

src/helpers.rs

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
decoder::MappingsDecoder,
1313
encoder::create_encoder,
1414
linear_map::LinearMap,
15-
object_pool::with_current_thread_object_pool_scope,
15+
object_pool::ObjectPool,
1616
source::{Mapping, OriginalLocation},
1717
source_content_lines::SourceContentLines,
1818
with_indices::WithIndices,
@@ -22,13 +22,6 @@ use crate::{
2222
pub fn get_map<'a, S: StreamChunks>(
2323
stream: &'a S,
2424
options: &'a MapOptions,
25-
) -> Option<SourceMap> {
26-
with_current_thread_object_pool_scope(|| get_map_impl(stream, options))
27-
}
28-
29-
pub fn get_map_impl<'a, S: StreamChunks>(
30-
stream: &'a S,
31-
options: &'a MapOptions,
3225
) -> Option<SourceMap> {
3326
let mut mappings_encoder = create_encoder(options.columns);
3427
let mut sources: Vec<String> = Vec::new();
@@ -40,6 +33,7 @@ pub fn get_map_impl<'a, S: StreamChunks>(
4033
columns: options.columns,
4134
final_source: true,
4235
},
36+
&ObjectPool::default(),
4337
// on_chunk
4438
&mut |_, mapping| {
4539
mappings_encoder.encode(&mapping);
@@ -78,6 +72,7 @@ pub trait StreamChunks {
7872
fn stream_chunks<'a>(
7973
&'a self,
8074
options: &MapOptions,
75+
object_pool: &'a ObjectPool,
8176
on_chunk: OnChunk<'_, 'a>,
8277
on_source: OnSource<'_, 'a>,
8378
on_name: OnName<'_, 'a>,
@@ -97,9 +92,10 @@ pub type OnName<'a, 'b> = &'a mut dyn FnMut(u32, Cow<'b, str>);
9792

9893
/// Default stream chunks behavior impl, see [webpack-sources streamChunks](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L15-L35).
9994
pub fn stream_chunks_default<'a, S>(
95+
options: &MapOptions,
96+
object_pool: &'a ObjectPool,
10097
source: S,
10198
source_map: Option<&'a SourceMap>,
102-
options: &MapOptions,
10399
on_chunk: OnChunk<'_, 'a>,
104100
on_source: OnSource<'_, 'a>,
105101
on_name: OnName<'_, 'a>,
@@ -109,7 +105,13 @@ where
109105
{
110106
if let Some(map) = source_map {
111107
stream_chunks_of_source_map(
112-
source, map, on_chunk, on_source, on_name, options,
108+
options,
109+
object_pool,
110+
source,
111+
map,
112+
on_chunk,
113+
on_source,
114+
on_name,
113115
)
114116
} else {
115117
stream_chunks_of_raw_source(source, options, on_chunk, on_source, on_name)
@@ -308,12 +310,13 @@ where
308310
}
309311

310312
pub fn stream_chunks_of_source_map<'a, S>(
313+
options: &MapOptions,
314+
object_pool: &'a ObjectPool,
311315
source: S,
312316
source_map: &'a SourceMap,
313317
on_chunk: OnChunk<'_, 'a>,
314318
on_source: OnSource<'_, 'a>,
315319
on_name: OnName<'_, 'a>,
316-
options: &MapOptions,
317320
) -> GeneratedInfo
318321
where
319322
S: SourceText<'a> + 'a,
@@ -322,24 +325,33 @@ where
322325
MapOptions {
323326
columns: true,
324327
final_source: true,
328+
..
325329
} => stream_chunks_of_source_map_final(
326330
source, source_map, on_chunk, on_source, on_name,
327331
),
328332
MapOptions {
329333
columns: true,
330334
final_source: false,
335+
..
331336
} => stream_chunks_of_source_map_full(
332-
source, source_map, on_chunk, on_source, on_name,
337+
object_pool,
338+
source,
339+
source_map,
340+
on_chunk,
341+
on_source,
342+
on_name,
333343
),
334344
MapOptions {
335345
columns: false,
336346
final_source: true,
347+
..
337348
} => stream_chunks_of_source_map_lines_final(
338349
source, source_map, on_chunk, on_source, on_name,
339350
),
340351
MapOptions {
341352
columns: false,
342353
final_source: false,
354+
..
343355
} => stream_chunks_of_source_map_lines_full(
344356
source, source_map, on_chunk, on_source, on_name,
345357
),
@@ -418,6 +430,7 @@ where
418430
}
419431

420432
fn stream_chunks_of_source_map_full<'a, S>(
433+
object_pool: &'a ObjectPool,
421434
source: S,
422435
source_map: &'a SourceMap,
423436
on_chunk: OnChunk<'_, 'a>,
@@ -428,7 +441,9 @@ where
428441
S: SourceText<'a> + 'a,
429442
{
430443
let lines = split_into_lines(&source);
431-
let line_with_indices_list = lines.map(WithIndices::new).collect::<Vec<_>>();
444+
let line_with_indices_list = lines
445+
.map(|line| WithIndices::new(object_pool, line))
446+
.collect::<Vec<_>>();
432447

433448
if line_with_indices_list.is_empty() {
434449
return GeneratedInfo {
@@ -715,6 +730,8 @@ type InnerSourceIndexValueMapping<'a> =
715730

716731
#[allow(clippy::too_many_arguments)]
717732
pub fn stream_chunks_of_combined_source_map<'a, S>(
733+
options: &MapOptions,
734+
object_pool: &'a ObjectPool,
718735
source: S,
719736
source_map: &'a SourceMap,
720737
inner_source_name: &'a str,
@@ -724,7 +741,6 @@ pub fn stream_chunks_of_combined_source_map<'a, S>(
724741
on_chunk: OnChunk<'_, 'a>,
725742
on_source: OnSource<'_, 'a>,
726743
on_name: OnName<'_, 'a>,
727-
options: &MapOptions,
728744
) -> GeneratedInfo
729745
where
730746
S: SourceText<'a> + 'a,
@@ -781,6 +797,8 @@ where
781797
};
782798

783799
stream_chunks_of_source_map(
800+
options,
801+
object_pool,
784802
source.clone(),
785803
source_map,
786804
&mut |chunk, mapping| {
@@ -835,7 +853,10 @@ where
835853
let inner_source_contents = inner_source_contents.borrow();
836854
match inner_source_contents.get(&inner_source_index) {
837855
Some(Some(source_content)) => {
838-
Some(SourceContentLines::from(source_content.clone()))
856+
Some(SourceContentLines::from(
857+
object_pool,
858+
source_content.clone(),
859+
))
839860
}
840861
_ => None,
841862
}
@@ -934,7 +955,10 @@ where
934955
let inner_source_contents = inner_source_contents.borrow();
935956
match inner_source_contents.get(&inner_source_index) {
936957
Some(Some(source_content)) => {
937-
Some(SourceContentLines::from(source_content.clone()))
958+
Some(SourceContentLines::from(
959+
object_pool,
960+
source_content.clone(),
961+
))
938962
}
939963
_ => None,
940964
}
@@ -1098,6 +1122,11 @@ where
10981122
}
10991123
source_index_mapping.borrow_mut().insert(i, -2);
11001124
stream_chunks_of_source_map(
1125+
&MapOptions {
1126+
columns: options.columns,
1127+
final_source: false,
1128+
},
1129+
object_pool,
11011130
source_content.unwrap().as_ref(),
11021131
inner_source_map,
11031132
&mut |chunk, mapping| {
@@ -1167,10 +1196,6 @@ where
11671196
inner_name_index_mapping.borrow_mut().insert(i, -2);
11681197
inner_name_index_value_mapping.borrow_mut().insert(i, name);
11691198
},
1170-
&MapOptions {
1171-
columns: options.columns,
1172-
final_source: false,
1173-
},
11741199
);
11751200
} else {
11761201
let mut source_mapping = source_mapping.borrow_mut();
@@ -1190,13 +1215,13 @@ where
11901215
name_index_mapping.borrow_mut().insert(i, -2);
11911216
name_index_value_mapping.borrow_mut().insert(i, name);
11921217
},
1193-
options,
11941218
)
11951219
}
11961220

11971221
pub fn stream_and_get_source_and_map<'a, S: StreamChunks>(
1198-
input_source: &'a S,
11991222
options: &MapOptions,
1223+
object_pool: &'a ObjectPool,
1224+
input_source: &'a S,
12001225
on_chunk: OnChunk<'_, 'a>,
12011226
on_source: OnSource<'_, 'a>,
12021227
on_name: OnName<'_, 'a>,
@@ -1208,6 +1233,7 @@ pub fn stream_and_get_source_and_map<'a, S: StreamChunks>(
12081233

12091234
let generated_info = input_source.stream_chunks(
12101235
options,
1236+
object_pool,
12111237
&mut |chunk, mapping| {
12121238
mappings_encoder.encode(&mapping);
12131239
on_chunk(chunk, mapping);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ pub mod stream_chunks {
4242

4343
pub use helpers::{decode_mappings, encode_mappings};
4444

45-
pub use object_pool::with_object_pool_scope;
45+
pub use object_pool::ObjectPool;

0 commit comments

Comments
 (0)