Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit bd9fae9

Browse files
committed
make ancestors parameter optional to avoid forcing construction of empty hash maps
1 parent 2b18989 commit bd9fae9

File tree

3 files changed

+32
-31
lines changed

3 files changed

+32
-31
lines changed

runtime/src/accounts_db.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -624,11 +624,10 @@ impl AccountsDB {
624624
let (mut purges, purges_in_root) = pubkeys
625625
.par_chunks(4096)
626626
.map(|pubkeys: &[Pubkey]| {
627-
let no_ancestors = HashMap::new();
628627
let mut purges_in_root = Vec::new();
629628
let mut purges = HashMap::new();
630629
for pubkey in pubkeys {
631-
if let Some((list, index)) = accounts_index.get(pubkey, &no_ancestors) {
630+
if let Some((list, index)) = accounts_index.get(pubkey, None) {
632631
let (slot, account_info) = &list[index];
633632
if account_info.lamports == 0 {
634633
purges.insert(*pubkey, accounts_index.would_purge(pubkey));
@@ -801,7 +800,6 @@ impl AccountsDB {
801800
}
802801

803802
let alive_accounts: Vec<_> = {
804-
let no_ancestors = HashMap::new();
805803
let accounts_index = self.accounts_index.read().unwrap();
806804
stored_accounts
807805
.iter()
@@ -814,7 +812,7 @@ impl AccountsDB {
814812
(store_id, offset),
815813
_write_version,
816814
)| {
817-
if let Some((list, _)) = accounts_index.get(pubkey, &no_ancestors) {
815+
if let Some((list, _)) = accounts_index.get(pubkey, None) {
818816
list.iter()
819817
.any(|(_slot, i)| i.store_id == *store_id && i.offset == *offset)
820818
} else {
@@ -1015,7 +1013,7 @@ impl AccountsDB {
10151013
accounts_index: &AccountsIndex<AccountInfo>,
10161014
pubkey: &Pubkey,
10171015
) -> Option<(Account, Slot)> {
1018-
let (lock, index) = accounts_index.get(pubkey, ancestors)?;
1016+
let (lock, index) = accounts_index.get(pubkey, Some(ancestors))?;
10191017
let slot = lock[index].0;
10201018
//TODO: thread this as a ref
10211019
if let Some(slot_storage) = storage.0.get(&slot) {
@@ -1032,7 +1030,7 @@ impl AccountsDB {
10321030
#[cfg(test)]
10331031
fn load_account_hash(&self, ancestors: &Ancestors, pubkey: &Pubkey) -> Hash {
10341032
let accounts_index = self.accounts_index.read().unwrap();
1035-
let (lock, index) = accounts_index.get(pubkey, ancestors).unwrap();
1033+
let (lock, index) = accounts_index.get(pubkey, Some(ancestors)).unwrap();
10361034
let slot = lock[index].0;
10371035
let storage = self.storage.read().unwrap();
10381036
let slot_storage = storage.0.get(&slot).unwrap();
@@ -1444,7 +1442,7 @@ impl AccountsDB {
14441442
let hashes: Vec<_> = keys
14451443
.par_iter()
14461444
.filter_map(|pubkey| {
1447-
if let Some((list, index)) = accounts_index.get(pubkey, ancestors) {
1445+
if let Some((list, index)) = accounts_index.get(pubkey, Some(ancestors)) {
14481446
let (slot, account_info) = &list[index];
14491447
if account_info.lamports != 0 {
14501448
storage
@@ -2113,7 +2111,7 @@ pub mod tests {
21132111
.accounts_index
21142112
.read()
21152113
.unwrap()
2116-
.get(&key, &ancestors)
2114+
.get(&key, Some(&ancestors))
21172115
.is_some());
21182116
assert_load_account(&db, unrooted_slot, key, 1);
21192117

@@ -2134,7 +2132,7 @@ pub mod tests {
21342132
.accounts_index
21352133
.read()
21362134
.unwrap()
2137-
.get(&key, &ancestors)
2135+
.get(&key, Some(&ancestors))
21382136
.is_none());
21392137

21402138
// Test we can store for the same slot again and get the right information
@@ -2430,7 +2428,7 @@ pub mod tests {
24302428
let ancestors = vec![(0, 0)].into_iter().collect();
24312429
let id = {
24322430
let index = accounts.accounts_index.read().unwrap();
2433-
let (list, idx) = index.get(&pubkey, &ancestors).unwrap();
2431+
let (list, idx) = index.get(&pubkey, Some(&ancestors)).unwrap();
24342432
list[idx].1.store_id
24352433
};
24362434
accounts.add_root(1);

runtime/src/accounts_index.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,22 @@ impl<'a, T: 'a + Clone> AccountsIndex<T> {
2929
{
3030
for (pubkey, list) in iter {
3131
let list_r = &list.1.read().unwrap();
32-
if let Some(index) = self.latest_slot(ancestors, &list_r) {
32+
if let Some(index) = self.latest_slot(Some(ancestors), &list_r) {
3333
func(pubkey, (&list_r[index].1, list_r[index].0));
3434
}
3535
}
3636
}
3737

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

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

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

9194
/// Get an account
9295
/// The latest account that appears in `ancestors` or `roots` is returned.
93-
pub fn get(
96+
pub(crate) fn get(
9497
&self,
9598
pubkey: &Pubkey,
96-
ancestors: &Ancestors,
99+
ancestors: Option<&Ancestors>,
97100
) -> Option<(RwLockReadGuard<SlotList<T>>, usize)> {
98101
self.account_maps.get(pubkey).and_then(|list| {
99102
let list_r = list.1.read().unwrap();
@@ -245,7 +248,8 @@ mod tests {
245248
let key = Keypair::new();
246249
let index = AccountsIndex::<bool>::default();
247250
let ancestors = HashMap::new();
248-
assert!(index.get(&key.pubkey(), &ancestors).is_none());
251+
assert!(index.get(&key.pubkey(), Some(&ancestors)).is_none());
252+
assert!(index.get(&key.pubkey(), None).is_none());
249253

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

263267
let ancestors = HashMap::new();
264-
assert!(index.get(&key.pubkey(), &ancestors).is_none());
268+
assert!(index.get(&key.pubkey(), Some(&ancestors)).is_none());
269+
assert!(index.get(&key.pubkey(), None).is_none());
265270

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

279284
let ancestors = vec![(1, 1)].into_iter().collect();
280-
assert!(index.get(&key.pubkey(), &ancestors).is_none());
285+
assert!(index.get(&key.pubkey(), Some(&ancestors)).is_none());
281286

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

295300
let ancestors = vec![(0, 0)].into_iter().collect();
296-
let (list, idx) = index.get(&key.pubkey(), &ancestors).unwrap();
301+
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors)).unwrap();
297302
assert_eq!(list[idx], (0, true));
298303

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

327-
let ancestors = vec![].into_iter().collect();
328332
index.add_root(0);
329-
let (list, idx) = index.get(&key.pubkey(), &ancestors).unwrap();
333+
let (list, idx) = index.get(&key.pubkey(), None).unwrap();
330334
assert_eq!(list[idx], (0, true));
331335
}
332336

@@ -369,14 +373,14 @@ mod tests {
369373
let mut gc = Vec::new();
370374
index.insert(0, &key.pubkey(), true, &mut gc);
371375
assert!(gc.is_empty());
372-
let (list, idx) = index.get(&key.pubkey(), &ancestors).unwrap();
376+
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors)).unwrap();
373377
assert_eq!(list[idx], (0, true));
374378
drop(list);
375379

376380
let mut gc = Vec::new();
377381
index.insert(0, &key.pubkey(), false, &mut gc);
378382
assert_eq!(gc, vec![(0, true)]);
379-
let (list, idx) = index.get(&key.pubkey(), &ancestors).unwrap();
383+
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors)).unwrap();
380384
assert_eq!(list[idx], (0, false));
381385
}
382386

@@ -391,10 +395,10 @@ mod tests {
391395
assert!(gc.is_empty());
392396
index.insert(1, &key.pubkey(), false, &mut gc);
393397
assert!(gc.is_empty());
394-
let (list, idx) = index.get(&key.pubkey(), &ancestors).unwrap();
398+
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors)).unwrap();
395399
assert_eq!(list[idx], (0, true));
396400
let ancestors = vec![(1, 0)].into_iter().collect();
397-
let (list, idx) = index.get(&key.pubkey(), &ancestors).unwrap();
401+
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors)).unwrap();
398402
assert_eq!(list[idx], (1, false));
399403
}
400404

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

420423
let mut num = 0;
421424
let mut found_key = false;
422-
index.scan_accounts(&ancestors, |pubkey, _index| {
425+
index.scan_accounts(&Ancestors::new(), |pubkey, _index| {
423426
if pubkey == &key.pubkey() {
424427
found_key = true;
425428
assert_eq!(_index, (&true, 3));

runtime/src/bank.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3854,7 +3854,7 @@ mod tests {
38543854
impl Bank {
38553855
fn slots_by_pubkey(&self, pubkey: &Pubkey, ancestors: &Ancestors) -> Vec<Slot> {
38563856
let accounts_index = self.rc.accounts.accounts_db.accounts_index.read().unwrap();
3857-
let (accounts, _) = accounts_index.get(&pubkey, &ancestors).unwrap();
3857+
let (accounts, _) = accounts_index.get(&pubkey, Some(&ancestors)).unwrap();
38583858
accounts
38593859
.iter()
38603860
.map(|(slot, _)| *slot)

0 commit comments

Comments
 (0)