From c9a3b8941f8e7efe637f1a77bd9f0287652b7a70 Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" <75863576+jeffwashington@users.noreply.github.com> Date: Sun, 12 Sep 2021 13:39:29 -0500 Subject: [PATCH] move insert_new_entry_if_missing_with_lock into InMemAccountsIndex (#19800) --- runtime/src/accounts_index.rs | 30 ++-------------------------- runtime/src/in_mem_accounts_index.rs | 24 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/runtime/src/accounts_index.rs b/runtime/src/accounts_index.rs index b283186383..4ae9d1755b 100644 --- a/runtime/src/accounts_index.rs +++ b/runtime/src/accounts_index.rs @@ -1203,29 +1203,6 @@ impl AccountsIndex { .map(WriteAccountMapEntry::from_account_map_entry) } - // return None if item was created new - // if entry for pubkey already existed, return Some(entry). Caller needs to call entry.update. - fn insert_new_entry_if_missing_with_lock( - &self, - pubkey: Pubkey, - w_account_maps: &mut AccountMapsWriteLock, - new_entry: AccountMapEntry, - ) -> Option<(WriteAccountMapEntry, T, Pubkey)> { - let account_entry = w_account_maps.entry(pubkey); - match account_entry { - Entry::Occupied(account_entry) => Some(( - WriteAccountMapEntry::from_account_map_entry(account_entry.get().clone()), - // extract the new account_info from the unused 'new_entry' - new_entry.slot_list.write().unwrap().remove(0).1, - *account_entry.key(), - )), - Entry::Vacant(account_entry) => { - account_entry.insert(new_entry); - None - } - } - } - pub fn handle_dead_keys( &self, dead_keys: &[&Pubkey], @@ -1593,11 +1570,8 @@ impl AccountsIndex { let mut w_account_maps = self.account_maps[pubkey_bin].write().unwrap(); let mut insert_time = Measure::start("insert_into_primary_index"); items.into_iter().for_each(|(pubkey, new_item)| { - let already_exists = self.insert_new_entry_if_missing_with_lock( - pubkey, - &mut w_account_maps, - new_item, - ); + let already_exists = + w_account_maps.insert_new_entry_if_missing_with_lock(pubkey, new_item); if let Some((mut w_account_entry, account_info, pubkey)) = already_exists { let is_zero_lamport = account_info.is_zero_lamport(); w_account_entry.update(slot, account_info, &mut _reclaims); diff --git a/runtime/src/in_mem_accounts_index.rs b/runtime/src/in_mem_accounts_index.rs index 87e7cfeedb..6b1f320d61 100644 --- a/runtime/src/in_mem_accounts_index.rs +++ b/runtime/src/in_mem_accounts_index.rs @@ -1,4 +1,4 @@ -use crate::accounts_index::{AccountMapEntry, IsCached}; +use crate::accounts_index::{AccountMapEntry, IsCached, WriteAccountMapEntry}; use solana_sdk::pubkey::Pubkey; use std::collections::{ hash_map::{Entry, Keys}, @@ -61,4 +61,26 @@ impl InMemAccountsIndex { pub fn is_empty(&self) -> bool { self.len() == 0 } + + // return None if item was created new + // if entry for pubkey already existed, return Some(entry). Caller needs to call entry.update. + pub fn insert_new_entry_if_missing_with_lock( + &mut self, + pubkey: Pubkey, + new_entry: AccountMapEntry, + ) -> Option<(WriteAccountMapEntry, T, Pubkey)> { + let account_entry = self.map.entry(pubkey); + match account_entry { + Entry::Occupied(account_entry) => Some(( + WriteAccountMapEntry::from_account_map_entry(account_entry.get().clone()), + // extract the new account_info from the unused 'new_entry' + new_entry.slot_list.write().unwrap().remove(0).1, + *account_entry.key(), + )), + Entry::Vacant(account_entry) => { + account_entry.insert(new_entry); + None + } + } + } }