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