Skip to content

Commit 9795fc8

Browse files
authored
revert: Revert "perf: improve bundle splitting" (#11468)
Revert "perf: improve bundle splitting (#11465)" This reverts commit 65397a5.
1 parent 65397a5 commit 9795fc8

File tree

4 files changed

+49
-41
lines changed

4 files changed

+49
-41
lines changed

crates/rspack_plugin_split_chunks/src/module_group.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ use crate::{
1919
/// The original name of `ModuleGroup` is `ChunkInfoItem` borrowed from Webpack
2020
#[derive(Debug)]
2121
pub(crate) struct ModuleGroup {
22-
pub modules: IdentifierSet,
2322
/// the real index used for mapping the ModuleGroup to corresponding CacheGroup
23+
idx: CacheGroupIdx,
24+
pub modules: IdentifierSet,
2425
pub cache_group_index: usize,
2526
pub cache_group_priority: f64,
2627
pub cache_group_reuse_existing_chunk: bool,
@@ -36,11 +37,13 @@ pub(crate) struct ModuleGroup {
3637

3738
impl ModuleGroup {
3839
pub fn new(
40+
idx: CacheGroupIdx,
3941
chunk_name: Option<String>,
4042
cache_group_index: usize,
4143
cache_group: &CacheGroup,
4244
) -> Self {
4345
Self {
46+
idx,
4447
modules: Default::default(),
4548
cache_group_index,
4649
cache_group_priority: cache_group.priority,
@@ -113,14 +116,20 @@ impl ModuleGroup {
113116
}
114117

115118
pub fn get_cache_group<'a>(&self, cache_groups: &'a [CacheGroup]) -> &'a CacheGroup {
116-
&cache_groups[self.cache_group_index]
119+
&cache_groups[self.idx.0]
117120
}
118121
}
119122

120-
pub(crate) fn compare_entries(
121-
(a_key, a): (&String, &ModuleGroup),
122-
(b_key, b): (&String, &ModuleGroup),
123-
) -> f64 {
123+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
124+
pub(crate) struct CacheGroupIdx(usize);
125+
126+
impl CacheGroupIdx {
127+
pub fn new(idx: usize) -> Self {
128+
Self(idx)
129+
}
130+
}
131+
132+
pub(crate) fn compare_entries(a: &ModuleGroup, b: &ModuleGroup) -> f64 {
124133
// 1. by priority
125134
let diff_priority = a.cache_group_priority - b.cache_group_priority;
126135
if diff_priority != 0f64 {
@@ -161,7 +170,7 @@ pub(crate) fn compare_entries(
161170

162171
loop {
163172
match (modules_a.pop(), modules_b.pop()) {
164-
(None, None) => break,
173+
(None, None) => return 0f64,
165174
(Some(a), Some(b)) => {
166175
let res = a.cmp(b);
167176
if !res.is_eq() {
@@ -171,8 +180,6 @@ pub(crate) fn compare_entries(
171180
_ => unreachable!(),
172181
}
173182
}
174-
175-
a_key.cmp(b_key) as i32 as f64
176183
}
177184

178185
fn total_size(sizes: &SplitChunkSizes) -> f64 {

crates/rspack_plugin_split_chunks/src/plugin/module_group.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ use rspack_core::{
1414
};
1515
use rspack_error::{Result, ToStringResultToRspackResultExt};
1616
use rspack_util::{fx_hash::FxDashMap, tracing_preset::TRACING_BENCH_TARGET};
17-
use rustc_hash::{FxHashMap, FxHasher};
17+
use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
1818
use tracing::instrument;
1919

2020
use super::ModuleGroupMap;
2121
use crate::{
2222
SplitChunksPlugin,
2323
common::{ModuleChunks, ModuleSizes},
24-
module_group::{ModuleGroup, compare_entries},
24+
module_group::{CacheGroupIdx, ModuleGroup, compare_entries},
2525
options::{
2626
cache_group::CacheGroup,
2727
cache_group_test::{CacheGroupTest, CacheGroupTestFnCtx},
@@ -34,6 +34,7 @@ type ChunksKey = u64;
3434
/// If a module meets requirements of a `ModuleGroup`. We consider the `Module` and the `CacheGroup`
3535
/// to be a `MatchedItem`, which are consumed later to calculate `ModuleGroup`.
3636
struct MatchedItem<'a> {
37+
idx: CacheGroupIdx,
3738
module: &'a dyn Module,
3839
cache_group_index: usize,
3940
cache_group: &'a CacheGroup,
@@ -302,7 +303,7 @@ impl SplitChunksPlugin {
302303
let best_entry_key = module_group_map
303304
.iter()
304305
.min_by(|a, b| {
305-
if compare_entries(*a, *b) < 0f64 {
306+
if compare_entries(a.1, b.1) < 0f64 {
306307
Ordering::Greater
307308
} else {
308309
Ordering::Less
@@ -358,7 +359,9 @@ impl SplitChunksPlugin {
358359
let module = module_graph.module_by_identifier(module).expect("should have module").as_ref();
359360
let mut temp = Vec::with_capacity(plugin.cache_groups.len());
360361

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+
362365
let mut is_match = true;
363366
// Filter by `splitChunks.cacheGroups.{cacheGroup}.type`
364367
is_match &= (cache_group.r#type)(module);
@@ -382,27 +385,17 @@ impl SplitChunksPlugin {
382385
}
383386
let mut chunk_key_to_string = HashMap::<ChunksKey, String, ChunksKeyHashBuilder>::default();
384387

385-
let mut filtered = plugin
388+
let filtered = plugin
386389
.cache_groups
387390
.iter()
388391
.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]);
396393

397394
let mut used_exports_combs = None;
398395
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();
405397

398+
for (cache_group_index, (idx, cache_group)) in filtered.enumerate() {
406399
let combs = if cache_group.used_exports {
407400
if used_exports_combs.is_none() {
408401
used_exports_combs = Some(combinator.get_combs(
@@ -480,12 +473,9 @@ impl SplitChunksPlugin {
480473
let selected_chunks_key =
481474
get_key(selected_chunks.iter().copied());
482475

483-
if matched_max_priority.is_none() {
484-
matched_max_priority = Some(cache_group.priority);
485-
}
486-
487476
merge_matched_item_into_module_group_map(
488477
MatchedItem {
478+
idx: CacheGroupIdx::new(idx),
489479
module,
490480
cache_group,
491481
cache_group_index,
@@ -496,6 +486,7 @@ impl SplitChunksPlugin {
496486
&mut chunk_key_to_string,
497487
compilation,
498488
module_sizes,
489+
&mut added_keys,
499490
).await?;
500491
}
501492
}
@@ -607,8 +598,10 @@ async fn merge_matched_item_into_module_group_map(
607598
chunk_key_to_string: &mut HashMap<ChunksKey, String, ChunksKeyHashBuilder>,
608599
compilation: &Compilation,
609600
module_sizes: &ModuleSizes,
601+
added_keys: &mut FxHashSet<String>,
610602
) -> Result<()> {
611603
let MatchedItem {
604+
idx,
612605
module,
613606
cache_group_index,
614607
cache_group,
@@ -654,9 +647,11 @@ async fn merge_matched_item_into_module_group_map(
654647
let mut module_group = {
655648
module_group_map
656649
.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))
658651
};
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+
}
660655
module_group.chunks.extend(selected_chunks.iter().copied());
661656

662657
Ok(())

tests/e2e/cases/css/chunk-loading-order/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { test, expect } from "@/fixtures";
22

33
test("should be red", async ({ page }) => {
44
await expect(page.locator("#status")).toHaveText("ok");
5-
await expect(page.locator("body")).toHaveCSS("color", "rgb(0, 0, 255)");
5+
await expect(page.locator("body")).toHaveCSS("color", "rgb(255, 0, 0)");
66
});

tests/webpack-test/__snapshots__/StatsTestCases.basictest.js.snap

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2521,22 +2521,20 @@ custom-chunks-filter:
25212521
25222522
custom-chunks-filter-in-cache-groups:
25232523
Entrypoint main xx KiB = custom-xxx.js
2524-
Entrypoint a xx KiB = custom-xxx.js
2524+
Entrypoint a xx KiB = custom-xxx.js xx KiB
25252525
Entrypoint b xx KiB = custom-xxx.js xx KiB
25262526
Entrypoint c xx KiB = custom-xxx.js xx KiB
2527-
chunk (runtime: a, main) custom-xxx.js (async-g) xx bytes <{341}> <{545}> <{724}> [rendered]
2527+
chunk (runtime: a, main) custom-xxx.js (async-g) xx bytes <{341}> <{545}> <{70}> <{724}> [rendered]
25282528
> ./g ./a.js 6:0-47
25292529
dependent modules xx bytes [dependent] 1 module
25302530
./g.js xx bytes [built] [code generated]
2531-
chunk (runtime: a) custom-xxx.js (a) xx bytes (javascript) xx KiB (runtime) >{138}< [entry] [rendered]
2531+
chunk (runtime: a) custom-xxx.js (a) xx bytes (javascript) xx KiB (runtime) ={70}= >{138}< [entry] [rendered]
25322532
> ./a a
25332533
> x a
25342534
> y a
25352535
> z a
2536-
dependent modules xx bytes [dependent] 3 modules
2537-
cacheable modules xx bytes
2538-
./a.js + 1 modules xx bytes [code generated]
2539-
./node_modules/z.js xx bytes [built] [code generated]
2536+
dependent modules xx bytes [dependent] 1 module
2537+
./a.js + 1 modules xx bytes [code generated]
25402538
chunk (runtime: b) custom-xxx.js (b) xx bytes (javascript) xx KiB (runtime) ={545}= [entry] [rendered]
25412539
> ./b b
25422540
> x b
@@ -2559,6 +2557,14 @@ custom-chunks-filter-in-cache-groups:
25592557
./node_modules/x.js xx bytes [built] [code generated]
25602558
./node_modules/y.js xx bytes [built] [code generated]
25612559
./node_modules/z.js xx bytes [built] [code generated]
2560+
chunk (runtime: a) custom-xxx.js (id hint: vendors) xx bytes ={341}= >{138}< [initial] [rendered] split chunk (cache group: defaultVendors)
2561+
> ./a a
2562+
> x a
2563+
> y a
2564+
> z a
2565+
./node_modules/x.js xx bytes [built] [code generated]
2566+
./node_modules/y.js xx bytes [built] [code generated]
2567+
./node_modules/z.js xx bytes [built] [code generated]
25622568
chunk (runtime: main) custom-xxx.js (async-b) xx bytes <{889}> ={545}= [rendered]
25632569
> ./b ./index.js 2:0-47
25642570
dependent modules xx bytes [dependent] 2 modules

0 commit comments

Comments
 (0)