Skip to content

Commit b57e1e5

Browse files
authored
perf: parallelize processing chunk hashes (#10912)
1 parent 4e2081b commit b57e1e5

File tree

1 file changed

+62
-23
lines changed

1 file changed

+62
-23
lines changed

crates/rspack_core/src/compiler/compilation.rs

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::{
99
};
1010

1111
use dashmap::DashSet;
12-
use futures::future::join_all;
1312
use indexmap::{IndexMap, IndexSet};
1413
use itertools::Itertools;
1514
use rayon::prelude::*;
@@ -2252,23 +2251,50 @@ impl Compilation {
22522251
.iter()
22532252
.filter(|key| !unordered_runtime_chunks.contains(key))
22542253
.collect();
2254+
22552255
// create hash for runtime modules in other chunks
2256-
for chunk in &other_chunks {
2257-
for runtime_module_identifier in self.chunk_graph.get_chunk_runtime_modules_iterable(chunk) {
2258-
let runtime_module = &self.runtime_modules[runtime_module_identifier];
2259-
let digest = runtime_module.get_runtime_hash(self, None).await?;
2260-
self
2261-
.runtime_modules_hash
2262-
.insert(*runtime_module_identifier, digest);
2263-
}
2256+
let other_chunk_runtime_module_hashes = rspack_futures::scope::<_, Result<_>>(|token| {
2257+
other_chunks
2258+
.iter()
2259+
.flat_map(|chunk| self.chunk_graph.get_chunk_runtime_modules_iterable(chunk))
2260+
.for_each(|runtime_module_identifier| {
2261+
let s = unsafe { token.used((&self, runtime_module_identifier)) };
2262+
s.spawn(|(compilation, runtime_module_identifier)| async {
2263+
let runtime_module = &compilation.runtime_modules[runtime_module_identifier];
2264+
let digest = runtime_module.get_runtime_hash(compilation, None).await?;
2265+
Ok((*runtime_module_identifier, digest))
2266+
});
2267+
})
2268+
})
2269+
.await
2270+
.into_iter()
2271+
.map(|res| res.to_rspack_result())
2272+
.collect::<Result<Vec<_>>>()?;
2273+
2274+
for res in other_chunk_runtime_module_hashes {
2275+
let (runtime_module_identifier, digest) = res?;
2276+
self
2277+
.runtime_modules_hash
2278+
.insert(runtime_module_identifier, digest);
22642279
}
2280+
22652281
// create hash for other chunks
2266-
let other_chunks_hash_results: Vec<Result<(ChunkUkey, ChunkHashResult)>> =
2267-
join_all(other_chunks.into_iter().map(|chunk| async {
2268-
let hash_result = self.process_chunk_hash(*chunk, &plugin_driver).await?;
2269-
Ok((*chunk, hash_result))
2270-
}))
2271-
.await;
2282+
let other_chunks_hash_results = rspack_futures::scope::<_, Result<_>>(|token| {
2283+
for chunk in other_chunks {
2284+
let s = unsafe { token.used((&self, chunk, &plugin_driver)) };
2285+
s.spawn(|(compilation, chunk, plugin_driver)| async {
2286+
let hash_result = compilation
2287+
.process_chunk_hash(*chunk, plugin_driver)
2288+
.await?;
2289+
Ok((*chunk, hash_result))
2290+
});
2291+
}
2292+
})
2293+
.await
2294+
.into_iter()
2295+
.map(|res| res.to_rspack_result())
2296+
.collect::<Result<Vec<_>>>()?;
2297+
22722298
try_process_chunk_hash_results(self, other_chunks_hash_results)?;
22732299
logger.time_end(start);
22742300

@@ -2379,16 +2405,29 @@ impl Compilation {
23792405
// Therefore, create hashes one by one in sequence.
23802406
let start = logger.time("hashing: hash runtime chunks");
23812407
for runtime_chunk_ukey in runtime_chunks {
2382-
for runtime_module_identifier in self
2383-
.chunk_graph
2384-
.get_chunk_runtime_modules_iterable(&runtime_chunk_ukey)
2385-
{
2386-
let runtime_module = &self.runtime_modules[runtime_module_identifier];
2387-
let digest = runtime_module.get_runtime_hash(self, None).await?;
2408+
let runtime_module_hashes = rspack_futures::scope::<_, Result<_>>(|token| {
23882409
self
2389-
.runtime_modules_hash
2390-
.insert(*runtime_module_identifier, digest);
2410+
.chunk_graph
2411+
.get_chunk_runtime_modules_iterable(&runtime_chunk_ukey)
2412+
.for_each(|runtime_module_identifier| {
2413+
let s = unsafe { token.used((&self, runtime_module_identifier)) };
2414+
s.spawn(|(compilation, runtime_module_identifier)| async {
2415+
let runtime_module = &compilation.runtime_modules[runtime_module_identifier];
2416+
let digest = runtime_module.get_runtime_hash(compilation, None).await?;
2417+
Ok((*runtime_module_identifier, digest))
2418+
});
2419+
})
2420+
})
2421+
.await
2422+
.into_iter()
2423+
.map(|res| res.to_rspack_result())
2424+
.collect::<Result<Vec<_>>>()?;
2425+
2426+
for res in runtime_module_hashes {
2427+
let (mid, digest) = res?;
2428+
self.runtime_modules_hash.insert(mid, digest);
23912429
}
2430+
23922431
let chunk_hash_result = self
23932432
.process_chunk_hash(runtime_chunk_ukey, &plugin_driver)
23942433
.await?;

0 commit comments

Comments
 (0)