-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
perf(engine): add StateRootTask bench #13212
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
//! Benchmark for `StateRootTask` complete workflow, including sending state | ||
//! updates using the incoming messages sender and waiting for the final result. | ||
|
||
#![allow(missing_docs)] | ||
|
||
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
use reth_engine_tree::tree::root::{StateRootConfig, StateRootTask}; | ||
use reth_evm::system_calls::OnStateHook; | ||
use reth_primitives::{Account as RethAccount, StorageEntry}; | ||
use reth_provider::{ | ||
providers::ConsistentDbView, | ||
test_utils::{create_test_provider_factory, MockNodeTypesWithDB}, | ||
HashingWriter, ProviderFactory, | ||
}; | ||
use reth_testing_utils::generators::{self, Rng}; | ||
use reth_trie::TrieInput; | ||
use revm_primitives::{ | ||
Account as RevmAccount, AccountInfo, AccountStatus, Address, EvmState, EvmStorageSlot, HashMap, | ||
B256, KECCAK_EMPTY, U256, | ||
}; | ||
use std::sync::Arc; | ||
|
||
#[derive(Debug, Clone)] | ||
struct BenchParams { | ||
num_accounts: usize, | ||
updates_per_account: usize, | ||
storage_slots_per_account: usize, | ||
} | ||
|
||
fn create_bench_state_updates(params: &BenchParams) -> Vec<EvmState> { | ||
let mut rng = generators::rng(); | ||
let all_addresses: Vec<Address> = (0..params.num_accounts).map(|_| rng.gen()).collect(); | ||
let mut updates = Vec::new(); | ||
|
||
for _ in 0..params.updates_per_account { | ||
let num_accounts_in_update = rng.gen_range(1..=params.num_accounts); | ||
let mut state_update = EvmState::default(); | ||
|
||
let selected_addresses = &all_addresses[0..num_accounts_in_update]; | ||
|
||
for &address in selected_addresses { | ||
let mut storage = HashMap::default(); | ||
for _ in 0..params.storage_slots_per_account { | ||
let slot = U256::from(rng.gen::<u64>()); | ||
storage.insert( | ||
slot, | ||
EvmStorageSlot::new_changed(U256::ZERO, U256::from(rng.gen::<u64>())), | ||
); | ||
} | ||
|
||
let account = RevmAccount { | ||
info: AccountInfo { | ||
balance: U256::from(rng.gen::<u64>()), | ||
nonce: rng.gen::<u64>(), | ||
code_hash: KECCAK_EMPTY, | ||
code: Some(Default::default()), | ||
}, | ||
storage, | ||
status: AccountStatus::Touched, | ||
}; | ||
|
||
state_update.insert(address, account); | ||
} | ||
|
||
updates.push(state_update); | ||
} | ||
|
||
updates | ||
} | ||
|
||
fn convert_revm_to_reth_account(revm_account: &RevmAccount) -> RethAccount { | ||
RethAccount { | ||
balance: revm_account.info.balance, | ||
nonce: revm_account.info.nonce, | ||
bytecode_hash: if revm_account.info.code_hash == KECCAK_EMPTY { | ||
None | ||
} else { | ||
Some(revm_account.info.code_hash) | ||
}, | ||
} | ||
} | ||
|
||
fn setup_provider( | ||
factory: &ProviderFactory<MockNodeTypesWithDB>, | ||
state_updates: &[EvmState], | ||
) -> Result<(), Box<dyn std::error::Error>> { | ||
let provider_rw = factory.provider_rw()?; | ||
|
||
for update in state_updates { | ||
let account_updates = update | ||
.iter() | ||
.map(|(address, account)| (*address, Some(convert_revm_to_reth_account(account)))); | ||
provider_rw.insert_account_for_hashing(account_updates)?; | ||
|
||
let storage_updates = update.iter().map(|(address, account)| { | ||
let storage_entries = account.storage.iter().map(|(slot, value)| StorageEntry { | ||
key: B256::from(*slot), | ||
value: value.present_value, | ||
}); | ||
(*address, storage_entries) | ||
}); | ||
provider_rw.insert_storage_for_hashing(storage_updates)?; | ||
} | ||
|
||
provider_rw.commit()?; | ||
Ok(()) | ||
} | ||
|
||
fn bench_state_root(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("state_root"); | ||
|
||
let scenarios = vec![ | ||
BenchParams { num_accounts: 100, updates_per_account: 5, storage_slots_per_account: 10 }, | ||
BenchParams { num_accounts: 1000, updates_per_account: 10, storage_slots_per_account: 20 }, | ||
]; | ||
|
||
for params in scenarios { | ||
group.bench_with_input( | ||
BenchmarkId::new( | ||
"state_root_task", | ||
format!( | ||
"accounts_{}_updates_{}_slots_{}", | ||
params.num_accounts, | ||
params.updates_per_account, | ||
params.storage_slots_per_account | ||
), | ||
), | ||
¶ms, | ||
|b, params| { | ||
b.iter_with_setup( | ||
|| { | ||
let factory = create_test_provider_factory(); | ||
let state_updates = create_bench_state_updates(params); | ||
setup_provider(&factory, &state_updates).expect("failed to setup provider"); | ||
|
||
let trie_input = Arc::new(TrieInput::from_state(Default::default())); | ||
|
||
let config = StateRootConfig { | ||
consistent_view: ConsistentDbView::new(factory, None), | ||
input: trie_input, | ||
}; | ||
|
||
(config, state_updates) | ||
}, | ||
|(config, state_updates)| { | ||
let task = StateRootTask::new(config); | ||
let mut hook = task.state_hook(); | ||
let handle = task.spawn(); | ||
|
||
for update in state_updates { | ||
hook.on_state(&update) | ||
} | ||
drop(hook); | ||
|
||
black_box(handle.wait_for_result().expect("task failed")); | ||
}, | ||
) | ||
}, | ||
); | ||
} | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group!(benches, bench_state_root); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be nice to extend it to selfdestructs in the future as well, so that we can provide the number of deleted accounts in params
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sg, will prepare a follow-up