accounts index insert uses pubkey by value (#18460)

This commit is contained in:
Jeff Washington (jwash)
2021-07-07 15:35:35 -05:00
committed by GitHub
parent ccdf93e2b8
commit 49c4e54b28
2 changed files with 26 additions and 26 deletions

View File

@ -1402,7 +1402,7 @@ impl Default for AccountsDb {
} }
type GenerateIndexAccountsMap<'a> = type GenerateIndexAccountsMap<'a> =
HashMap<&'a Pubkey, (StoredMetaWriteVersion, AppendVecId, StoredAccountMeta<'a>)>; HashMap<Pubkey, (StoredMetaWriteVersion, AppendVecId, StoredAccountMeta<'a>)>;
impl AccountsDb { impl AccountsDb {
pub fn new(paths: Vec<PathBuf>, cluster_type: &ClusterType) -> Self { pub fn new(paths: Vec<PathBuf>, cluster_type: &ClusterType) -> Self {
@ -5845,7 +5845,7 @@ impl AccountsDb {
let accounts = storage.all_accounts(); let accounts = storage.all_accounts();
accounts.into_iter().for_each(|stored_account| { accounts.into_iter().for_each(|stored_account| {
let this_version = stored_account.meta.write_version; let this_version = stored_account.meta.write_version;
match accounts_map.entry(&stored_account.meta.pubkey) { match accounts_map.entry(stored_account.meta.pubkey) {
std::collections::hash_map::Entry::Vacant(entry) => { std::collections::hash_map::Entry::Vacant(entry) => {
entry.insert((this_version, storage.append_vec_id(), stored_account)); entry.insert((this_version, storage.append_vec_id(), stored_account));
} }
@ -5872,14 +5872,25 @@ impl AccountsDb {
return 0; return 0;
} }
let secondary = !self.account_indexes.is_empty();
let len = accounts_map.len(); let len = accounts_map.len();
let items = accounts_map let items = accounts_map
.iter() .into_iter()
.map(|(pubkey, (_, store_id, stored_account))| { .map(|(pubkey, (_, store_id, stored_account))| {
if secondary {
self.accounts_index.update_secondary_indexes(
&pubkey,
&stored_account.account_meta.owner,
stored_account.data,
&self.account_indexes,
);
}
( (
*pubkey, pubkey,
AccountInfo { AccountInfo {
store_id: *store_id, store_id,
offset: stored_account.offset, offset: stored_account.offset,
stored_size: stored_account.stored_size, stored_size: stored_account.stored_size,
lamports: stored_account.account_meta.lamports, lamports: stored_account.account_meta.lamports,
@ -5897,17 +5908,6 @@ impl AccountsDb {
if !dirty_pubkeys.is_empty() { if !dirty_pubkeys.is_empty() {
self.uncleaned_pubkeys.insert(*slot, dirty_pubkeys); self.uncleaned_pubkeys.insert(*slot, dirty_pubkeys);
} }
if !self.account_indexes.is_empty() {
for (pubkey, (_, _store_id, stored_account)) in accounts_map.iter() {
self.accounts_index.update_secondary_indexes(
pubkey,
&stored_account.account_meta.owner,
stored_account.data,
&self.account_indexes,
);
}
}
insert_us insert_us
} }

View File

@ -1376,11 +1376,11 @@ impl<T: 'static + Clone + IsCached + ZeroLamport + std::marker::Sync + std::mark
// But, does NOT update secondary index // But, does NOT update secondary index
// This is designed to be called at startup time. // This is designed to be called at startup time.
#[allow(clippy::needless_collect)] #[allow(clippy::needless_collect)]
pub(crate) fn insert_new_if_missing_into_primary_index<'a>( pub(crate) fn insert_new_if_missing_into_primary_index(
&'a self, &self,
slot: Slot, slot: Slot,
item_len: usize, item_len: usize,
items: impl Iterator<Item = (&'a Pubkey, T)>, items: impl Iterator<Item = (Pubkey, T)>,
) -> (Vec<Pubkey>, u64) { ) -> (Vec<Pubkey>, u64) {
let expected_items_per_bin = item_len * 2 / BINS; // big enough so not likely to re-allocate, small enough to not over-allocate let expected_items_per_bin = item_len * 2 / BINS; // big enough so not likely to re-allocate, small enough to not over-allocate
let mut binned = (0..BINS) let mut binned = (0..BINS)
@ -1388,10 +1388,10 @@ impl<T: 'static + Clone + IsCached + ZeroLamport + std::marker::Sync + std::mark
.map(|pubkey_bin| (pubkey_bin, Vec::with_capacity(expected_items_per_bin))) .map(|pubkey_bin| (pubkey_bin, Vec::with_capacity(expected_items_per_bin)))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
items.for_each(|(pubkey, account_info)| { items.for_each(|(pubkey, account_info)| {
let bin = get_bin_pubkey(pubkey); let bin = get_bin_pubkey(&pubkey);
// this value is equivalent to what update() below would have created if we inserted a new item // this value is equivalent to what update() below would have created if we inserted a new item
let info = WriteAccountMapEntry::new_entry_after_update(slot, account_info); let info = WriteAccountMapEntry::new_entry_after_update(slot, account_info);
binned[bin].1.push((*pubkey, info)); binned[bin].1.push((pubkey, info));
}); });
binned.retain(|x| !x.1.is_empty()); binned.retain(|x| !x.1.is_empty());
@ -2550,7 +2550,7 @@ pub mod tests {
let index = AccountsIndex::<bool>::default(); let index = AccountsIndex::<bool>::default();
let account_info = true; let account_info = true;
let items = vec![(pubkey, account_info)]; let items = vec![(*pubkey, account_info)];
index.insert_new_if_missing_into_primary_index(slot, items.len(), items.into_iter()); index.insert_new_if_missing_into_primary_index(slot, items.len(), items.into_iter());
let mut ancestors = Ancestors::default(); let mut ancestors = Ancestors::default();
@ -2569,7 +2569,7 @@ pub mod tests {
// not zero lamports // not zero lamports
let index = AccountsIndex::<AccountInfoTest>::default(); let index = AccountsIndex::<AccountInfoTest>::default();
let account_info: AccountInfoTest = 0 as AccountInfoTest; let account_info: AccountInfoTest = 0 as AccountInfoTest;
let items = vec![(pubkey, account_info)]; let items = vec![(*pubkey, account_info)];
index.insert_new_if_missing_into_primary_index(slot, items.len(), items.into_iter()); index.insert_new_if_missing_into_primary_index(slot, items.len(), items.into_iter());
let mut ancestors = Ancestors::default(); let mut ancestors = Ancestors::default();
@ -2621,7 +2621,7 @@ pub mod tests {
let index = AccountsIndex::<bool>::default(); let index = AccountsIndex::<bool>::default();
let account_infos = [true, false]; let account_infos = [true, false];
let items = vec![(&key0, account_infos[0]), (&key1, account_infos[1])]; let items = vec![(key0, account_infos[0]), (key1, account_infos[1])];
index.insert_new_if_missing_into_primary_index(slot0, items.len(), items.into_iter()); index.insert_new_if_missing_into_primary_index(slot0, items.len(), items.into_iter());
for (i, key) in [key0, key1].iter().enumerate() { for (i, key) in [key0, key1].iter().enumerate() {
@ -2664,7 +2664,7 @@ pub mod tests {
&mut gc, &mut gc,
); );
} else { } else {
let items = vec![(&key, account_infos[0].clone())]; let items = vec![(key, account_infos[0].clone())];
index.insert_new_if_missing_into_primary_index(slot0, items.len(), items.into_iter()); index.insert_new_if_missing_into_primary_index(slot0, items.len(), items.into_iter());
} }
assert!(gc.is_empty()); assert!(gc.is_empty());
@ -2698,7 +2698,7 @@ pub mod tests {
&mut gc, &mut gc,
); );
} else { } else {
let items = vec![(&key, account_infos[1].clone())]; let items = vec![(key, account_infos[1].clone())];
index.insert_new_if_missing_into_primary_index(slot1, items.len(), items.into_iter()); index.insert_new_if_missing_into_primary_index(slot1, items.len(), items.into_iter());
} }
assert!(gc.is_empty()); assert!(gc.is_empty());