Skip to content

Commit 1d42a84

Browse files
committed
perf: parallel get_runtime_hash
1 parent 9cb7d7f commit 1d42a84

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

crates/rspack_core/src/concatenated_module.rs

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,9 @@ impl Module for ConcatenatedModule {
12951295
compilation: &Compilation,
12961296
generation_runtime: Option<&RuntimeSpec>,
12971297
) -> Result<RspackHashDigest> {
1298+
use rayon::prelude::*;
1299+
use tokio::runtime::Handle;
1300+
12981301
let mut hasher = RspackHash::from(&compilation.options.output);
12991302
let runtime = if let Some(self_runtime) = &self.runtime
13001303
&& let Some(generation_runtime) = generation_runtime
@@ -1309,32 +1312,47 @@ impl Module for ConcatenatedModule {
13091312
generation_runtime.map(Cow::Borrowed)
13101313
};
13111314
let runtime = runtime.as_deref();
1312-
for info in self.create_concatenation_list(
1315+
let list = self.create_concatenation_list(
13131316
self.root_module_ctxt.id,
13141317
self.modules.iter().map(|item| item.id).collect(),
13151318
runtime,
13161319
&compilation.get_module_graph(),
1317-
) {
1318-
match info {
1319-
ConcatenationEntry::Concatenated(e) => {
1320-
compilation
1321-
.get_module_graph()
1322-
.module_by_identifier(&e.module)
1323-
.expect("should have module")
1324-
.get_runtime_hash(compilation, generation_runtime)
1325-
.await?
1326-
.encoded()
1327-
.dyn_hash(&mut hasher);
1328-
}
1329-
ConcatenationEntry::External(e) => {
1330-
ChunkGraph::get_module_id(
1331-
&compilation.module_ids_artifact,
1332-
e.module(&compilation.get_module_graph()),
1333-
)
1334-
.dyn_hash(&mut hasher);
1335-
}
1336-
};
1337-
}
1320+
);
1321+
let handle = Handle::current();
1322+
1323+
let to_hash = |info: &_| match info {
1324+
ConcatenationEntry::Concatenated(e) => {
1325+
let mg = compilation.get_module_graph();
1326+
let fut = mg
1327+
.module_by_identifier(&e.module)
1328+
.expect("should have module")
1329+
.get_runtime_hash(compilation, generation_runtime);
1330+
let result = tokio::task::block_in_place(|| handle.block_on(fut));
1331+
result.map(|digest| Box::new(digest) as Box<dyn DynHash + Send>)
1332+
}
1333+
ConcatenationEntry::External(e) => {
1334+
let maybe_module_id = ChunkGraph::get_module_id(
1335+
&compilation.module_ids_artifact,
1336+
e.module(&compilation.get_module_graph()),
1337+
)
1338+
.cloned();
1339+
Ok(Box::new(maybe_module_id) as Box<_>)
1340+
}
1341+
};
1342+
1343+
if list.len() > 2 {
1344+
let list = list.par_iter().map(to_hash).collect::<Result<Vec<_>>>()?;
1345+
1346+
for digest in list {
1347+
digest.dyn_hash(&mut hasher);
1348+
}
1349+
} else {
1350+
for digest in list.iter().map(to_hash) {
1351+
let digest = digest?;
1352+
digest.dyn_hash(&mut hasher);
1353+
}
1354+
};
1355+
13381356
module_update_hash(self, &mut hasher, compilation, generation_runtime);
13391357
Ok(hasher.digest(&compilation.options.output.hash_digest))
13401358
}

0 commit comments

Comments
 (0)