Skip to content

Commit

Permalink
chore(io): support STATE_PROVIDER_OPTS for tuning (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekomoto911 authored Jan 16, 2025
1 parent 4a95a05 commit 41aa4c2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
35 changes: 16 additions & 19 deletions crates/stages/stages/src/stages/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use reth_provider::{
writer::UnifiedStorageWriter,
BlockReader, DBProvider, HeaderProvider, LatestStateProviderRef, OriginalValuesKnown,
ProviderError, StateChangeWriter, StateProvider, StateProviderOptions, StateWriter,
StaticFileProviderFactory, StatsReader, TransactionVariant,
StaticFileProviderFactory, StatsReader, TransactionVariant, STATE_PROVIDER_OPTS,
};
use reth_prune_types::PruneModes;
use reth_revm::database::StateProviderDatabase;
Expand Down Expand Up @@ -346,29 +346,26 @@ where
None
};

let mut executor = if let Some(parallel_provider) =
self.executor_provider.try_into_parallel_provider()
{
let db: Arc<dyn StateProvider> = if let Some(factory) = factory {
Arc::new(
factory.latest(StateProviderOptions { parallel: NonZero::new(8).unwrap() })?,
let mut executor =
if let Some(parallel_provider) = self.executor_provider.try_into_parallel_provider() {
let db: Arc<dyn StateProvider> = if let Some(factory) = factory {
Arc::new(factory.latest(STATE_PROVIDER_OPTS.clone())?)
} else {
Arc::new(LatestStateProviderRef::new(
provider.tx_ref(),
provider.static_file_provider(),
))
};
EitherBatchExecutor::Parallel(
parallel_provider.batch_executor(StateProviderDatabase(db)),
)
} else {
Arc::new(LatestStateProviderRef::new(
let db = StateProviderDatabase(LatestStateProviderRef::new(
provider.tx_ref(),
provider.static_file_provider(),
))
));
EitherBatchExecutor::Sequential(self.executor_provider.batch_executor(db))
};
EitherBatchExecutor::Parallel(
parallel_provider.batch_executor(StateProviderDatabase(db)),
)
} else {
let db = StateProviderDatabase(LatestStateProviderRef::new(
provider.tx_ref(),
provider.static_file_provider(),
));
EitherBatchExecutor::Sequential(self.executor_provider.batch_executor(db))
};
executor.set_tip(max_block);
executor.set_prune_modes(prune_modes);

Expand Down
1 change: 1 addition & 0 deletions crates/storage/storage-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ alloy-eips.workspace = true
alloy-primitives.workspace = true

auto_impl.workspace = true
once_cell.workspace = true
12 changes: 12 additions & 0 deletions crates/storage/storage-api/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::{
use alloy_eips::{BlockId, BlockNumHash, BlockNumberOrTag};
use alloy_primitives::{Address, BlockHash, BlockNumber, StorageKey, StorageValue, B256, U256};
use auto_impl::auto_impl;
use once_cell::sync::Lazy;
use reth_execution_types::ExecutionOutcome;
use reth_primitives::{Bytecode, KECCAK_EMPTY};
use reth_storage_errors::provider::{ProviderError, ProviderResult};
Expand Down Expand Up @@ -97,6 +98,17 @@ pub struct StateProviderOptions {
pub parallel: NonZero<usize>,
}

/// General options for state providers.
pub static STATE_PROVIDER_OPTS: Lazy<StateProviderOptions> = Lazy::new(|| StateProviderOptions {
parallel: NonZero::new(
std::env::var("STATE_PROVIDER_OPTS_PARALLEL")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(8),
)
.unwrap(),
});

impl Default for StateProviderOptions {
fn default() -> Self {
Self { parallel: NonZero::new(1).unwrap() }
Expand Down

0 comments on commit 41aa4c2

Please sign in to comment.