From f33988fadd944d335eb8a893b7d064eddbfa6579 Mon Sep 17 00:00:00 2001 From: Federico Gimenez Date: Mon, 20 Jan 2025 12:58:33 +0100 Subject: [PATCH] more Arc --- crates/trie/sparse/src/trie.rs | 2 +- .../trie/trie/src/hashed_cursor/post_state.rs | 12 ++--- crates/trie/trie/src/state.rs | 47 ++++++++++++------- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/crates/trie/sparse/src/trie.rs b/crates/trie/sparse/src/trie.rs index fe619bc7acc2..4a4f0f185ceb 100644 --- a/crates/trie/sparse/src/trie.rs +++ b/crates/trie/sparse/src/trie.rs @@ -1470,7 +1470,7 @@ mod tests { walker, HashedPostStateAccountCursor::new( NoopHashedAccountCursor::default(), - hashed_post_state.accounts().clone(), + hashed_post_state.accounts(), ), ); diff --git a/crates/trie/trie/src/hashed_cursor/post_state.rs b/crates/trie/trie/src/hashed_cursor/post_state.rs index 658204ce9870..65c2373267a8 100644 --- a/crates/trie/trie/src/hashed_cursor/post_state.rs +++ b/crates/trie/trie/src/hashed_cursor/post_state.rs @@ -54,7 +54,7 @@ pub struct HashedPostStateAccountCursor { /// Forward-only in-memory cursor over accounts. post_state_cursor: ForwardInMemoryCursor, /// Reference to the collection of account keys that were destroyed. - destroyed_accounts: B256HashSet, + destroyed_accounts: Arc, /// The last hashed account that was returned by the cursor. /// De facto, this is a current cursor position. last_account: Option, @@ -65,9 +65,9 @@ where C: HashedCursor, { /// Create new instance of [`HashedPostStateAccountCursor`]. - pub fn new(cursor: C, post_state_accounts: HashedAccountsSorted) -> Self { - let post_state_cursor = ForwardInMemoryCursor::new(Arc::new(post_state_accounts.accounts)); - let destroyed_accounts = post_state_accounts.destroyed_accounts; + pub fn new(cursor: C, post_state_accounts: Arc) -> Self { + let post_state_cursor = ForwardInMemoryCursor::new(post_state_accounts.accounts.clone()); + let destroyed_accounts = post_state_accounts.destroyed_accounts.clone(); Self { cursor, post_state_cursor, destroyed_accounts, last_account: None } } @@ -188,7 +188,7 @@ pub struct HashedPostStateStorageCursor { /// Forward-only in-memory cursor over non zero-valued account storage slots. post_state_cursor: Option>, /// Reference to the collection of storage slot keys that were cleared. - cleared_slots: Option, + cleared_slots: Option>, /// Flag indicating whether database storage was wiped. storage_wiped: bool, /// The last slot that has been returned by the cursor. @@ -204,7 +204,7 @@ where pub fn new(cursor: C, post_state_storage: Option>) -> Self { let post_state_cursor = post_state_storage .as_ref() - .map(|s| ForwardInMemoryCursor::new(Arc::new(s.non_zero_valued_slots.clone()))); + .map(|s| ForwardInMemoryCursor::new(s.non_zero_valued_slots.clone())); let cleared_slots = post_state_storage.as_ref().map(|s| s.zero_valued_slots.clone()); let storage_wiped = post_state_storage.is_some_and(|s| s.wiped); Self { cursor, post_state_cursor, cleared_slots, storage_wiped, last_slot: None } diff --git a/crates/trie/trie/src/state.rs b/crates/trie/trie/src/state.rs index 510b914ceaf9..2121362432ae 100644 --- a/crates/trie/trie/src/state.rs +++ b/crates/trie/trie/src/state.rs @@ -12,7 +12,7 @@ use rayon::prelude::{IntoParallelIterator, ParallelIterator}; use reth_primitives::Account; use reth_trie_common::KeyHasher; use revm::db::{states::CacheAccount, AccountStatus, BundleAccount}; -use std::borrow::Cow; +use std::{borrow::Cow, sync::Arc}; /// Representation of in-memory hashed state. #[derive(PartialEq, Eq, Clone, Default, Debug)] @@ -192,7 +192,7 @@ impl HashedPostState { } } updated_accounts.sort_unstable_by_key(|(address, _)| *address); - let accounts = HashedAccountsSorted { accounts: updated_accounts, destroyed_accounts }; + let accounts = HashedAccountsSorted::new(updated_accounts, destroyed_accounts); let storages = self .storages @@ -200,7 +200,7 @@ impl HashedPostState { .map(|(hashed_address, storage)| (hashed_address, storage.into_sorted())) .collect(); - HashedPostStateSorted { accounts, storages } + HashedPostStateSorted::new(accounts, storages) } } @@ -271,7 +271,7 @@ impl HashedStorage { } non_zero_valued_slots.sort_unstable_by_key(|(key, _)| *key); - HashedStorageSorted { non_zero_valued_slots, zero_valued_slots, wiped: self.wiped } + HashedStorageSorted::new(non_zero_valued_slots, zero_valued_slots, self.wiped) } } @@ -279,23 +279,20 @@ impl HashedStorage { #[derive(PartialEq, Eq, Clone, Default, Debug)] pub struct HashedPostStateSorted { /// Updated state of accounts. - pub(crate) accounts: HashedAccountsSorted, + pub(crate) accounts: Arc, /// Map of hashed addresses to hashed storage. pub(crate) storages: B256HashMap, } impl HashedPostStateSorted { /// Create new instance of [`HashedPostStateSorted`] - pub const fn new( - accounts: HashedAccountsSorted, - storages: B256HashMap, - ) -> Self { - Self { accounts, storages } + pub fn new(accounts: HashedAccountsSorted, storages: B256HashMap) -> Self { + Self { accounts: Arc::new(accounts), storages } } /// Returns reference to hashed accounts. - pub const fn accounts(&self) -> &HashedAccountsSorted { - &self.accounts + pub fn accounts(&self) -> Arc { + self.accounts.clone() } /// Returns reference to hashed account storages. @@ -308,12 +305,17 @@ impl HashedPostStateSorted { #[derive(Clone, Eq, PartialEq, Default, Debug)] pub struct HashedAccountsSorted { /// Sorted collection of hashed addresses and their account info. - pub(crate) accounts: Vec<(B256, Account)>, + pub(crate) accounts: Arc>, /// Set of destroyed account keys. - pub(crate) destroyed_accounts: B256HashSet, + pub(crate) destroyed_accounts: Arc, } impl HashedAccountsSorted { + /// Create new instance of [`HashedAccountsSorted`] + pub fn new(accounts: Vec<(B256, Account)>, destroyed_accounts: B256HashSet) -> Self { + Self { accounts: Arc::new(accounts), destroyed_accounts: Arc::new(destroyed_accounts) } + } + /// Returns a sorted iterator over updated accounts. pub fn accounts_sorted(&self) -> impl Iterator)> { self.accounts @@ -328,14 +330,27 @@ impl HashedAccountsSorted { #[derive(Clone, Eq, PartialEq, Debug)] pub struct HashedStorageSorted { /// Sorted hashed storage slots with non-zero value. - pub(crate) non_zero_valued_slots: Vec<(B256, U256)>, + pub(crate) non_zero_valued_slots: Arc>, /// Slots that have been zero valued. - pub(crate) zero_valued_slots: B256HashSet, + pub(crate) zero_valued_slots: Arc, /// Flag indicating whether the storage was wiped or not. pub(crate) wiped: bool, } impl HashedStorageSorted { + /// Create new instance of [`HashedStorageSorted`] + pub fn new( + non_zero_valued_slots: Vec<(B256, U256)>, + zero_valued_slots: B256HashSet, + wiped: bool, + ) -> Self { + Self { + non_zero_valued_slots: Arc::new(non_zero_valued_slots), + zero_valued_slots: Arc::new(zero_valued_slots), + wiped, + } + } + /// Returns `true` if the account was wiped. pub const fn is_wiped(&self) -> bool { self.wiped