Skip to content

Commit

Permalink
make ancestors parameter optional to avoid forcing construction of em…
Browse files Browse the repository at this point in the history
…pty hash maps
  • Loading branch information
svenski123 committed Jun 8, 2020
1 parent 2b18989 commit bd9fae9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
18 changes: 8 additions & 10 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,10 @@ impl AccountsDB {
let (mut purges, purges_in_root) = pubkeys
.par_chunks(4096)
.map(|pubkeys: &[Pubkey]| {
let no_ancestors = HashMap::new();
let mut purges_in_root = Vec::new();
let mut purges = HashMap::new();
for pubkey in pubkeys {
if let Some((list, index)) = accounts_index.get(pubkey, &no_ancestors) {
if let Some((list, index)) = accounts_index.get(pubkey, None) {
let (slot, account_info) = &list[index];
if account_info.lamports == 0 {
purges.insert(*pubkey, accounts_index.would_purge(pubkey));
Expand Down Expand Up @@ -801,7 +800,6 @@ impl AccountsDB {
}

let alive_accounts: Vec<_> = {
let no_ancestors = HashMap::new();
let accounts_index = self.accounts_index.read().unwrap();
stored_accounts
.iter()
Expand All @@ -814,7 +812,7 @@ impl AccountsDB {
(store_id, offset),
_write_version,
)| {
if let Some((list, _)) = accounts_index.get(pubkey, &no_ancestors) {
if let Some((list, _)) = accounts_index.get(pubkey, None) {
list.iter()
.any(|(_slot, i)| i.store_id == *store_id && i.offset == *offset)
} else {
Expand Down Expand Up @@ -1015,7 +1013,7 @@ impl AccountsDB {
accounts_index: &AccountsIndex<AccountInfo>,
pubkey: &Pubkey,
) -> Option<(Account, Slot)> {
let (lock, index) = accounts_index.get(pubkey, ancestors)?;
let (lock, index) = accounts_index.get(pubkey, Some(ancestors))?;
let slot = lock[index].0;
//TODO: thread this as a ref
if let Some(slot_storage) = storage.0.get(&slot) {
Expand All @@ -1032,7 +1030,7 @@ impl AccountsDB {
#[cfg(test)]
fn load_account_hash(&self, ancestors: &Ancestors, pubkey: &Pubkey) -> Hash {
let accounts_index = self.accounts_index.read().unwrap();
let (lock, index) = accounts_index.get(pubkey, ancestors).unwrap();
let (lock, index) = accounts_index.get(pubkey, Some(ancestors)).unwrap();
let slot = lock[index].0;
let storage = self.storage.read().unwrap();
let slot_storage = storage.0.get(&slot).unwrap();
Expand Down Expand Up @@ -1444,7 +1442,7 @@ impl AccountsDB {
let hashes: Vec<_> = keys
.par_iter()
.filter_map(|pubkey| {
if let Some((list, index)) = accounts_index.get(pubkey, ancestors) {
if let Some((list, index)) = accounts_index.get(pubkey, Some(ancestors)) {
let (slot, account_info) = &list[index];
if account_info.lamports != 0 {
storage
Expand Down Expand Up @@ -2113,7 +2111,7 @@ pub mod tests {
.accounts_index
.read()
.unwrap()
.get(&key, &ancestors)
.get(&key, Some(&ancestors))
.is_some());
assert_load_account(&db, unrooted_slot, key, 1);

Expand All @@ -2134,7 +2132,7 @@ pub mod tests {
.accounts_index
.read()
.unwrap()
.get(&key, &ancestors)
.get(&key, Some(&ancestors))
.is_none());

// Test we can store for the same slot again and get the right information
Expand Down Expand Up @@ -2430,7 +2428,7 @@ pub mod tests {
let ancestors = vec![(0, 0)].into_iter().collect();
let id = {
let index = accounts.accounts_index.read().unwrap();
let (list, idx) = index.get(&pubkey, &ancestors).unwrap();
let (list, idx) = index.get(&pubkey, Some(&ancestors)).unwrap();
list[idx].1.store_id
};
accounts.add_root(1);
Expand Down
43 changes: 23 additions & 20 deletions runtime/src/accounts_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ impl<'a, T: 'a + Clone> AccountsIndex<T> {
{
for (pubkey, list) in iter {
let list_r = &list.1.read().unwrap();
if let Some(index) = self.latest_slot(ancestors, &list_r) {
if let Some(index) = self.latest_slot(Some(ancestors), &list_r) {
func(pubkey, (&list_r[index].1, list_r[index].0));
}
}
}

/// call func with every pubkey and index visible from a given set of ancestors
pub fn scan_accounts<F>(&self, ancestors: &Ancestors, func: F)
pub(crate) fn scan_accounts<F>(&self, ancestors: &Ancestors, func: F)
where
F: FnMut(&Pubkey, (&T, Slot)),
{
self.do_scan_accounts(ancestors, func, self.account_maps.iter());
}

/// call func with every pubkey and index visible from a given set of ancestors with range
pub fn range_scan_accounts<F, R>(&self, ancestors: &Ancestors, range: R, func: F)
pub(crate) fn range_scan_accounts<F, R>(&self, ancestors: &Ancestors, range: R, func: F)
where
F: FnMut(&Pubkey, (&T, Slot)),
R: RangeBounds<Pubkey>,
Expand Down Expand Up @@ -76,11 +76,14 @@ impl<'a, T: 'a + Clone> AccountsIndex<T> {

// find the latest slot and T in a slice for a given ancestor
// returns index into 'slice' if found, None if not.
fn latest_slot(&self, ancestors: &Ancestors, slice: SlotSlice<T>) -> Option<usize> {
fn latest_slot(&self, ancestors: Option<&Ancestors>, slice: SlotSlice<T>) -> Option<usize> {
let mut max = 0;
let mut rv = None;
for (i, (slot, _t)) in slice.iter().rev().enumerate() {
if *slot >= max && (ancestors.contains_key(slot) || self.is_root(*slot)) {
if *slot >= max
&& (ancestors.map_or(false, |ancestors| ancestors.contains_key(slot))
|| self.is_root(*slot))
{
rv = Some((slice.len() - 1) - i);
max = *slot;
}
Expand All @@ -90,10 +93,10 @@ impl<'a, T: 'a + Clone> AccountsIndex<T> {

/// Get an account
/// The latest account that appears in `ancestors` or `roots` is returned.
pub fn get(
pub(crate) fn get(
&self,
pubkey: &Pubkey,
ancestors: &Ancestors,
ancestors: Option<&Ancestors>,
) -> Option<(RwLockReadGuard<SlotList<T>>, usize)> {
self.account_maps.get(pubkey).and_then(|list| {
let list_r = list.1.read().unwrap();
Expand Down Expand Up @@ -245,7 +248,8 @@ mod tests {
let key = Keypair::new();
let index = AccountsIndex::<bool>::default();
let ancestors = HashMap::new();
assert!(index.get(&key.pubkey(), &ancestors).is_none());
assert!(index.get(&key.pubkey(), Some(&ancestors)).is_none());
assert!(index.get(&key.pubkey(), None).is_none());

let mut num = 0;
index.scan_accounts(&ancestors, |_pubkey, _index| num += 1);
Expand All @@ -261,7 +265,8 @@ mod tests {
assert!(gc.is_empty());

let ancestors = HashMap::new();
assert!(index.get(&key.pubkey(), &ancestors).is_none());
assert!(index.get(&key.pubkey(), Some(&ancestors)).is_none());
assert!(index.get(&key.pubkey(), None).is_none());

let mut num = 0;
index.scan_accounts(&ancestors, |_pubkey, _index| num += 1);
Expand All @@ -277,7 +282,7 @@ mod tests {
assert!(gc.is_empty());

let ancestors = vec![(1, 1)].into_iter().collect();
assert!(index.get(&key.pubkey(), &ancestors).is_none());
assert!(index.get(&key.pubkey(), Some(&ancestors)).is_none());

let mut num = 0;
index.scan_accounts(&ancestors, |_pubkey, _index| num += 1);
Expand All @@ -293,7 +298,7 @@ mod tests {
assert!(gc.is_empty());

let ancestors = vec![(0, 0)].into_iter().collect();
let (list, idx) = index.get(&key.pubkey(), &ancestors).unwrap();
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors)).unwrap();
assert_eq!(list[idx], (0, true));

let mut num = 0;
Expand Down Expand Up @@ -324,9 +329,8 @@ mod tests {
index.insert(0, &key.pubkey(), true, &mut gc);
assert!(gc.is_empty());

let ancestors = vec![].into_iter().collect();
index.add_root(0);
let (list, idx) = index.get(&key.pubkey(), &ancestors).unwrap();
let (list, idx) = index.get(&key.pubkey(), None).unwrap();
assert_eq!(list[idx], (0, true));
}

Expand Down Expand Up @@ -369,14 +373,14 @@ mod tests {
let mut gc = Vec::new();
index.insert(0, &key.pubkey(), true, &mut gc);
assert!(gc.is_empty());
let (list, idx) = index.get(&key.pubkey(), &ancestors).unwrap();
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors)).unwrap();
assert_eq!(list[idx], (0, true));
drop(list);

let mut gc = Vec::new();
index.insert(0, &key.pubkey(), false, &mut gc);
assert_eq!(gc, vec![(0, true)]);
let (list, idx) = index.get(&key.pubkey(), &ancestors).unwrap();
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors)).unwrap();
assert_eq!(list[idx], (0, false));
}

Expand All @@ -391,10 +395,10 @@ mod tests {
assert!(gc.is_empty());
index.insert(1, &key.pubkey(), false, &mut gc);
assert!(gc.is_empty());
let (list, idx) = index.get(&key.pubkey(), &ancestors).unwrap();
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors)).unwrap();
assert_eq!(list[idx], (0, true));
let ancestors = vec![(1, 0)].into_iter().collect();
let (list, idx) = index.get(&key.pubkey(), &ancestors).unwrap();
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors)).unwrap();
assert_eq!(list[idx], (1, false));
}

Expand All @@ -413,13 +417,12 @@ mod tests {
index.add_root(3);
index.insert(4, &key.pubkey(), true, &mut gc);
assert_eq!(gc, vec![(0, true), (1, false), (2, true)]);
let ancestors = vec![].into_iter().collect();
let (list, idx) = index.get(&key.pubkey(), &ancestors).unwrap();
let (list, idx) = index.get(&key.pubkey(), None).unwrap();
assert_eq!(list[idx], (3, true));

let mut num = 0;
let mut found_key = false;
index.scan_accounts(&ancestors, |pubkey, _index| {
index.scan_accounts(&Ancestors::new(), |pubkey, _index| {
if pubkey == &key.pubkey() {
found_key = true;
assert_eq!(_index, (&true, 3));
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3854,7 +3854,7 @@ mod tests {
impl Bank {
fn slots_by_pubkey(&self, pubkey: &Pubkey, ancestors: &Ancestors) -> Vec<Slot> {
let accounts_index = self.rc.accounts.accounts_db.accounts_index.read().unwrap();
let (accounts, _) = accounts_index.get(&pubkey, &ancestors).unwrap();
let (accounts, _) = accounts_index.get(&pubkey, Some(&ancestors)).unwrap();
accounts
.iter()
.map(|(slot, _)| *slot)
Expand Down

0 comments on commit bd9fae9

Please sign in to comment.