Skip to content

Commit f3d0dde

Browse files
committed
perf: reduce depth assignment allocations
1 parent d7ddd76 commit f3d0dde

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

crates/rspack_core/src/compilation/build_chunk_graph/code_splitter.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
733733
compilation: &mut Compilation,
734734
) -> Result<FxIndexMap<ChunkGroupUkey, Vec<ModuleIdentifier>>> {
735735
let mut input_entrypoints_and_modules: FxIndexMap<ChunkGroupUkey, Vec<ModuleIdentifier>> =
736-
FxIndexMap::default();
736+
FxIndexMap::with_capacity_and_hasher(compilation.entries.len(), Default::default());
737737

738738
let entries = compilation.entries.keys().cloned().collect::<Vec<_>>();
739739

@@ -742,12 +742,16 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
742742
all_modules
743743
.par_iter()
744744
.map(|m| {
745-
(
746-
*m,
747-
mg.get_outgoing_connections(m)
748-
.map(|con| *con.module_identifier())
749-
.collect::<Vec<_>>(),
750-
)
745+
let mut outgoing = mg
746+
.module_graph_module_by_identifier(m)
747+
.map(|mgm| Vec::with_capacity(mgm.outgoing_connections().len()))
748+
.unwrap_or_default();
749+
750+
for con in mg.get_outgoing_connections(m) {
751+
outgoing.push(*con.module_identifier());
752+
}
753+
754+
(*m, outgoing)
751755
})
752756
.collect::<IdentifierMap<_>>()
753757
};
@@ -756,12 +760,23 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
756760
.iter()
757761
.map(|name| self.prepare_entry_input(name, compilation))
758762
.collect::<Result<Vec<_>>>()?;
763+
let initial_depth_capacity = all_modules
764+
.len()
765+
.checked_div(entries.len())
766+
.unwrap_or_default();
759767

760768
let assign_depths_maps = assign_tasks
761769
.par_iter()
762770
.map(|(_, modules)| {
763-
let mut assign_depths_map = IdentifierMap::default();
764-
assign_depths(&mut assign_depths_map, modules.iter(), &outgoings);
771+
let initial_depth_capacity = initial_depth_capacity.max(modules.len());
772+
let mut assign_depths_map =
773+
IdentifierMap::with_capacity_and_hasher(initial_depth_capacity, Default::default());
774+
assign_depths(
775+
&mut assign_depths_map,
776+
modules.iter(),
777+
&outgoings,
778+
initial_depth_capacity,
779+
);
765780
assign_depths_map
766781
})
767782
.collect::<Vec<_>>();

crates/rspack_core/src/compilation/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,11 +1459,12 @@ impl AssetInfoRelated {
14591459
/// the same time.
14601460
pub fn assign_depths<'a>(
14611461
assign_map: &mut IdentifierMap<usize>,
1462-
modules: impl Iterator<Item = &'a ModuleIdentifier>,
1462+
modules: impl ExactSizeIterator<Item = &'a ModuleIdentifier>,
14631463
outgoings: &IdentifierMap<Vec<ModuleIdentifier>>,
1464+
initial_queue_capacity: usize,
14641465
) {
14651466
// https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/Compilation.js#L3720
1466-
let mut q = VecDeque::new();
1467+
let mut q = VecDeque::with_capacity(initial_queue_capacity.max(modules.len()));
14671468
for item in modules {
14681469
q.push_back((*item, 0));
14691470
}

0 commit comments

Comments
 (0)