From 81402ee7f53f0405529f112a49a57ca74ca52579 Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" <75863576+jeffwashington@users.noreply.github.com> Date: Tue, 27 Apr 2021 09:10:06 -0500 Subject: [PATCH] pass &Pubkey through account storage, slot clean code to reduce copies (#16778) * &Pubkey * use trait to pass &Hash or Hash * use impl Borrow instead of trait * remove old code line commented out --- runtime/src/accounts_cache.rs | 11 ++++++----- runtime/src/accounts_db.rs | 24 ++++++++++++------------ runtime/src/append_vec.rs | 7 ++++--- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/runtime/src/accounts_cache.rs b/runtime/src/accounts_cache.rs index c235116fb..08a4e7eb1 100644 --- a/runtime/src/accounts_cache.rs +++ b/runtime/src/accounts_cache.rs @@ -6,6 +6,7 @@ use solana_sdk::{ pubkey::Pubkey, }; use std::{ + borrow::Borrow, collections::BTreeSet, ops::Deref, sync::{ @@ -51,7 +52,7 @@ impl SlotCacheInner { &self, pubkey: &Pubkey, account: AccountSharedData, - hash: Option, + hash: Option>, slot: Slot, ) -> CachedAccount { if self.cache.contains_key(pubkey) { @@ -64,7 +65,7 @@ impl SlotCacheInner { } let item = Arc::new(CachedAccountInner { account, - hash: RwLock::new(hash), + hash: RwLock::new(hash.map(|h| *h.borrow())), slot, pubkey: *pubkey, }); @@ -169,7 +170,7 @@ impl AccountsCache { slot: Slot, pubkey: &Pubkey, account: AccountSharedData, - hash: Option, + hash: Option>, ) -> CachedAccount { let slot_cache = self.slot_cache(slot).unwrap_or_else(|| // DashMap entry.or_insert() returns a RefMut, essentially a write lock, @@ -283,7 +284,7 @@ pub mod tests { inserted_slot, &Pubkey::new_unique(), AccountSharedData::new(1, 0, &Pubkey::default()), - Some(Hash::default()), + Some(&Hash::default()), ); // If the cache is told the size limit is 0, it should return the one slot let removed = cache.remove_slots_le(0); @@ -301,7 +302,7 @@ pub mod tests { inserted_slot, &Pubkey::new_unique(), AccountSharedData::new(1, 0, &Pubkey::default()), - Some(Hash::default()), + Some(&Hash::default()), ); // If the cache is told the size limit is 0, it should return nothing because there's only diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index be96402e0..5e05ab9a9 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -51,7 +51,7 @@ use solana_sdk::{ }; use solana_vote_program::vote_state::MAX_LOCKOUT_HISTORY; use std::{ - borrow::Cow, + borrow::{Borrow, Cow}, boxed::Box, collections::{hash_map::Entry, BTreeMap, BTreeSet, HashMap, HashSet}, convert::TryFrom, @@ -1937,7 +1937,7 @@ impl AccountsDb { for (_pubkey, alive_account) in alive_accounts.iter() { stored_accounts.push(alive_account.account.clone_account()); - hashes.push(*alive_account.account.hash); + hashes.push(alive_account.account.hash); write_versions.push(alive_account.account.meta.write_version); } let accounts = alive_accounts @@ -3308,7 +3308,7 @@ impl AccountsDb { fn write_accounts_to_storage Arc>( &self, slot: Slot, - hashes: &[Hash], + hashes: &[impl Borrow], mut storage_finder: F, accounts_and_meta_to_store: &[(StoredMeta, &AccountSharedData)], ) -> Vec { @@ -3691,7 +3691,7 @@ impl AccountsDb { fn write_accounts_to_cache( &self, slot: Slot, - hashes: Option<&[Hash]>, + hashes: Option<&[impl Borrow]>, accounts_and_meta_to_store: &[(StoredMeta, &AccountSharedData)], ) -> Vec { let len = accounts_and_meta_to_store.len(); @@ -3704,7 +3704,7 @@ impl AccountsDb { .iter() .enumerate() .map(|(i, (meta, account))| { - let hash = hashes.map(|hashes| hashes[i]); + let hash = hashes.map(|hashes| hashes[i].borrow()); let cached_account = self.accounts_cache .store(slot, &meta.pubkey, (*account).clone(), hash); @@ -3733,7 +3733,7 @@ impl AccountsDb { &self, slot: Slot, accounts: &[(&Pubkey, &AccountSharedData)], - hashes: Option<&[Hash]>, + hashes: Option<&[impl Borrow]>, storage_finder: F, mut write_version_producer: P, is_cached_store: bool, @@ -4674,7 +4674,7 @@ impl AccountsDb { &self, slot: Slot, accounts: &[(&Pubkey, &AccountSharedData)], - hashes: Option<&[Hash]>, + hashes: Option<&[&Hash]>, is_cached_store: bool, ) { // This path comes from a store to a non-frozen slot. @@ -4700,7 +4700,7 @@ impl AccountsDb { &'a self, slot: Slot, accounts: &[(&Pubkey, &AccountSharedData)], - hashes: Option<&[Hash]>, + hashes: Option<&[impl Borrow]>, storage_finder: Option>, write_version_producer: Option>>, ) -> StoreAccountsTiming { @@ -4724,7 +4724,7 @@ impl AccountsDb { &'a self, slot: Slot, accounts: &[(&Pubkey, &AccountSharedData)], - hashes: Option<&[Hash]>, + hashes: Option<&[impl Borrow]>, storage_finder: Option>, write_version_producer: Option>>, is_cached_store: bool, @@ -5178,7 +5178,7 @@ impl AccountsDb { for (pubkey, alive_account) in alive_accounts { accounts.push((pubkey, &alive_account.account)); - hashes.push(alive_account.account_hash); + hashes.push(&alive_account.account_hash); write_versions.push(alive_account.write_version); } start.stop(); @@ -5809,7 +5809,7 @@ pub mod tests { }; storages[0][0] .accounts - .append_accounts(&[(sm, &acc)], &[Hash::default()]); + .append_accounts(&[(sm, &acc)], &[&Hash::default()]); let calls = AtomicU64::new(0); let result = AccountsDb::scan_account_storage_no_bank( @@ -7704,7 +7704,7 @@ pub mod tests { } // provide bogus account hashes let some_hash = Hash::new(&[0xca; HASH_BYTES]); - db.store_accounts_unfrozen(some_slot, accounts, Some(&[some_hash]), false); + db.store_accounts_unfrozen(some_slot, accounts, Some(&[&some_hash]), false); db.add_root(some_slot); assert_matches!( db.verify_bank_hash_and_lamports(some_slot, &ancestors, 1), diff --git a/runtime/src/append_vec.rs b/runtime/src/append_vec.rs index 1fd773a92..134238d2d 100644 --- a/runtime/src/append_vec.rs +++ b/runtime/src/append_vec.rs @@ -11,6 +11,7 @@ use solana_sdk::{ pubkey::Pubkey, }; use std::{ + borrow::Borrow, fs::{remove_file, OpenOptions}, io, io::{Seek, SeekFrom, Write}, @@ -453,7 +454,7 @@ impl AppendVec { pub fn append_accounts( &self, accounts: &[(StoredMeta, &AccountSharedData)], - hashes: &[Hash], + hashes: &[impl Borrow], ) -> Vec { let _lock = self.append_lock.lock().unwrap(); let mut offset = self.len(); @@ -464,7 +465,7 @@ impl AppendVec { let account_meta_ptr = &account_meta as *const AccountMeta; let data_len = stored_meta.data_len as usize; let data_ptr = account.data().as_ptr(); - let hash_ptr = hash.as_ref().as_ptr(); + let hash_ptr = hash.borrow().as_ref().as_ptr(); let ptrs = [ (meta_ptr as *const u8, mem::size_of::()), (account_meta_ptr as *const u8, mem::size_of::()), @@ -494,7 +495,7 @@ impl AppendVec { account: &AccountSharedData, hash: Hash, ) -> Option { - let res = self.append_accounts(&[(storage_meta, account)], &[hash]); + let res = self.append_accounts(&[(storage_meta, account)], &[&hash]); if res.len() == 1 { None } else {