@@ -10,7 +10,7 @@ use itertools::{
10
10
Itertools ,
11
11
} ;
12
12
use regex:: Regex ;
13
- use rspack_collections:: DatabaseItem ;
13
+ use rspack_collections:: { DatabaseItem , UkeyMap } ;
14
14
use rspack_core:: {
15
15
compare_runtime, BoxModule , Chunk , ChunkGraph , ChunkGroupByUkey , ChunkUkey , Compilation ,
16
16
ModuleGraph , ModuleGraphCacheArtifact , ModuleIdentifier , ModuleIdsArtifact ,
@@ -120,7 +120,7 @@ pub fn get_hash(s: impl Hash, length: usize) -> String {
120
120
pub fn assign_deterministic_ids < T > (
121
121
mut items : Vec < T > ,
122
122
get_name : impl Fn ( & T ) -> String ,
123
- comparator : impl Fn ( & T , & T ) -> Ordering ,
123
+ comparator : impl FnMut ( & T , & T ) -> Ordering ,
124
124
mut assign_id : impl FnMut ( & T , usize ) -> bool ,
125
125
ranges : & [ usize ] ,
126
126
expand_factor : usize ,
@@ -356,25 +356,42 @@ pub fn assign_ascending_chunk_ids(chunks: &[ChunkUkey], compilation: &mut Compil
356
356
}
357
357
}
358
358
359
- fn compare_chunks_by_modules (
359
+ fn compare_chunks_by_modules < ' a > (
360
360
chunk_graph : & ChunkGraph ,
361
361
module_graph : & ModuleGraph ,
362
- module_ids : & ModuleIdsArtifact ,
362
+ module_ids : & ' a ModuleIdsArtifact ,
363
363
a : & Chunk ,
364
364
b : & Chunk ,
365
+ ordered_chunk_modules_cache : & mut UkeyMap < ChunkUkey , Vec < Option < & ' a str > > > ,
365
366
) -> 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 ( ) ;
368
389
369
- let ordering = a_modules
390
+ a_modules
370
391
. into_iter ( )
371
392
. zip_longest ( b_modules)
372
393
. 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) => {
378
395
let ordering = compare_ids (
379
396
a_module_id. unwrap_or_default ( ) ,
380
397
b_module_id. unwrap_or_default ( ) ,
@@ -387,9 +404,7 @@ fn compare_chunks_by_modules(
387
404
Left ( _) => Some ( Ordering :: Greater ) ,
388
405
Right ( _) => Some ( Ordering :: Less ) ,
389
406
} )
390
- . unwrap_or ( Ordering :: Equal ) ;
391
-
392
- ordering
407
+ . unwrap_or ( Ordering :: Equal )
393
408
}
394
409
395
410
fn compare_chunks_by_groups (
@@ -406,13 +421,14 @@ fn compare_chunks_by_groups(
406
421
a_groups. cmp_by ( b_groups, |a, b| a. cmp ( & b) )
407
422
}
408
423
409
- pub fn compare_chunks_natural (
424
+ pub fn compare_chunks_natural < ' a > (
410
425
chunk_graph : & ChunkGraph ,
411
426
module_graph : & ModuleGraph ,
412
427
chunk_group_by_ukey : & ChunkGroupByUkey ,
413
- module_ids : & ModuleIdsArtifact ,
428
+ module_ids : & ' a ModuleIdsArtifact ,
414
429
a : & Chunk ,
415
430
b : & Chunk ,
431
+ ordered_chunk_modules_cache : & mut UkeyMap < ChunkUkey , Vec < Option < & ' a str > > > ,
416
432
) -> Ordering {
417
433
let name_ordering = compare_ids ( a. name ( ) . unwrap_or_default ( ) , b. name ( ) . unwrap_or_default ( ) ) ;
418
434
if name_ordering != Ordering :: Equal {
@@ -424,7 +440,14 @@ pub fn compare_chunks_natural(
424
440
return runtime_ordering;
425
441
}
426
442
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
+ ) ;
428
451
if modules_ordering != Ordering :: Equal {
429
452
return modules_ordering;
430
453
}
0 commit comments