From 2874f6b0a2bbc4fd58a9173e22f615387daa0c5b Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" <75863576+jeffwashington@users.noreply.github.com> Date: Thu, 2 Sep 2021 16:54:37 -0500 Subject: [PATCH] create remove_if_slot_list_empty to define accounts index behavior (#19581) --- runtime/src/accounts_index.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/runtime/src/accounts_index.rs b/runtime/src/accounts_index.rs index 064f654cf7..81852c8d43 100644 --- a/runtime/src/accounts_index.rs +++ b/runtime/src/accounts_index.rs @@ -1233,20 +1233,32 @@ impl AccountsIndex { if !dead_keys.is_empty() { for key in dead_keys.iter() { let mut w_index = self.get_account_maps_write_lock(key); - if let Entry::Occupied(index_entry) = w_index.entry(**key) { - if index_entry.get().slot_list.read().unwrap().is_empty() { - index_entry.remove(); - - // Note it's only safe to remove all the entries for this key - // because we have the lock for this key's entry in the AccountsIndex, - // so no other thread is also updating the index - self.purge_secondary_indexes_by_inner_key(key, account_indexes); - } + if self.remove_if_slot_list_empty(key, &mut w_index) { + // Note it's only safe to remove all the entries for this key + // because we have the lock for this key's entry in the AccountsIndex, + // so no other thread is also updating the index + self.purge_secondary_indexes_by_inner_key(key, account_indexes); } } } } + // If the slot list for pubkey exists in the index and is empty, remove the index entry for pubkey and return true. + // Return false otherwise. + fn remove_if_slot_list_empty( + &self, + pubkey: &Pubkey, + lock: &mut AccountMapsWriteLock, + ) -> bool { + if let Entry::Occupied(index_entry) = lock.entry(*pubkey) { + if index_entry.get().slot_list.read().unwrap().is_empty() { + index_entry.remove(); + return true; + } + } + false + } + /// call func with every pubkey and index visible from a given set of ancestors pub(crate) fn scan_accounts( &self,