Skip to content

Commit 94ece6f

Browse files
authored
perf: cache ordered modules ids while sorting chunks (#10908)
1 parent 8b32606 commit 94ece6f

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed

crates/rspack_ids/src/deterministic_chunk_ids_plugin.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ async fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_
7777
})
7878
.collect::<UkeyMap<_, _>>();
7979

80+
let mut ordered_chunk_modules_cache = Default::default();
81+
8082
assign_deterministic_ids(
8183
chunks,
8284
|chunk| {
@@ -93,6 +95,7 @@ async fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_
9395
&compilation.module_ids_artifact,
9496
a,
9597
b,
98+
&mut ordered_chunk_modules_cache,
9699
)
97100
},
98101
|chunk, id| {

crates/rspack_ids/src/id_helpers.rs

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use itertools::{
1010
Itertools,
1111
};
1212
use regex::Regex;
13-
use rspack_collections::DatabaseItem;
13+
use rspack_collections::{DatabaseItem, UkeyMap};
1414
use rspack_core::{
1515
compare_runtime, BoxModule, Chunk, ChunkGraph, ChunkGroupByUkey, ChunkUkey, Compilation,
1616
ModuleGraph, ModuleGraphCacheArtifact, ModuleIdentifier, ModuleIdsArtifact,
@@ -120,7 +120,7 @@ pub fn get_hash(s: impl Hash, length: usize) -> String {
120120
pub fn assign_deterministic_ids<T>(
121121
mut items: Vec<T>,
122122
get_name: impl Fn(&T) -> String,
123-
comparator: impl Fn(&T, &T) -> Ordering,
123+
comparator: impl FnMut(&T, &T) -> Ordering,
124124
mut assign_id: impl FnMut(&T, usize) -> bool,
125125
ranges: &[usize],
126126
expand_factor: usize,
@@ -356,25 +356,42 @@ pub fn assign_ascending_chunk_ids(chunks: &[ChunkUkey], compilation: &mut Compil
356356
}
357357
}
358358

359-
fn compare_chunks_by_modules(
359+
fn compare_chunks_by_modules<'a>(
360360
chunk_graph: &ChunkGraph,
361361
module_graph: &ModuleGraph,
362-
module_ids: &ModuleIdsArtifact,
362+
module_ids: &'a ModuleIdsArtifact,
363363
a: &Chunk,
364364
b: &Chunk,
365+
ordered_chunk_modules_cache: &mut UkeyMap<ChunkUkey, Vec<Option<&'a str>>>,
365366
) -> Ordering {
366-
let a_modules = chunk_graph.get_ordered_chunk_modules(&a.ukey(), module_graph);
367-
let b_modules = chunk_graph.get_ordered_chunk_modules(&b.ukey(), module_graph);
367+
let a_ukey = a.ukey();
368+
let b_ukey = b.ukey();
369+
let a_modules = ordered_chunk_modules_cache
370+
.entry(a_ukey)
371+
.or_insert_with(|| {
372+
chunk_graph
373+
.get_ordered_chunk_modules(&a_ukey, module_graph)
374+
.into_iter()
375+
.map(|m| ChunkGraph::get_module_id(module_ids, m.identifier()).map(|s| s.as_str()))
376+
.collect_vec()
377+
})
378+
.clone();
379+
let b_modules = ordered_chunk_modules_cache
380+
.entry(b_ukey)
381+
.or_insert_with(|| {
382+
chunk_graph
383+
.get_ordered_chunk_modules(&b_ukey, module_graph)
384+
.into_iter()
385+
.map(|m| ChunkGraph::get_module_id(module_ids, m.identifier()).map(|s| s.as_str()))
386+
.collect_vec()
387+
})
388+
.clone();
368389

369-
let ordering = a_modules
390+
a_modules
370391
.into_iter()
371392
.zip_longest(b_modules)
372393
.find_map(|pair| match pair {
373-
Both(a_module, b_module) => {
374-
let a_module_id =
375-
ChunkGraph::get_module_id(module_ids, a_module.identifier()).map(|s| s.as_str());
376-
let b_module_id =
377-
ChunkGraph::get_module_id(module_ids, b_module.identifier()).map(|s| s.as_str());
394+
Both(a_module_id, b_module_id) => {
378395
let ordering = compare_ids(
379396
a_module_id.unwrap_or_default(),
380397
b_module_id.unwrap_or_default(),
@@ -387,9 +404,7 @@ fn compare_chunks_by_modules(
387404
Left(_) => Some(Ordering::Greater),
388405
Right(_) => Some(Ordering::Less),
389406
})
390-
.unwrap_or(Ordering::Equal);
391-
392-
ordering
407+
.unwrap_or(Ordering::Equal)
393408
}
394409

395410
fn compare_chunks_by_groups(
@@ -406,13 +421,14 @@ fn compare_chunks_by_groups(
406421
a_groups.cmp_by(b_groups, |a, b| a.cmp(&b))
407422
}
408423

409-
pub fn compare_chunks_natural(
424+
pub fn compare_chunks_natural<'a>(
410425
chunk_graph: &ChunkGraph,
411426
module_graph: &ModuleGraph,
412427
chunk_group_by_ukey: &ChunkGroupByUkey,
413-
module_ids: &ModuleIdsArtifact,
428+
module_ids: &'a ModuleIdsArtifact,
414429
a: &Chunk,
415430
b: &Chunk,
431+
ordered_chunk_modules_cache: &mut UkeyMap<ChunkUkey, Vec<Option<&'a str>>>,
416432
) -> Ordering {
417433
let name_ordering = compare_ids(a.name().unwrap_or_default(), b.name().unwrap_or_default());
418434
if name_ordering != Ordering::Equal {
@@ -424,7 +440,14 @@ pub fn compare_chunks_natural(
424440
return runtime_ordering;
425441
}
426442

427-
let modules_ordering = compare_chunks_by_modules(chunk_graph, module_graph, module_ids, a, b);
443+
let modules_ordering = compare_chunks_by_modules(
444+
chunk_graph,
445+
module_graph,
446+
module_ids,
447+
a,
448+
b,
449+
ordered_chunk_modules_cache,
450+
);
428451
if modules_ordering != Ordering::Equal {
429452
return modules_ordering;
430453
}

crates/rspack_ids/src/named_chunk_ids_plugin.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ fn assign_named_chunk_ids(
9898
let name_to_items_keys = name_to_items.keys().cloned().collect::<FxHashSet<_>>();
9999
let mut unnamed_items = vec![];
100100

101+
let mut ordered_chunk_modules_cache = Default::default();
102+
101103
for (name, mut items) in name_to_items {
102104
if name.is_empty() {
103105
for item in items {
@@ -123,6 +125,7 @@ fn assign_named_chunk_ids(
123125
&compilation.module_ids_artifact,
124126
a,
125127
b,
128+
&mut ordered_chunk_modules_cache,
126129
)
127130
});
128131
let mut i = 0;
@@ -155,6 +158,7 @@ fn assign_named_chunk_ids(
155158
&compilation.module_ids_artifact,
156159
a,
157160
b,
161+
&mut ordered_chunk_modules_cache,
158162
)
159163
});
160164
unnamed_items

crates/rspack_ids/src/natural_chunk_ids_plugin.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_
2828
let module_ids = &compilation.module_ids_artifact;
2929
let chunk_graph = &compilation.chunk_graph;
3030
let module_graph = &compilation.get_module_graph();
31+
let mut ordered_chunk_modules_cache = Default::default();
3132

3233
let chunks = compilation
3334
.chunk_by_ukey
@@ -41,6 +42,7 @@ async fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_
4142
module_ids,
4243
a,
4344
b,
45+
&mut ordered_chunk_modules_cache,
4446
)
4547
})
4648
.map(|chunk| chunk.ukey())

crates/rspack_ids/src/occurrence_chunk_ids_plugin.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ async fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> Result<
6262
occurs_in_initial_chunks_map.insert(chunk.ukey(), occurs);
6363
}
6464

65+
let mut ordered_chunk_modules_cache = Default::default();
6566
let chunks = compilation
6667
.chunk_by_ukey
6768
.values()
@@ -88,6 +89,7 @@ async fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> Result<
8889
&compilation.module_ids_artifact,
8990
a,
9091
b,
92+
&mut ordered_chunk_modules_cache,
9193
)
9294
})
9395
.map(|chunk| chunk.ukey())

0 commit comments

Comments
 (0)