From c234cc64321b95f9be64756222fbbc8fa1347a90 Mon Sep 17 00:00:00 2001 From: hana <81144685+2501babe@users.noreply.github.com> Date: Fri, 17 Jan 2025 13:34:20 +0000 Subject: [PATCH] get rid of several vec collections --- accounts-db/src/account_locks.rs | 10 +++----- accounts-db/src/accounts.rs | 41 +++++++++++++------------------- 2 files changed, 20 insertions(+), 31 deletions(-) diff --git a/accounts-db/src/account_locks.rs b/accounts-db/src/account_locks.rs index 9c5843239d407d..65a3d03f67ad60 100644 --- a/accounts-db/src/account_locks.rs +++ b/accounts-db/src/account_locks.rs @@ -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 + Clone>>, + validated_batch_keys: impl Iterator< + Item = Result + Clone>, + >, ) -> Vec> { - // 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() diff --git a/accounts-db/src/accounts.rs b/accounts-db/src/accounts.rs index a8fecadead478f..cc6d164422b18a 100644 --- a/accounts-db/src/accounts.rs +++ b/accounts-db/src/accounts.rs @@ -565,12 +565,10 @@ impl Accounts { relax_intrabatch_account_locks: bool, ) -> Vec> { // Validate the account locks, then get iterator if successful validation. - let tx_account_locks_results: Vec> = 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) } @@ -583,37 +581,32 @@ impl Accounts { relax_intrabatch_account_locks: bool, ) -> Vec> { // Validate the account locks, then get iterator if successful validation. - let tx_account_locks_results: Vec> = 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>>, + tx_account_locks_results: impl Iterator< + Item = Result>, + >, relax_intrabatch_account_locks: bool, ) -> Vec> { 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()),