Skip to content

Commit

Permalink
runtime: remove Default req on account scan interfaces (solana-labs…
Browse files Browse the repository at this point in the history
  • Loading branch information
t-nelson committed Oct 26, 2022
1 parent c312f22 commit c2ce2c8
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 146 deletions.
81 changes: 36 additions & 45 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,10 +614,9 @@ impl Accounts {
if num == 0 {
return Ok(vec![]);
}
let account_balances = self.accounts_db.scan_accounts(
ancestors,
bank_id,
|collector: &mut BinaryHeap<Reverse<(u64, Pubkey)>>, option| {
let mut account_balances = BinaryHeap::new();
self.accounts_db
.scan_accounts(ancestors, bank_id, |option| {
if let Some((pubkey, account, _slot)) = option {
if account.lamports() == 0 {
return;
Expand All @@ -630,19 +629,18 @@ 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)));
}
},
)?;
})?;
Ok(account_balances
.into_sorted_vec()
.into_iter()
Expand Down Expand Up @@ -717,15 +715,14 @@ impl Accounts {
bank_id: BankId,
program_id: &Pubkey,
) -> ScanResult<Vec<(Pubkey, AccountSharedData)>> {
self.accounts_db.scan_accounts(
ancestors,
bank_id,
|collector: &mut Vec<(Pubkey, AccountSharedData)>, some_account_tuple| {
Self::load_while_filtering(collector, some_account_tuple, |account| {
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
})
},
)
})
.map(|_| collector)
}

pub fn load_by_program_with_filter<F: Fn(&AccountSharedData) -> bool>(
Expand All @@ -735,15 +732,14 @@ impl Accounts {
program_id: &Pubkey,
filter: F,
) -> ScanResult<Vec<(Pubkey, AccountSharedData)>> {
self.accounts_db.scan_accounts(
ancestors,
bank_id,
|collector: &mut Vec<(Pubkey, AccountSharedData)>, some_account_tuple| {
Self::load_while_filtering(collector, some_account_tuple, |account| {
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)
})
},
)
})
.map(|_| collector)
}

pub fn load_by_index_key_with_filter<F: Fn(&AccountSharedData) -> bool>(
Expand All @@ -753,18 +749,14 @@ impl Accounts {
index_key: &IndexKey,
filter: F,
) -> ScanResult<Vec<(Pubkey, AccountSharedData)>> {
let mut collector = Vec::new();
self.accounts_db
.index_scan_accounts(
ancestors,
bank_id,
*index_key,
|collector: &mut Vec<(Pubkey, AccountSharedData)>, some_account_tuple| {
Self::load_while_filtering(collector, some_account_tuple, |account| {
filter(account)
})
},
)
.map(|result| result.0)
.index_scan_accounts(ancestors, bank_id, *index_key, |some_account_tuple| {
Self::load_while_filtering(&mut collector, some_account_tuple, |account| {
filter(account)
})
})
.map(|_| collector)
}

pub fn account_indexes_include_key(&self, key: &Pubkey) -> bool {
Expand All @@ -776,32 +768,31 @@ impl Accounts {
ancestors: &Ancestors,
bank_id: BankId,
) -> ScanResult<Vec<(Pubkey, AccountSharedData, Slot)>> {
self.accounts_db.scan_accounts(
ancestors,
bank_id,
|collector: &mut Vec<(Pubkey, AccountSharedData, Slot)>, some_account_tuple| {
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))
}
},
)
})
.map(|_| collector)
}

pub fn load_to_collect_rent_eagerly<R: RangeBounds<Pubkey>>(
&self,
ancestors: &Ancestors,
range: R,
) -> Vec<(Pubkey, AccountSharedData)> {
let mut collector = Vec::new();
self.accounts_db.range_scan_accounts(
"load_to_collect_rent_eagerly_scan_elapsed",
ancestors,
range,
|collector: &mut Vec<(Pubkey, AccountSharedData)>, option| {
Self::load_while_filtering(collector, option, |_| true)
},
)
|option| Self::load_while_filtering(&mut collector, option, |_| true),
);
collector
}

/// Slow because lock is held for 1 operation instead of many.
Expand Down
Loading

0 comments on commit c2ce2c8

Please sign in to comment.