Skip to content

Commit

Permalink
get rid of several vec collections
Browse files Browse the repository at this point in the history
  • Loading branch information
2501babe committed Jan 17, 2025
1 parent be461b5 commit c234cc6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
10 changes: 3 additions & 7 deletions accounts-db/src/account_locks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,11 @@ impl AccountLocks {
/// the only logic, and this note can be removed with the feature gate.
pub fn try_lock_transaction_batch<'a>(
&mut self,
validated_batch_keys: Vec<Result<impl Iterator<Item = (&'a Pubkey, bool)> + Clone>>,
validated_batch_keys: impl Iterator<
Item = Result<impl Iterator<Item = (&'a Pubkey, bool)> + Clone>,
>,
) -> Vec<Result<()>> {
// HANA TODO the vec allocation here is unfortunate but hard to avoid
// we cannot do this in one closure because of borrow rules
// play around with alternate strategies, according to benches this may be up to
// 50% slower for small batches and few locks, but for large batches and many locks
// it is around 20% faster. so it's not horrible but ideally we improve all-case perf
let available_batch_keys: Vec<_> = validated_batch_keys
.into_iter()
.map(|validated_keys| {
validated_keys
.clone()
Expand Down
41 changes: 17 additions & 24 deletions accounts-db/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,12 +565,10 @@ impl Accounts {
relax_intrabatch_account_locks: bool,
) -> Vec<Result<()>> {
// Validate the account locks, then get iterator if successful validation.
let tx_account_locks_results: Vec<Result<_>> = txs
.map(|tx| {
validate_account_locks(tx.account_keys(), tx_account_lock_limit)
.map(|_| TransactionAccountLocksIterator::new(tx))
})
.collect();
let tx_account_locks_results = txs.map(|tx| {
validate_account_locks(tx.account_keys(), tx_account_lock_limit)
.map(|_| TransactionAccountLocksIterator::new(tx))
});
self.lock_accounts_inner(tx_account_locks_results, relax_intrabatch_account_locks)
}

Expand All @@ -583,37 +581,32 @@ impl Accounts {
relax_intrabatch_account_locks: bool,
) -> Vec<Result<()>> {
// Validate the account locks, then get iterator if successful validation.
let tx_account_locks_results: Vec<Result<_>> = txs
.zip(results)
.map(|(tx, result)| match result {
Ok(()) => validate_account_locks(tx.account_keys(), tx_account_lock_limit)
.map(|_| TransactionAccountLocksIterator::new(tx)),
Err(err) => Err(err),
})
.collect();
let tx_account_locks_results = txs.zip(results).map(|(tx, result)| match result {
Ok(()) => validate_account_locks(tx.account_keys(), tx_account_lock_limit)
.map(|_| TransactionAccountLocksIterator::new(tx)),
Err(err) => Err(err),
});
self.lock_accounts_inner(tx_account_locks_results, relax_intrabatch_account_locks)
}

#[must_use]
fn lock_accounts_inner(
fn lock_accounts_inner<'a>(
&self,
tx_account_locks_results: Vec<Result<TransactionAccountLocksIterator<impl SVMMessage>>>,
tx_account_locks_results: impl Iterator<
Item = Result<TransactionAccountLocksIterator<'a, impl SVMMessage + 'a>>,
>,
relax_intrabatch_account_locks: bool,
) -> Vec<Result<()>> {
let account_locks = &mut self.account_locks.lock().unwrap();
if relax_intrabatch_account_locks {
let validated_batch_keys = tx_account_locks_results
.into_iter()
.map(|tx_account_locks_result| {
tx_account_locks_result
.map(|tx_account_locks| tx_account_locks.accounts_with_is_writable())
})
.collect();
let validated_batch_keys = tx_account_locks_results.map(|tx_account_locks_result| {
tx_account_locks_result
.map(|tx_account_locks| tx_account_locks.accounts_with_is_writable())
});

account_locks.try_lock_transaction_batch(validated_batch_keys)
} else {
tx_account_locks_results
.into_iter()
.map(|tx_account_locks_result| match tx_account_locks_result {
Ok(tx_account_locks) => account_locks
.try_lock_accounts(tx_account_locks.accounts_with_is_writable()),
Expand Down

0 comments on commit c234cc6

Please sign in to comment.