diff --git a/Cargo.lock b/Cargo.lock index 984db80c7b..b040ce6d09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10611,6 +10611,7 @@ dependencies = [ "proptest-arbitrary-interop", "rand 0.8.5", "rand 0.9.1", + "rayon", "reth-execution-errors", "reth-primitives-traits", "reth-trie", diff --git a/crates/trie/sparse-parallel/Cargo.toml b/crates/trie/sparse-parallel/Cargo.toml index 21764ff429..039f6d82a5 100644 --- a/crates/trie/sparse-parallel/Cargo.toml +++ b/crates/trie/sparse-parallel/Cargo.toml @@ -25,6 +25,7 @@ alloy-rlp.workspace = true # misc smallvec.workspace = true +rayon = { workspace = true, optional = true } [dev-dependencies] # reth @@ -33,6 +34,7 @@ reth-trie-common = { workspace = true, features = ["test-utils", "arbitrary"] } reth-trie.workspace = true reth-trie-sparse = { workspace = true, features = ["test-utils"] } +# misc arbitrary.workspace = true assert_matches.workspace = true itertools.workspace = true @@ -40,3 +42,17 @@ proptest-arbitrary-interop.workspace = true proptest.workspace = true rand.workspace = true rand_08.workspace = true + +[features] +default = ["std"] +std = [ + "dep:rayon", + "alloy-primitives/std", + "alloy-rlp/std", + "alloy-trie/std", + "reth-execution-errors/std", + "reth-primitives-traits/std", + "reth-trie-common/std", + "reth-trie-sparse/std", + "tracing/std", +] diff --git a/crates/trie/sparse-parallel/src/trie.rs b/crates/trie/sparse-parallel/src/trie.rs index 96fccea84b..e4951b9550 100644 --- a/crates/trie/sparse-parallel/src/trie.rs +++ b/crates/trie/sparse-parallel/src/trie.rs @@ -769,13 +769,28 @@ impl ParallelSparseTrie { // Update the prefix set with the keys that didn't have matching subtries self.prefix_set = unchanged_prefix_set; - // Update subtrie hashes in parallel - // TODO: call `update_hashes` on each subtrie in parallel let (tx, rx) = mpsc::channel(); + + #[cfg(not(feature = "std"))] + // Update subtrie hashes serially if nostd for ChangedSubtrie { index, mut subtrie, mut prefix_set } in subtries { subtrie.update_hashes(&mut prefix_set); tx.send((index, subtrie)).unwrap(); } + + #[cfg(feature = "std")] + // Update subtrie hashes in parallel + { + use rayon::iter::{IntoParallelIterator, ParallelIterator}; + subtries + .into_par_iter() + .map(|ChangedSubtrie { index, mut subtrie, mut prefix_set }| { + subtrie.update_hashes(&mut prefix_set); + (index, subtrie) + }) + .for_each_init(|| tx.clone(), |tx, result| tx.send(result).unwrap()); + } + drop(tx); // Return updated subtries back to the trie