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

Commit

Permalink
SQUASHME: update call sites
Browse files Browse the repository at this point in the history
  • Loading branch information
t-nelson committed Oct 21, 2022
1 parent 2d7c6a1 commit 0e88006
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 69 deletions.
101 changes: 56 additions & 45 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,11 @@ impl Accounts {
if num == 0 {
return Ok(vec![]);
}
let account_balances = self.accounts_db.scan_accounts(
let mut account_balances = BinaryHeap::new();
self.accounts_db.scan_accounts(
ancestors,
bank_id,
|collector: &mut BinaryHeap<Reverse<(u64, Pubkey)>>, option| {
|option| {
if let Some((pubkey, account, _slot)) = option {
if account.lamports() == 0 {
return;
Expand All @@ -758,16 +759,16 @@ impl Accounts {
if !collect {
return;
}
if collector.len() == num {
let Reverse(entry) = collector
if account_balances.len() == num {
let Reverse(entry) = account_balances
.peek()
.expect("BinaryHeap::peek should succeed when len > 0");
if *entry >= (account.lamports(), *pubkey) {
return;
}
collector.pop();
account_balances.pop();
}
collector.push(Reverse((account.lamports(), *pubkey)));
account_balances.push(Reverse((account.lamports(), *pubkey)));
}
},
&ScanConfig::default(),
Expand Down Expand Up @@ -878,16 +879,19 @@ impl Accounts {
program_id: &Pubkey,
config: &ScanConfig,
) -> ScanResult<Vec<TransactionAccount>> {
self.accounts_db.scan_accounts(
ancestors,
bank_id,
|collector: &mut Vec<TransactionAccount>, some_account_tuple| {
Self::load_while_filtering(collector, some_account_tuple, |account| {
account.owner() == program_id
})
},
config,
)
let mut collector = Vec::new();
self.accounts_db
.scan_accounts(
ancestors,
bank_id,
|some_account_tuple| {
Self::load_while_filtering(&mut collector, some_account_tuple, |account| {
account.owner() == program_id
})
},
config,
)
.map(|_| collector)
}

pub fn load_by_program_with_filter<F: Fn(&AccountSharedData) -> bool>(
Expand All @@ -898,16 +902,19 @@ impl Accounts {
filter: F,
config: &ScanConfig,
) -> ScanResult<Vec<TransactionAccount>> {
self.accounts_db.scan_accounts(
ancestors,
bank_id,
|collector: &mut Vec<TransactionAccount>, some_account_tuple| {
Self::load_while_filtering(collector, some_account_tuple, |account| {
account.owner() == program_id && filter(account)
})
},
config,
)
let mut collector = Vec::new();
self.accounts_db
.scan_accounts(
ancestors,
bank_id,
|some_account_tuple| {
Self::load_while_filtering(&mut collector, some_account_tuple, |account| {
account.owner() == program_id && filter(account)
})
},
config,
)
.map(|_| collector)
}

fn calc_scan_result_size(account: &AccountSharedData) -> usize {
Expand Down Expand Up @@ -957,14 +964,15 @@ impl Accounts {
) -> ScanResult<Vec<TransactionAccount>> {
let sum = AtomicUsize::default();
let config = config.recreate_with_abort();
let mut collector = Vec::new();
let result = self
.accounts_db
.index_scan_accounts(
ancestors,
bank_id,
*index_key,
|collector: &mut Vec<TransactionAccount>, some_account_tuple| {
Self::load_while_filtering(collector, some_account_tuple, |account| {
|some_account_tuple| {
Self::load_while_filtering(&mut collector, some_account_tuple, |account| {
let use_account = filter(account);
if use_account
&& Self::accumulate_and_check_scan_result_size(
Expand All @@ -981,7 +989,7 @@ impl Accounts {
},
&config,
)
.map(|result| result.0);
.map(|_| collector);
Self::maybe_abort_scan(result, &config)
}

Expand All @@ -994,18 +1002,21 @@ impl Accounts {
ancestors: &Ancestors,
bank_id: BankId,
) -> ScanResult<Vec<PubkeyAccountSlot>> {
self.accounts_db.scan_accounts(
ancestors,
bank_id,
|collector: &mut Vec<PubkeyAccountSlot>, some_account_tuple| {
if let Some((pubkey, account, slot)) = some_account_tuple
.filter(|(_, account, _)| Self::is_loadable(account.lamports()))
{
collector.push((*pubkey, account, slot))
}
},
&ScanConfig::default(),
)
let mut collector = Vec::new();
self.accounts_db
.scan_accounts(
ancestors,
bank_id,
|some_account_tuple| {
if let Some((pubkey, account, slot)) = some_account_tuple
.filter(|(_, account, _)| Self::is_loadable(account.lamports()))
{
collector.push((*pubkey, account, slot))
}
},
&ScanConfig::default(),
)
.map(|_| collector)
}

pub fn hold_range_in_memory<R>(
Expand All @@ -1026,15 +1037,15 @@ impl Accounts {
ancestors: &Ancestors,
range: R,
) -> Vec<PubkeyAccountSlot> {
let mut collector = Vec::new();
self.accounts_db.range_scan_accounts(
"", // disable logging of this. We now parallelize it and this results in multiple parallel logs
ancestors,
range,
&ScanConfig::new(true),
|collector: &mut Vec<PubkeyAccountSlot>, option| {
Self::load_with_slot(collector, option)
},
)
|option| Self::load_with_slot(&mut collector, option),
);
collector
}

/// Slow because lock is held for 1 operation instead of many.
Expand Down
53 changes: 29 additions & 24 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10556,11 +10556,12 @@ pub mod tests {
&account1
);

let accounts: Vec<AccountSharedData> = db.unchecked_scan_accounts(
let mut accounts = Vec::new();
db.unchecked_scan_accounts(
"",
&ancestors,
|accounts: &mut Vec<AccountSharedData>, option| {
accounts.push(option.1.take_account());
|_, account, _| {
accounts.push(account.take_account());
},
&ScanConfig::default(),
);
Expand Down Expand Up @@ -11487,40 +11488,42 @@ pub mod tests {
keys: [mint_key].iter().cloned().collect::<HashSet<Pubkey>>(),
});
// Secondary index can't be used - do normal scan: should still find both pubkeys
let found_accounts = accounts
let mut found_accounts = HashSet::new();
let used_index = accounts
.index_scan_accounts(
&Ancestors::default(),
bank_id,
index_key,
|collection: &mut HashSet<Pubkey>, account| {
collection.insert(*account.unwrap().0);
|account| {
found_accounts.insert(*account.unwrap().0);
},
&ScanConfig::default(),
)
.unwrap();
assert!(!found_accounts.1);
assert_eq!(found_accounts.0.len(), 2);
assert!(found_accounts.0.contains(&pubkey1));
assert!(found_accounts.0.contains(&pubkey2));
assert!(!used_index);
assert_eq!(found_accounts.len(), 2);
assert!(found_accounts.contains(&pubkey1));
assert!(found_accounts.contains(&pubkey2));

accounts.account_indexes.keys = None;

// Secondary index can now be used since it isn't marked as excluded
let found_accounts = accounts
let mut found_accounts = HashSet::new();
let used_index = accounts
.index_scan_accounts(
&Ancestors::default(),
bank_id,
index_key,
|collection: &mut HashSet<Pubkey>, account| {
collection.insert(*account.unwrap().0);
|account| {
found_accounts.insert(*account.unwrap().0);
},
&ScanConfig::default(),
)
.unwrap();
assert!(found_accounts.1);
assert_eq!(found_accounts.0.len(), 2);
assert!(found_accounts.0.contains(&pubkey1));
assert!(found_accounts.0.contains(&pubkey2));
assert!(used_index);
assert_eq!(found_accounts.len(), 2);
assert!(found_accounts.contains(&pubkey1));
assert!(found_accounts.contains(&pubkey2));

accounts.account_indexes.keys = None;
}
Expand Down Expand Up @@ -12129,22 +12132,24 @@ pub mod tests {
db.store_uncached(1, &[(&key1, &account1)]);

let ancestors = vec![(0, 0)].into_iter().collect();
let accounts: Vec<AccountSharedData> = db.unchecked_scan_accounts(
let mut accounts = Vec::new();
db.unchecked_scan_accounts(
"",
&ancestors,
|accounts: &mut Vec<AccountSharedData>, option| {
accounts.push(option.1.take_account());
|_, account, _| {
accounts.push(account.take_account());
},
&ScanConfig::default(),
);
assert_eq!(accounts, vec![account0]);

let ancestors = vec![(1, 1), (0, 0)].into_iter().collect();
let accounts: Vec<AccountSharedData> = db.unchecked_scan_accounts(
let mut accounts = Vec::new();
db.unchecked_scan_accounts(
"",
&ancestors,
|accounts: &mut Vec<AccountSharedData>, option| {
accounts.push(option.1.take_account());
|_, account, _| {
accounts.push(account.take_account());
},
&ScanConfig::default(),
);
Expand Down Expand Up @@ -14394,7 +14399,7 @@ pub mod tests {
db.scan_accounts(
&scan_ancestors,
bank_id,
|_collector: &mut Vec<(Pubkey, AccountSharedData)>, maybe_account| {
|maybe_account| {
ready_.store(true, Ordering::Relaxed);
if let Some((pubkey, _, _)) = maybe_account {
if *pubkey == stall_key {
Expand Down

0 comments on commit 0e88006

Please sign in to comment.