Skip to content

Commit 5e95aaa

Browse files
authored
perf: remove useless iterator in new code splitting and some allocations (#9821)
perf: fix useless iterator in new code splitting
1 parent f61c7ab commit 5e95aaa

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

crates/rspack_core/src/build_chunk_graph/available_modules.rs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::borrow::Cow;
22

33
use rspack_collections::IdentifierMap;
44
use rustc_hash::FxHashSet as HashSet;
5+
use tracing::instrument;
56

67
use super::new_code_splitter::{CacheableChunkItem, ChunkDesc, EntryChunkDesc};
78
use crate::Compilation;
@@ -78,6 +79,7 @@ impl AvailableModules {
7879
}
7980
}
8081

82+
#[instrument(skip_all)]
8183
#[allow(unused_variables)]
8284
pub fn remove_available_modules(
8385
compilation: &Compilation,
@@ -126,11 +128,13 @@ pub fn remove_available_modules(
126128
continue;
127129
}
128130
let res = res.into_owned();
129-
*curr = res.clone();
130-
res
131+
*curr = res;
132+
curr
131133
} else {
132-
available_modules[chunk_index] = Some(parent_available_modules.clone());
133-
parent_available_modules
134+
available_modules[chunk_index] = Some(parent_available_modules);
135+
available_modules[chunk_index]
136+
.as_ref()
137+
.expect("should have available modules")
134138
};
135139

136140
// we have incomings that are not calculated, wait till we calculated
@@ -175,14 +179,18 @@ pub fn remove_available_modules(
175179
}
176180

177181
let module_graph = compilation.get_module_graph();
182+
let mut removed = HashSet::default();
183+
let mut disconnect_children = HashSet::default();
184+
178185
for (chunk_index, available) in available_modules.iter().enumerate() {
186+
removed.clear();
187+
disconnect_children.clear();
188+
179189
let chunk = &mut chunks[chunk_index].1.chunk_desc;
180190
let Some(available) = available else {
181191
continue;
182192
};
183193

184-
let mut removed = HashSet::default();
185-
186194
chunk.chunk_modules_mut().retain(|module_identifier| {
187195
let module = {
188196
#[cfg(debug_assertions)]
@@ -207,29 +215,30 @@ pub fn remove_available_modules(
207215
!in_parent
208216
});
209217

218+
if removed.is_empty() {
219+
continue;
220+
}
221+
210222
let chunk = &chunks[chunk_index].1.chunk_desc;
211223
let outgoings = chunk.outgoings();
212-
for removed_block_id in &removed {
213-
let mut disconnect_children = HashSet::default();
214-
215-
chunk_children[chunk_index].iter().for_each(|child| {
216-
let child_chunk = &chunks[*child].1.chunk_desc;
217-
218-
// if all incomings from current chunk are removed, we can remove this child
219-
if child_chunk.incomings().iter().all(|incoming| {
220-
// if all incomings are not from current chunk, we disconnect them
221-
!removed.contains(incoming) && !outgoings.contains(incoming)
222-
}) {
223-
disconnect_children.insert(*child);
224-
}
225-
});
226224

227-
if !disconnect_children.is_empty() {
228-
chunk_children[chunk_index].retain(|child| !disconnect_children.contains(child));
225+
chunk_children[chunk_index].iter().for_each(|child| {
226+
let child_chunk = &chunks[*child].1.chunk_desc;
229227

230-
for dead_child in disconnect_children {
231-
chunk_parents[dead_child].retain(|parent| *parent != chunk_index);
232-
}
228+
// if all incomings from current chunk are removed, we can remove this child
229+
if child_chunk.incomings().iter().all(|incoming| {
230+
// if all incomings are not from current chunk, we disconnect them
231+
!removed.contains(incoming) && !outgoings.contains(incoming)
232+
}) {
233+
disconnect_children.insert(*child);
234+
}
235+
});
236+
237+
if !disconnect_children.is_empty() {
238+
chunk_children[chunk_index].retain(|child| !disconnect_children.contains(child));
239+
240+
for dead_child in &disconnect_children {
241+
chunk_parents[*dead_child].retain(|parent| *parent != chunk_index);
233242
}
234243
}
235244
}

crates/rspack_core/src/build_chunk_graph/new_code_splitter.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rspack_collections::{
1414
};
1515
use rspack_error::{error, Diagnostic, Result};
1616
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
17+
use tracing::instrument;
1718

1819
use super::available_modules::{remove_available_modules, AvailableModules};
1920
use crate::{
@@ -389,6 +390,7 @@ impl CodeSplitter {
389390
}
390391
}
391392

393+
#[instrument(skip_all)]
392394
fn analyze_module_graph(
393395
&mut self,
394396
compilation: &mut Compilation,
@@ -1418,6 +1420,7 @@ Or do you want to use the entrypoints '{name}' and '{entry_runtime}' independent
14181420

14191421
// 1. determine parent child relationship
14201422
// 2. remove modules that exist in all parents
1423+
#[instrument(skip_all)]
14211424
fn finalize_chunk_desc(
14221425
&mut self,
14231426
mut chunks: Vec<(bool, CacheableChunkItem)>,
@@ -1473,7 +1476,9 @@ Or do you want to use the entrypoints '{name}' and '{entry_runtime}' independent
14731476
}
14741477

14751478
// 2nd iter, analyze chunk relations
1479+
let mut parents = HashSet::default();
14761480
for (idx, (_, cache)) in chunks.iter().enumerate() {
1481+
parents.clear();
14771482
match &cache.chunk_desc {
14781483
ChunkDesc::Entry(entry) => {
14791484
if let Some(depend_on) = &entry.options.depend_on {
@@ -1493,7 +1498,6 @@ Or do you want to use the entrypoints '{name}' and '{entry_runtime}' independent
14931498
}
14941499
}
14951500
ChunkDesc::Chunk(chunk) => {
1496-
let mut parents = HashSet::default();
14971501
for block in &chunk.incoming_blocks {
14981502
let Some(chunk_parents) = chunks_by_block.get(block) else {
14991503
continue;
@@ -1503,8 +1507,9 @@ Or do you want to use the entrypoints '{name}' and '{entry_runtime}' independent
15031507
}
15041508
chunk_parents.push(
15051509
parents
1506-
.into_iter()
1507-
.filter(|parent| *parent != idx)
1510+
.iter()
1511+
.filter(|parent| **parent != idx)
1512+
.copied()
15081513
.collect::<Vec<_>>(),
15091514
);
15101515
}
@@ -1605,6 +1610,7 @@ Or do you want to use the entrypoints '{name}' and '{entry_runtime}' independent
16051610
}
16061611

16071612
// merge chunks that has the same name on them
1613+
#[instrument(skip_all)]
16081614
fn merge_same_chunks(
16091615
&mut self,
16101616
chunks: Vec<(bool, CacheableChunkItem)>,

0 commit comments

Comments
 (0)