Skip to content

Commit c1e897a

Browse files
committed
perf: avoid indexmap in code splitter prepare
1 parent f3d0dde commit c1e897a

1 file changed

Lines changed: 74 additions & 30 deletions

File tree

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

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,60 @@ pub(crate) type DependenciesBlockIdentifierSet =
3434
std::collections::HashSet<DependenciesBlockIdentifier, BuildHasherDefault<FxHasher>>;
3535

3636
type ConnectionIdList = Arc<[DependencyId]>;
37-
type PreparedBlockConnectionMap =
38-
FxIndexMap<(DependenciesBlockIdentifier, ModuleIdentifier), ConnectionIdList>;
37+
type PreparedBlockConnectionMap = Vec<PreparedBlockConnection>;
3938
type BlockConnectionMap =
4039
DependenciesBlockIdentifierMap<Arc<Vec<(ModuleIdentifier, ConnectionState, ConnectionIdList)>>>;
4140

41+
#[derive(Debug, Clone)]
42+
struct PreparedBlockConnection {
43+
block: DependenciesBlockIdentifier,
44+
module: ModuleIdentifier,
45+
connections: ConnectionIdList,
46+
}
47+
48+
struct PreparedBlockConnectionBuilder {
49+
block: DependenciesBlockIdentifier,
50+
module: ModuleIdentifier,
51+
dependency: DependencyId,
52+
}
53+
54+
struct PreparedBlockConnectionGroupBuilder {
55+
block: DependenciesBlockIdentifier,
56+
module: ModuleIdentifier,
57+
connections: Vec<DependencyId>,
58+
}
59+
60+
fn finalize_prepared_connection_map(
61+
connections: Vec<PreparedBlockConnectionBuilder>,
62+
) -> PreparedBlockConnectionMap {
63+
let mut groups = Vec::<PreparedBlockConnectionGroupBuilder>::new();
64+
let mut group_index_by_block = DependenciesBlockIdentifierMap::<IdentifierMap<usize>>::default();
65+
for connection in connections {
66+
let index_by_module = group_index_by_block.entry(connection.block).or_default();
67+
if let Some(index) = index_by_module.get(&connection.module) {
68+
groups[*index].connections.push(connection.dependency);
69+
continue;
70+
}
71+
72+
let index = groups.len();
73+
index_by_module.insert(connection.module, index);
74+
groups.push(PreparedBlockConnectionGroupBuilder {
75+
block: connection.block,
76+
module: connection.module,
77+
connections: vec![connection.dependency],
78+
});
79+
}
80+
81+
groups
82+
.into_iter()
83+
.map(|connection| PreparedBlockConnection {
84+
block: connection.block,
85+
module: connection.module,
86+
connections: Arc::from(connection.connections.into_boxed_slice()),
87+
})
88+
.collect()
89+
}
90+
4291
#[derive(Debug, Clone, Default)]
4392
pub struct ChunkGroupInfo {
4493
pub initialized: bool,
@@ -2268,12 +2317,7 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
22682317
.module_graph_module_by_identifier(module)
22692318
.map(|mgm| mgm.all_dependencies())
22702319
.unwrap_or_default();
2271-
let mut connection_map = FxIndexMap::<
2272-
(DependenciesBlockIdentifier, ModuleIdentifier),
2273-
Vec<DependencyId>,
2274-
>::with_capacity_and_hasher(
2275-
all_dependencies.len(), Default::default()
2276-
);
2320+
let mut connection_map = Vec::new();
22772321

22782322
let mut ordered_deps = Vec::new();
22792323
let mut unordered_deps = Vec::new();
@@ -2304,27 +2348,23 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
23042348
}
23052349
ordered_deps.sort_by_key(|(source_order, _, _, _)| *source_order);
23062350

2307-
for (_, block_id, dep_id, module_identifier) in ordered_deps {
2308-
connection_map
2309-
.entry((block_id, module_identifier))
2310-
.and_modify(|e| e.push(dep_id))
2311-
.or_insert_with(|| vec![dep_id]);
2351+
for (_, block, dependency, module) in ordered_deps {
2352+
connection_map.push(PreparedBlockConnectionBuilder {
2353+
block,
2354+
module,
2355+
dependency,
2356+
});
23122357
}
23132358

2314-
for (block_id, dep_id, module_identifier) in unordered_deps {
2315-
connection_map
2316-
.entry((block_id, module_identifier))
2317-
.and_modify(|e| e.push(dep_id))
2318-
.or_insert_with(|| vec![dep_id]);
2359+
for (block, dependency, module) in unordered_deps {
2360+
connection_map.push(PreparedBlockConnectionBuilder {
2361+
block,
2362+
module,
2363+
dependency,
2364+
});
23192365
}
23202366

2321-
(
2322-
*module,
2323-
connection_map
2324-
.into_iter()
2325-
.map(|(key, connections)| (key, Arc::from(connections.into_boxed_slice())))
2326-
.collect(),
2327-
)
2367+
(*module, finalize_prepared_connection_map(connection_map))
23282368
})
23292369
.collect::<IdentifierMap<_>>();
23302370

@@ -2476,15 +2516,15 @@ fn extract_block_modules(
24762516
.get(&module)
24772517
.expect("should have outgoing deps");
24782518

2479-
for ((block_id, module_identifier), connections) in connection_map {
2480-
if map.contains_key(block_id) {
2519+
for connection in connection_map {
2520+
if map.contains_key(&connection.block) {
24812521
continue;
24822522
}
24832523
let modules = module_map
2484-
.get_mut(block_id)
2524+
.get_mut(&connection.block)
24852525
.expect("should have modules in block_modules_runtime_map");
24862526
let active_state = get_active_state_of_connections(
2487-
connections,
2527+
&connection.connections,
24882528
runtime.as_deref(),
24892529
compilation.get_module_graph(),
24902530
&compilation.module_graph_cache_artifact,
@@ -2493,7 +2533,11 @@ fn extract_block_modules(
24932533
.side_effects_state_artifact,
24942534
&compilation.exports_info_artifact,
24952535
);
2496-
modules.push((*module_identifier, active_state, connections.clone()));
2536+
modules.push((
2537+
connection.module,
2538+
active_state,
2539+
connection.connections.clone(),
2540+
));
24972541
}
24982542
for (block, modules) in module_map {
24992543
map.insert(block, Arc::new(modules));

0 commit comments

Comments
 (0)