Skip to content

Commit

Permalink
more Arc
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimenez committed Jan 20, 2025
1 parent a10ea22 commit f33988f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
2 changes: 1 addition & 1 deletion crates/trie/sparse/src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ mod tests {
walker,
HashedPostStateAccountCursor::new(
NoopHashedAccountCursor::default(),
hashed_post_state.accounts().clone(),
hashed_post_state.accounts(),
),
);

Expand Down
12 changes: 6 additions & 6 deletions crates/trie/trie/src/hashed_cursor/post_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct HashedPostStateAccountCursor<C> {
/// Forward-only in-memory cursor over accounts.
post_state_cursor: ForwardInMemoryCursor<B256, Account>,
/// Reference to the collection of account keys that were destroyed.
destroyed_accounts: B256HashSet,
destroyed_accounts: Arc<B256HashSet>,
/// The last hashed account that was returned by the cursor.
/// De facto, this is a current cursor position.
last_account: Option<B256>,
Expand All @@ -65,9 +65,9 @@ where
C: HashedCursor<Value = Account>,
{
/// 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<HashedAccountsSorted>) -> 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 }
}

Expand Down Expand Up @@ -188,7 +188,7 @@ pub struct HashedPostStateStorageCursor<C> {
/// Forward-only in-memory cursor over non zero-valued account storage slots.
post_state_cursor: Option<ForwardInMemoryCursor<B256, U256>>,
/// Reference to the collection of storage slot keys that were cleared.
cleared_slots: Option<B256HashSet>,
cleared_slots: Option<Arc<B256HashSet>>,
/// Flag indicating whether database storage was wiped.
storage_wiped: bool,
/// The last slot that has been returned by the cursor.
Expand All @@ -204,7 +204,7 @@ where
pub fn new(cursor: C, post_state_storage: Option<Arc<HashedStorageSorted>>) -> 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 }
Expand Down
47 changes: 31 additions & 16 deletions crates/trie/trie/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -192,15 +192,15 @@ 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
.into_iter()
.map(|(hashed_address, storage)| (hashed_address, storage.into_sorted()))
.collect();

HashedPostStateSorted { accounts, storages }
HashedPostStateSorted::new(accounts, storages)
}
}

Expand Down Expand Up @@ -271,31 +271,28 @@ 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)
}
}

/// Sorted hashed post state optimized for iterating during state trie calculation.
#[derive(PartialEq, Eq, Clone, Default, Debug)]
pub struct HashedPostStateSorted {
/// Updated state of accounts.
pub(crate) accounts: HashedAccountsSorted,
pub(crate) accounts: Arc<HashedAccountsSorted>,
/// Map of hashed addresses to hashed storage.
pub(crate) storages: B256HashMap<HashedStorageSorted>,
}

impl HashedPostStateSorted {
/// Create new instance of [`HashedPostStateSorted`]
pub const fn new(
accounts: HashedAccountsSorted,
storages: B256HashMap<HashedStorageSorted>,
) -> Self {
Self { accounts, storages }
pub fn new(accounts: HashedAccountsSorted, storages: B256HashMap<HashedStorageSorted>) -> 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<HashedAccountsSorted> {
self.accounts.clone()
}

/// Returns reference to hashed account storages.
Expand All @@ -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<Vec<(B256, Account)>>,
/// Set of destroyed account keys.
pub(crate) destroyed_accounts: B256HashSet,
pub(crate) destroyed_accounts: Arc<B256HashSet>,
}

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<Item = (B256, Option<Account>)> {
self.accounts
Expand All @@ -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<Vec<(B256, U256)>>,
/// Slots that have been zero valued.
pub(crate) zero_valued_slots: B256HashSet,
pub(crate) zero_valued_slots: Arc<B256HashSet>,
/// 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
Expand Down

0 comments on commit f33988f

Please sign in to comment.