Skip to content

Commit

Permalink
feat(tree): increase state root task thread pool size (#14455)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin authored Feb 19, 2025
1 parent 36851cc commit 465af6e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ where
) -> Self {
let (incoming_tx, incoming) = std::sync::mpsc::channel();

let num_threads = root::thread_pool_size();
let num_threads = root::rayon_thread_pool_size();

let thread_pool = Arc::new(
rayon::ThreadPoolBuilder::new()
Expand Down
23 changes: 15 additions & 8 deletions crates/engine/tree/src/tree/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,21 @@ use tracing::{debug, error, trace, trace_span};
/// The level below which the sparse trie hashes are calculated in [`update_sparse_trie`].
const SPARSE_TRIE_INCREMENTAL_LEVEL: usize = 2;

/// Determines the size of the thread pool to be used in [`StateRootTask`].
/// It should be at least three, one for multiproof calculations plus two to be
/// used internally in [`StateRootTask`].
/// Determines the size of the rayon thread pool to be used in [`StateRootTask`].
///
/// The value is determined as `max(NUM_THREADS - 2, 3)`:
/// - It should leave at least 2 threads to the rest of the system to be used in:
/// - Engine
/// - State Root Task spawned in [`StateRootTask::spawn`]
/// - It should heave at least 3 threads to be used in:
/// - Sparse Trie spawned in [`run_sparse_trie`]
/// - Multiproof computation spawned in [`MultiproofManager::spawn_multiproof`]
/// - Storage root computation spawned in [`ParallelProof::multiproof`]
///
/// NOTE: this value can be greater than the available cores in the host, it
/// represents the maximum number of threads that can be handled by the pool.
pub(crate) fn thread_pool_size() -> usize {
std::thread::available_parallelism().map_or(3, |num| (num.get() / 2).max(3))
pub(crate) fn rayon_thread_pool_size() -> usize {
std::thread::available_parallelism().map_or(3, |num| (num.get().saturating_sub(2).max(3)))
}

/// Determines if the host has enough parallelism to run the state root task.
Expand Down Expand Up @@ -535,7 +542,7 @@ where
fetched_proof_targets: Default::default(),
proof_sequencer: ProofSequencer::new(),
thread_pool: thread_pool.clone(),
multiproof_manager: MultiproofManager::new(thread_pool, thread_pool_size()),
multiproof_manager: MultiproofManager::new(thread_pool, rayon_thread_pool_size()),
metrics: StateRootTaskMetrics::default(),
}
}
Expand Down Expand Up @@ -1286,7 +1293,7 @@ mod tests {
+ Clone
+ 'static,
{
let num_threads = thread_pool_size();
let num_threads = rayon_thread_pool_size();

let thread_pool = rayon::ThreadPoolBuilder::new()
.num_threads(num_threads)
Expand Down Expand Up @@ -1361,7 +1368,7 @@ mod tests {
prefix_sets: Arc::new(input.prefix_sets),
};

let num_threads = thread_pool_size();
let num_threads = rayon_thread_pool_size();

let state_root_task_pool = rayon::ThreadPoolBuilder::new()
.num_threads(num_threads)
Expand Down

0 comments on commit 465af6e

Please sign in to comment.