Skip to content

Commit

Permalink
fix(root): spawn state root task only if host has enough parallelism (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin authored Feb 18, 2025
1 parent 5fe28fd commit 42f8223
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2431,8 +2431,11 @@ where
// Atomic bool for letting the prewarm tasks know when to stop
let cancel_execution = ManualCancel::default();

let use_legacy_state_root =
self.config.legacy_state_root() || !root::has_enough_parallelism();

let (state_root_handle, state_root_task_config, state_root_sender, state_hook) =
if is_descendant_of_persisting_blocks && !self.config.legacy_state_root() {
if is_descendant_of_persisting_blocks && !use_legacy_state_root {
let consistent_view = ConsistentDbView::new_with_latest_tip(self.provider.clone())?;

// Compute trie input
Expand Down Expand Up @@ -2557,7 +2560,7 @@ where
// a different database transaction per thread and it might end up with a
// different view of the database.
let (state_root, trie_output, root_elapsed) = if is_descendant_of_persisting_blocks {
if self.config.legacy_state_root() {
if use_legacy_state_root {
match self.compute_state_root_parallel(block.header().parent_hash(), &hashed_state)
{
Ok(result) => {
Expand Down
12 changes: 12 additions & 0 deletions crates/engine/tree/src/tree/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ pub(crate) fn thread_pool_size() -> usize {
std::thread::available_parallelism().map_or(3, |num| (num.get() / 2).max(3))
}

/// Determines if the host has enough parallelism to run the state root task.
///
/// It requires at least 5 parallel threads:
/// - Engine in main thread that spawns the state root task.
/// - State Root Task spawned in [`StateRootTask::spawn`]
/// - Sparse Trie spawned in [`run_sparse_trie`]
/// - Multiproof computation spawned in [`MultiproofManager::spawn_multiproof`]
/// - Storage root computation spawned in [`ParallelProof::multiproof`]
pub(crate) fn has_enough_parallelism() -> bool {
std::thread::available_parallelism().is_ok_and(|num| num.get() >= 5)
}

/// Outcome of the state root computation, including the state root itself with
/// the trie updates and the total time spent.
#[derive(Debug)]
Expand Down

0 comments on commit 42f8223

Please sign in to comment.