@@ -14,14 +14,14 @@ use rspack_core::{
14
14
} ;
15
15
use rspack_error:: { Result , ToStringResultToRspackResultExt } ;
16
16
use rspack_util:: { fx_hash:: FxDashMap , tracing_preset:: TRACING_BENCH_TARGET } ;
17
- use rustc_hash:: { FxHashMap , FxHasher } ;
17
+ use rustc_hash:: { FxHashMap , FxHashSet , FxHasher } ;
18
18
use tracing:: instrument;
19
19
20
20
use super :: ModuleGroupMap ;
21
21
use crate :: {
22
22
SplitChunksPlugin ,
23
23
common:: { ModuleChunks , ModuleSizes } ,
24
- module_group:: { ModuleGroup , compare_entries} ,
24
+ module_group:: { CacheGroupIdx , ModuleGroup , compare_entries} ,
25
25
options:: {
26
26
cache_group:: CacheGroup ,
27
27
cache_group_test:: { CacheGroupTest , CacheGroupTestFnCtx } ,
@@ -34,6 +34,7 @@ type ChunksKey = u64;
34
34
/// If a module meets requirements of a `ModuleGroup`. We consider the `Module` and the `CacheGroup`
35
35
/// to be a `MatchedItem`, which are consumed later to calculate `ModuleGroup`.
36
36
struct MatchedItem < ' a > {
37
+ idx : CacheGroupIdx ,
37
38
module : & ' a dyn Module ,
38
39
cache_group_index : usize ,
39
40
cache_group : & ' a CacheGroup ,
@@ -302,7 +303,7 @@ impl SplitChunksPlugin {
302
303
let best_entry_key = module_group_map
303
304
. iter ( )
304
305
. min_by ( |a, b| {
305
- if compare_entries ( * a , * b ) < 0f64 {
306
+ if compare_entries ( a . 1 , b . 1 ) < 0f64 {
306
307
Ordering :: Greater
307
308
} else {
308
309
Ordering :: Less
@@ -358,7 +359,9 @@ impl SplitChunksPlugin {
358
359
let module = module_graph. module_by_identifier ( module) . expect ( "should have module" ) . as_ref ( ) ;
359
360
let mut temp = Vec :: with_capacity ( plugin. cache_groups . len ( ) ) ;
360
361
361
- for cache_group in plugin. cache_groups . iter ( ) {
362
+ for idx in 0 ..plugin. cache_groups . len ( ) {
363
+ let cache_group = & plugin. cache_groups [ idx] ;
364
+
362
365
let mut is_match = true ;
363
366
// Filter by `splitChunks.cacheGroups.{cacheGroup}.type`
364
367
is_match &= ( cache_group. r#type ) ( module) ;
@@ -382,27 +385,17 @@ impl SplitChunksPlugin {
382
385
}
383
386
let mut chunk_key_to_string = HashMap :: < ChunksKey , String , ChunksKeyHashBuilder > :: default ( ) ;
384
387
385
- let mut filtered = plugin
388
+ let filtered = plugin
386
389
. cache_groups
387
390
. iter ( )
388
391
. enumerate ( )
389
- . filter ( |( index, _) | temp[ * index] ) . collect :: < Vec < _ > > ( ) ;
390
-
391
- filtered. sort_by ( |a, b| {
392
- b. 1 . priority . partial_cmp ( & a. 1 . priority ) . unwrap_or_else ( || {
393
- a. 0 . cmp ( & b. 0 )
394
- } )
395
- } ) ;
392
+ . filter ( |( index, _) | temp[ * index] ) ;
396
393
397
394
let mut used_exports_combs = None ;
398
395
let mut non_used_exports_combs = None ;
399
- let mut matched_max_priority = None ;
400
-
401
- for ( cache_group_index, cache_group) in filtered. into_iter ( ) {
402
- if let Some ( matched_max_priority) = matched_max_priority && cache_group. priority < matched_max_priority {
403
- break ;
404
- }
396
+ let mut added_keys = FxHashSet :: default ( ) ;
405
397
398
+ for ( cache_group_index, ( idx, cache_group) ) in filtered. enumerate ( ) {
406
399
let combs = if cache_group. used_exports {
407
400
if used_exports_combs. is_none ( ) {
408
401
used_exports_combs = Some ( combinator. get_combs (
@@ -480,12 +473,9 @@ impl SplitChunksPlugin {
480
473
let selected_chunks_key =
481
474
get_key ( selected_chunks. iter ( ) . copied ( ) ) ;
482
475
483
- if matched_max_priority. is_none ( ) {
484
- matched_max_priority = Some ( cache_group. priority ) ;
485
- }
486
-
487
476
merge_matched_item_into_module_group_map (
488
477
MatchedItem {
478
+ idx : CacheGroupIdx :: new ( idx) ,
489
479
module,
490
480
cache_group,
491
481
cache_group_index,
@@ -496,6 +486,7 @@ impl SplitChunksPlugin {
496
486
& mut chunk_key_to_string,
497
487
compilation,
498
488
module_sizes,
489
+ & mut added_keys,
499
490
) . await ?;
500
491
}
501
492
}
@@ -607,8 +598,10 @@ async fn merge_matched_item_into_module_group_map(
607
598
chunk_key_to_string : & mut HashMap < ChunksKey , String , ChunksKeyHashBuilder > ,
608
599
compilation : & Compilation ,
609
600
module_sizes : & ModuleSizes ,
601
+ added_keys : & mut FxHashSet < String > ,
610
602
) -> Result < ( ) > {
611
603
let MatchedItem {
604
+ idx,
612
605
module,
613
606
cache_group_index,
614
607
cache_group,
@@ -654,9 +647,11 @@ async fn merge_matched_item_into_module_group_map(
654
647
let mut module_group = {
655
648
module_group_map
656
649
. entry ( key. clone ( ) )
657
- . or_insert_with ( || ModuleGroup :: new ( chunk_name, cache_group_index, cache_group) )
650
+ . or_insert_with ( || ModuleGroup :: new ( idx , chunk_name. clone ( ) , cache_group_index, cache_group) )
658
651
} ;
659
- module_group. add_module ( module. identifier ( ) , module_sizes) ;
652
+ if chunk_name. is_none ( ) || added_keys. insert ( key) {
653
+ module_group. add_module ( module. identifier ( ) , module_sizes) ;
654
+ }
660
655
module_group. chunks . extend ( selected_chunks. iter ( ) . copied ( ) ) ;
661
656
662
657
Ok ( ( ) )
0 commit comments