Skip to content

Commit 4163af0

Browse files
authored
perf: link in parallel (#11710)
1 parent 86d5c5b commit 4163af0

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

crates/linking/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ workspace = true
1515

1616
[dependencies]
1717
foundry-compilers = { workspace = true, features = ["full"] }
18-
semver.workspace = true
18+
1919
alloy-primitives = { workspace = true, features = ["rlp"] }
20+
21+
rayon.workspace = true
22+
semver.workspace = true
2023
thiserror.workspace = true

crates/linking/src/lib.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use foundry_compilers::{
1111
artifacts::{CompactContractBytecodeCow, Libraries},
1212
contracts::ArtifactContracts,
1313
};
14+
use rayon::prelude::*;
1415
use semver::Version;
1516
use std::{
1617
collections::{BTreeMap, BTreeSet},
@@ -174,7 +175,7 @@ impl<'a> Linker<'a> {
174175

175176
// Link and collect bytecodes for `libs_to_deploy`.
176177
let libs_to_deploy = libs_to_deploy
177-
.into_iter()
178+
.into_par_iter()
178179
.map(|(id, _)| {
179180
Ok(self.link(id, &libraries)?.get_bytecode_bytes().unwrap().into_owned())
180181
})
@@ -198,7 +199,7 @@ impl<'a> Linker<'a> {
198199
self.collect_dependencies(target, &mut needed_libraries)?;
199200

200201
let mut needed_libraries = needed_libraries
201-
.into_iter()
202+
.into_par_iter()
202203
.filter(|id| {
203204
// Filter out already provided libraries.
204205
let (file, name) = self.convert_artifact_id_to_lib_path(id);
@@ -233,9 +234,9 @@ impl<'a> Linker<'a> {
233234

234235
let (file, name) = self.convert_artifact_id_to_lib_path(id);
235236

236-
for (_, bytecode) in &mut needed_libraries {
237+
needed_libraries.par_iter_mut().for_each(|(_, bytecode)| {
237238
bytecode.to_mut().link(&file.to_string_lossy(), &name, address);
238-
}
239+
});
239240

240241
libraries.libs.entry(file).or_default().insert(name, address.to_checksum(None));
241242
}
@@ -295,14 +296,18 @@ impl<'a> Linker<'a> {
295296
&self,
296297
libraries: &Libraries,
297298
) -> Result<ArtifactContracts, LinkerError> {
298-
self.contracts.keys().map(|id| Ok((id.clone(), self.link(id, libraries)?))).collect()
299+
self.get_linked_artifacts_cow(libraries).map(ArtifactContracts::from_iter)
299300
}
300301

301302
pub fn get_linked_artifacts_cow(
302303
&self,
303304
libraries: &Libraries,
304305
) -> Result<ArtifactContracts<CompactContractBytecodeCow<'a>>, LinkerError> {
305-
self.contracts.keys().map(|id| Ok((id.clone(), self.link(id, libraries)?))).collect()
306+
self.contracts
307+
.par_iter()
308+
.map(|(id, _)| Ok((id.clone(), self.link(id, libraries)?)))
309+
.collect::<Result<_, _>>()
310+
.map(ArtifactContracts)
306311
}
307312
}
308313

0 commit comments

Comments
 (0)