Skip to content

Commit 3844606

Browse files
authored
perf: object pool (#192)
1 parent 98db0a7 commit 3844606

19 files changed

+557
-203
lines changed

Cargo.lock

Lines changed: 46 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,15 @@ dyn-clone = "1.0.17"
3636
rustc-hash = "2.1.0"
3737
memchr = "2.7.4"
3838

39-
4039
codspeed-criterion-compat = { version = "2.7.2", default-features = false, optional = true }
4140
static_assertions = "1.1.0"
4241
simd-json = "0.14.3"
43-
self_cell = "1.2.1"
4442

4543
[dev-dependencies]
4644
twox-hash = "2.1.0"
4745
regex = "1.11.1"
4846
criterion = { version = "0.5.1", default-features = false }
47+
rayon = "1.11.0"
4948

5049
[features]
5150
codspeed = ["codspeed-criterion-compat"]

benches/bench.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,23 @@ 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::{
21-
benchmark_complex_replace_source_map, benchmark_complex_replace_source_source,
21+
benchmark_complex_replace_source_map, benchmark_complex_replace_source_size,
22+
benchmark_complex_replace_source_source,
2223
};
2324
use bench_source_map::{
2425
benchmark_parse_source_map_from_json, benchmark_source_map_clone,
25-
benchmark_stringify_source_map_to_json,
2626
};
2727

2828
use benchmark_repetitive_react_components::{
2929
benchmark_repetitive_react_components_map,
3030
benchmark_repetitive_react_components_source,
3131
};
3232

33-
use crate::bench_complex_replace_source::benchmark_complex_replace_source_size;
34-
3533
const HELLOWORLD_JS: &str = include_str!(concat!(
3634
env!("CARGO_MANIFEST_DIR"),
3735
"/benches/fixtures/transpile-minify/files/helloworld.js"
@@ -80,7 +78,7 @@ fn benchmark_concat_generate_string(b: &mut Bencher) {
8078

8179
b.iter(|| {
8280
concat
83-
.map(&MapOptions::default())
81+
.map(&ObjectPool::default(), &MapOptions::default())
8482
.unwrap()
8583
.to_json()
8684
.unwrap();
@@ -109,7 +107,7 @@ fn benchmark_concat_generate_string_with_cache(b: &mut Bencher) {
109107

110108
b.iter(|| {
111109
cached
112-
.map(&MapOptions::default())
110+
.map(&ObjectPool::default(), &MapOptions::default())
113111
.unwrap()
114112
.to_json()
115113
.unwrap();
@@ -178,11 +176,6 @@ fn bench_rspack_sources(criterion: &mut Criterion) {
178176

179177
group.bench_function("source_map_clone", benchmark_source_map_clone);
180178

181-
group.bench_function(
182-
"stringify_source_map_to_json",
183-
benchmark_stringify_source_map_to_json,
184-
);
185-
186179
group.bench_function(
187180
"repetitive_react_components_map",
188181
benchmark_repetitive_react_components_map,

benches/bench_complex_replace_source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ 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
};
1414

1515
static LARGE_REPLACE_SOURCE: LazyLock<BoxSource> = LazyLock::new(|| {
@@ -36724,7 +36724,7 @@ pub fn benchmark_complex_replace_source_map(b: &mut Bencher) {
3672436724
let source = LARGE_REPLACE_SOURCE.clone();
3672536725

3672636726
b.iter(|| {
36727-
black_box(source.map(&MapOptions::default()));
36727+
black_box(source.map(&ObjectPool::default(), &MapOptions::default()));
3672836728
});
3672936729
}
3673036730

benches/bench_source_map.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,3 @@ pub fn benchmark_source_map_clone(b: &mut Bencher) {
2525
let _ = black_box(source.clone());
2626
})
2727
}
28-
29-
pub fn benchmark_stringify_source_map_to_json(b: &mut Bencher) {
30-
let source = SourceMap::from_json(ANTD_MIN_JS_MAP).unwrap();
31-
b.iter(|| {
32-
let _ = black_box(source.to_json().unwrap());
33-
})
34-
}

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: 27 additions & 12 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,
@@ -99,18 +100,22 @@ impl Source for CachedSource {
99100
*self.cache.size.get_or_init(|| self.inner.size())
100101
}
101102

102-
fn map(&self, options: &MapOptions) -> Option<SourceMap> {
103+
fn map(
104+
&self,
105+
object_pool: &ObjectPool,
106+
options: &MapOptions,
107+
) -> Option<SourceMap> {
103108
if options.columns {
104109
self
105110
.cache
106111
.columns_map
107-
.get_or_init(|| self.inner.map(options))
112+
.get_or_init(|| self.inner.map(object_pool, options))
108113
.clone()
109114
} else {
110115
self
111116
.cache
112117
.line_only_map
113-
.get_or_init(|| self.inner.map(options))
118+
.get_or_init(|| self.inner.map(object_pool, options))
114119
.clone()
115120
}
116121
}
@@ -123,6 +128,7 @@ impl Source for CachedSource {
123128
impl StreamChunks for CachedSource {
124129
fn stream_chunks<'a>(
125130
&'a self,
131+
object_pool: &'a ObjectPool,
126132
options: &MapOptions,
127133
on_chunk: crate::helpers::OnChunk<'_, 'a>,
128134
on_source: crate::helpers::OnSource<'_, 'a>,
@@ -138,7 +144,13 @@ impl StreamChunks for CachedSource {
138144
let source = self.rope();
139145
if let Some(map) = map {
140146
stream_chunks_of_source_map(
141-
source, map, on_chunk, on_source, on_name, options,
147+
options,
148+
object_pool,
149+
source,
150+
map,
151+
on_chunk,
152+
on_source,
153+
on_name,
142154
)
143155
} else {
144156
stream_chunks_of_raw_source(
@@ -148,8 +160,9 @@ impl StreamChunks for CachedSource {
148160
}
149161
None => {
150162
let (generated_info, map) = stream_and_get_source_and_map(
151-
&self.inner,
152163
options,
164+
object_pool,
165+
&self.inner,
153166
on_chunk,
154167
on_source,
155168
on_name,
@@ -233,7 +246,9 @@ mod tests {
233246
})
234247
.boxed(),
235248
]);
236-
let map = source.map(&Default::default()).unwrap();
249+
let map = source
250+
.map(&ObjectPool::default(), &Default::default())
251+
.unwrap();
237252
assert_eq!(map.mappings(), ";;AACA");
238253
}
239254

@@ -248,11 +263,11 @@ mod tests {
248263
source.source();
249264
source.buffer();
250265
source.size();
251-
source.map(&map_options);
266+
source.map(&ObjectPool::default(), &map_options);
252267

253268
assert_eq!(
254269
*clone.cache.columns_map.get().unwrap(),
255-
source.map(&map_options)
270+
source.map(&ObjectPool::default(), &map_options)
256271
);
257272
}
258273

@@ -302,16 +317,14 @@ mod tests {
302317

303318
#[test]
304319
fn should_produce_correct_output_for_cached_raw_source() {
305-
let map_options = MapOptions {
306-
columns: true,
307-
final_source: true,
308-
};
320+
let map_options = MapOptions::new(true);
309321

310322
let source = RawStringSource::from("Test\nTest\nTest\n");
311323
let mut on_chunk_count = 0;
312324
let mut on_source_count = 0;
313325
let mut on_name_count = 0;
314326
let generated_info = source.stream_chunks(
327+
&ObjectPool::default(),
315328
&map_options,
316329
&mut |_chunk, _mapping| {
317330
on_chunk_count += 1;
@@ -326,6 +339,7 @@ mod tests {
326339

327340
let cached_source = CachedSource::new(source);
328341
cached_source.stream_chunks(
342+
&ObjectPool::default(),
329343
&map_options,
330344
&mut |_chunk, _mapping| {},
331345
&mut |_source_index, _source, _source_content| {},
@@ -336,6 +350,7 @@ mod tests {
336350
let mut cached_on_source_count = 0;
337351
let mut cached_on_name_count = 0;
338352
let cached_generated_info = cached_source.stream_chunks(
353+
&ObjectPool::default(),
339354
&map_options,
340355
&mut |_chunk, _mapping| {
341356
cached_on_chunk_count += 1;

0 commit comments

Comments
 (0)