Skip to content

Commit 2e87f4f

Browse files
committed
perf: parallel get_runtime_hash
1 parent 0e36d5b commit 2e87f4f

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

0 commit comments

Comments
 (0)