From 149a54b786a6bffc576572c9c3d707cb75a8a7d3 Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" Date: Wed, 1 Jun 2022 17:41:56 -0500 Subject: [PATCH] remove par_iter on update index below threshold (#25699) --- runtime/src/accounts_db.rs | 65 ++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index 2549e9204a3205..d6391bbf27a251 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -6990,34 +6990,43 @@ impl AccountsDb { // using a thread pool here results in deadlock panics from bank_hashes.write() // so, instead we limit how many threads will be created to the same size as the bg thread pool let len = std::cmp::min(accounts.len(), infos.len()); - let chunk_size = std::cmp::max(1, len / quarter_thread_count()); // # pubkeys/thread - let batches = 1 + len / chunk_size; - (0..batches) - .into_par_iter() - .map(|batch| { - let start = batch * chunk_size; - let end = std::cmp::min(start + chunk_size, len); - let mut reclaims = Vec::with_capacity((end - start) / 2); - (start..end).into_iter().for_each(|i| { - let info = infos[i]; - let pubkey_account = (accounts.pubkey(i), accounts.account(i)); - let pubkey = pubkey_account.0; - let old_slot = accounts.slot(i); - self.accounts_index.upsert( - target_slot, - old_slot, - pubkey, - pubkey_account.1, - &self.account_indexes, - info, - &mut reclaims, - previous_slot_entry_was_cached, - ); - }); - reclaims - }) - .flatten() - .collect::>() + let threshold = 1; + let update = |start, end| { + let mut reclaims = Vec::with_capacity((end - start) / 2); + + (start..end).into_iter().for_each(|i| { + let info = infos[i]; + let pubkey_account = (accounts.pubkey(i), accounts.account(i)); + let pubkey = pubkey_account.0; + let old_slot = accounts.slot(i); + self.accounts_index.upsert( + target_slot, + old_slot, + pubkey, + pubkey_account.1, + &self.account_indexes, + info, + &mut reclaims, + previous_slot_entry_was_cached, + ); + }); + reclaims + }; + if len > threshold { + let chunk_size = std::cmp::max(1, len / quarter_thread_count()); // # pubkeys/thread + let batches = 1 + len / chunk_size; + (0..batches) + .into_par_iter() + .map(|batch| { + let start = batch * chunk_size; + let end = std::cmp::min(start + chunk_size, len); + update(start, end) + }) + .flatten() + .collect::>() + } else { + update(0, len) + } } fn should_not_shrink(aligned_bytes: u64, total_bytes: u64, num_stores: usize) -> bool {