Skip to content

perf(trie): parallel rlp node updates in sparse trie #13251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/engine/tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ reth-stages-api.workspace = true
reth-tasks.workspace = true
reth-trie-db.workspace = true
reth-trie-parallel.workspace = true
reth-trie-sparse.workspace = true
reth-trie-sparse = { workspace = true, features = ["rayon"] }
reth-trie.workspace = true

# alloy
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/tree/src/tree/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ where
trie.update_account(address, account.unwrap_or_default())?;
}

trie.calculate_below_level(SPARSE_TRIE_INCREMENTAL_LEVEL);
trie.calculate_below_level_par(SPARSE_TRIE_INCREMENTAL_LEVEL);
let elapsed = started_at.elapsed();

Ok(elapsed)
Expand Down
2 changes: 2 additions & 0 deletions crates/trie/sparse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ alloy-primitives.workspace = true
alloy-rlp.workspace = true

# misc
rayon = { workspace = true, optional = true }
smallvec = { workspace = true, features = ["const_new"] }
thiserror.workspace = true

Expand Down Expand Up @@ -58,6 +59,7 @@ arbitrary = [
"alloy-primitives/arbitrary",
"smallvec/arbitrary",
]
rayon = ["dep:rayon"]

[[bench]]
name = "root"
Expand Down
4 changes: 2 additions & 2 deletions crates/trie/sparse/src/blinded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use reth_trie_common::{Nibbles, TrieMask};
/// Factory for instantiating blinded node providers.
pub trait BlindedProviderFactory {
/// Type capable of fetching blinded account nodes.
type AccountNodeProvider: BlindedProvider;
type AccountNodeProvider: BlindedProvider + Send + Sync;
/// Type capable of fetching blinded storage nodes.
type StorageNodeProvider: BlindedProvider;
type StorageNodeProvider: BlindedProvider + Send + Sync;

/// Returns blinded account node provider.
fn account_node_provider(&self) -> Self::AccountNodeProvider;
Expand Down
19 changes: 13 additions & 6 deletions crates/trie/sparse/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,6 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> {
Ok(())
}

/// Calculates the hashes of the nodes below the provided level.
pub fn calculate_below_level(&mut self, level: usize) {
self.state.calculate_below_level(level);
}

/// Returns storage sparse trie root if the trie has been revealed.
pub fn storage_root(&mut self, account: B256) -> Option<B256> {
self.storages.get_mut(&account).and_then(|trie| trie.root())
Expand Down Expand Up @@ -475,7 +470,8 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> {
})
}
}
impl<F: BlindedProviderFactory> SparseStateTrie<F> {

impl<F: BlindedProviderFactory + Send + Sync> SparseStateTrie<F> {
/// Update the account leaf node.
pub fn update_account_leaf(
&mut self,
Expand Down Expand Up @@ -549,6 +545,17 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> {
storage_trie.remove_leaf(slot)?;
Ok(())
}

/// Calculates the hashes of the nodes below the provided level.
pub fn calculate_below_level(&mut self, level: usize) {
self.state.calculate_below_level(level);
}

/// Calculates the hashes of the nodes below the provided level in parallel.
#[cfg(feature = "rayon")]
pub fn calculate_below_level_par(&mut self, level: usize) {
self.state.calculate_below_level_par(level);
}
}

#[cfg(test)]
Expand Down
Loading
Loading