pass &Pubkey through account storage, slot clean code to reduce copies (#16778)
* &Pubkey * use trait to pass &Hash or Hash * use impl Borrow<Hash> instead of trait * remove old code line commented out
This commit is contained in:
parent
3b8d6b59fb
commit
81402ee7f5
|
@ -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>,
|
||||
hash: Option<impl Borrow<Hash>>,
|
||||
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>,
|
||||
hash: Option<impl Borrow<Hash>>,
|
||||
) -> 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
|
||||
|
|
|
@ -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<F: FnMut(Slot, usize) -> Arc<AccountStorageEntry>>(
|
||||
&self,
|
||||
slot: Slot,
|
||||
hashes: &[Hash],
|
||||
hashes: &[impl Borrow<Hash>],
|
||||
mut storage_finder: F,
|
||||
accounts_and_meta_to_store: &[(StoredMeta, &AccountSharedData)],
|
||||
) -> Vec<AccountInfo> {
|
||||
|
@ -3691,7 +3691,7 @@ impl AccountsDb {
|
|||
fn write_accounts_to_cache(
|
||||
&self,
|
||||
slot: Slot,
|
||||
hashes: Option<&[Hash]>,
|
||||
hashes: Option<&[impl Borrow<Hash>]>,
|
||||
accounts_and_meta_to_store: &[(StoredMeta, &AccountSharedData)],
|
||||
) -> Vec<AccountInfo> {
|
||||
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<Hash>]>,
|
||||
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<Hash>]>,
|
||||
storage_finder: Option<StorageFinder<'a>>,
|
||||
write_version_producer: Option<Box<dyn Iterator<Item = u64>>>,
|
||||
) -> StoreAccountsTiming {
|
||||
|
@ -4724,7 +4724,7 @@ impl AccountsDb {
|
|||
&'a self,
|
||||
slot: Slot,
|
||||
accounts: &[(&Pubkey, &AccountSharedData)],
|
||||
hashes: Option<&[Hash]>,
|
||||
hashes: Option<&[impl Borrow<Hash>]>,
|
||||
storage_finder: Option<StorageFinder<'a>>,
|
||||
write_version_producer: Option<Box<dyn Iterator<Item = u64>>>,
|
||||
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),
|
||||
|
|
|
@ -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<Hash>],
|
||||
) -> Vec<usize> {
|
||||
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::<StoredMeta>()),
|
||||
(account_meta_ptr as *const u8, mem::size_of::<AccountMeta>()),
|
||||
|
@ -494,7 +495,7 @@ impl AppendVec {
|
|||
account: &AccountSharedData,
|
||||
hash: Hash,
|
||||
) -> Option<usize> {
|
||||
let res = self.append_accounts(&[(storage_meta, account)], &[hash]);
|
||||
let res = self.append_accounts(&[(storage_meta, account)], &[&hash]);
|
||||
if res.len() == 1 {
|
||||
None
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue