Skip to content

Commit

Permalink
perf: avoid cloning bytecode when converting revm's &AccountInfo to…
Browse files Browse the repository at this point in the history
… reth's `Account` (#13126)
  • Loading branch information
hai-rise authored Dec 4, 2024
1 parent 8d1a332 commit 53243a2
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/evm/execution-types/src/execution_outcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl<T> ExecutionOutcome<T> {

/// Get account if account is known.
pub fn account(&self, address: &Address) -> Option<Option<Account>> {
self.bundle.account(address).map(|a| a.info.clone().map(Into::into))
self.bundle.account(address).map(|a| a.info.as_ref().map(Into::into))
}

/// Get storage if value is known.
Expand Down
13 changes: 11 additions & 2 deletions crates/primitives-traits/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,20 @@ impl From<&GenesisAccount> for Account {

impl From<AccountInfo> for Account {
fn from(revm_acc: AccountInfo) -> Self {
let code_hash = revm_acc.code_hash;
Self {
balance: revm_acc.balance,
nonce: revm_acc.nonce,
bytecode_hash: (code_hash != KECCAK_EMPTY).then_some(code_hash),
bytecode_hash: (!revm_acc.is_empty_code_hash()).then_some(revm_acc.code_hash),
}
}
}

impl From<&AccountInfo> for Account {
fn from(revm_acc: &AccountInfo) -> Self {
Self {
balance: revm_acc.balance,
nonce: revm_acc.nonce,
bytecode_hash: (!revm_acc.is_empty_code_hash()).then_some(revm_acc.code_hash),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/revm/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl ExecutionWitnessRecord {
let hashed_address = keccak256(address);
self.hashed_state
.accounts
.insert(hashed_address, account.account.as_ref().map(|a| a.info.clone().into()));
.insert(hashed_address, account.account.as_ref().map(|a| (&a.info).into()));

let storage = self
.hashed_state
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/provider/src/test_utils/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn bundle_state_root(execution_outcome: &ExecutionOutcome) -> B256 {
(
address,
(
Into::<Account>::into(info.clone()),
Into::<Account>::into(info),
storage_root_unhashed(
account
.storage
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/provider/src/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ mod tests {

let reth_account_a = account_a.into();
let reth_account_b = account_b.into();
let reth_account_b_changed = account_b_changed.clone().into();
let reth_account_b_changed = (&account_b_changed).into();

// Check plain state
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion crates/trie/trie/benches/hash_post_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn from_bundle_state_seq(state: &HashMap<Address, BundleAccount>) -> HashedPostS

for (address, account) in state {
let hashed_address = keccak256(address);
this.accounts.insert(hashed_address, account.info.clone().map(Into::into));
this.accounts.insert(hashed_address, account.info.as_ref().map(Into::into));

let hashed_storage = HashedStorage::from_iter(
account.status.was_destroyed(),
Expand Down
4 changes: 2 additions & 2 deletions crates/trie/trie/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl HashedPostState {
.into_par_iter()
.map(|(address, account)| {
let hashed_address = KH::hash_key(address);
let hashed_account = account.info.clone().map(Into::into);
let hashed_account = account.info.as_ref().map(Into::into);
let hashed_storage = HashedStorage::from_plain_storage(
account.status,
account.storage.iter().map(|(slot, value)| (slot, &value.present_value)),
Expand All @@ -61,7 +61,7 @@ impl HashedPostState {
.into_par_iter()
.map(|(address, account)| {
let hashed_address = KH::hash_key(address);
let hashed_account = account.account.as_ref().map(|a| a.info.clone().into());
let hashed_account = account.account.as_ref().map(|a| (&a.info).into());
let hashed_storage = HashedStorage::from_plain_storage(
account.status,
account.account.as_ref().map(|a| a.storage.iter()).into_iter().flatten(),
Expand Down

0 comments on commit 53243a2

Please sign in to comment.