feat(trie): ParallelSparseTrie: Compute lower subtrie hashes in parallel (#17173)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Brian Picciano
2025-07-03 12:03:34 +02:00
committed by GitHub
parent c6e6a54d5b
commit c2a2d7d449
3 changed files with 34 additions and 2 deletions

1
Cargo.lock generated
View File

@@ -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",

View File

@@ -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",
]

View File

@@ -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