From 4f83818fdb3cd5631dfb31b4250f094373b24461 Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" <75863576+jeffwashington@users.noreply.github.com> Date: Mon, 27 Sep 2021 16:24:19 -0500 Subject: [PATCH] get rid of WriteAccountMapEntry instance (#19093) --- runtime/src/accounts_index.rs | 106 ++++++++++++---------------------- 1 file changed, 36 insertions(+), 70 deletions(-) diff --git a/runtime/src/accounts_index.rs b/runtime/src/accounts_index.rs index b64fc8d1bb..87fa915dc5 100644 --- a/runtime/src/accounts_index.rs +++ b/runtime/src/accounts_index.rs @@ -284,37 +284,6 @@ impl PreAllocatedAccountMapEntry { } } -#[self_referencing] -pub struct WriteAccountMapEntry { - owned_entry: AccountMapEntry, - #[borrows(owned_entry)] - #[covariant] - slot_list_guard: RwLockWriteGuard<'this, SlotList>, -} - -impl WriteAccountMapEntry { - pub fn from_account_map_entry(account_map_entry: AccountMapEntry) -> Self { - WriteAccountMapEntryBuilder { - owned_entry: account_map_entry, - slot_list_guard_builder: |lock| lock.slot_list.write().unwrap(), - } - .build() - } - - pub fn slot_list(&self) -> &SlotList { - &*self.borrow_slot_list_guard() - } - - pub fn slot_list_mut( - &mut self, - user: impl for<'this> FnOnce(&mut RwLockWriteGuard<'this, SlotList>) -> RT, - ) -> RT { - let result = self.with_slot_list_guard_mut(user); - self.borrow_owned_entry().set_dirty(true); - result - } -} - #[derive(Debug, Default, AbiExample, Clone)] pub struct RollingBitField { max_width: u64, @@ -1199,12 +1168,20 @@ impl AccountsIndex { .map(ReadAccountMapEntry::from_account_map_entry) } - fn get_account_write_entry(&self, pubkey: &Pubkey) -> Option> { - self.account_maps[self.bin_calculator.bin_from_pubkey(pubkey)] + fn slot_list_mut( + &self, + pubkey: &Pubkey, + user: impl for<'a> FnOnce(&mut RwLockWriteGuard<'a, SlotList>) -> RT, + ) -> Option { + let read_lock = self.account_maps[self.bin_calculator.bin_from_pubkey(pubkey)] .read() - .unwrap() - .get(pubkey) - .map(WriteAccountMapEntry::from_account_map_entry) + .unwrap(); + let get = read_lock.get(pubkey); + get.map(|entry| { + let result = user(&mut entry.slot_list.write().unwrap()); + entry.set_dirty(true); + result + }) } pub fn handle_dead_keys( @@ -1342,22 +1319,19 @@ impl AccountsIndex { where C: Contains<'a, Slot>, { - if let Some(mut write_account_map_entry) = self.get_account_write_entry(pubkey) { - write_account_map_entry.slot_list_mut(|slot_list| { - slot_list.retain(|(slot, item)| { - let should_purge = slots_to_purge.contains(slot); - if should_purge { - reclaims.push((*slot, *item)); - false - } else { - true - } - }); - slot_list.is_empty() - }) - } else { - true - } + self.slot_list_mut(pubkey, |slot_list| { + slot_list.retain(|(slot, item)| { + let should_purge = slots_to_purge.contains(slot); + if should_purge { + reclaims.push((*slot, *item)); + false + } else { + true + } + }); + slot_list.is_empty() + }) + .unwrap_or(true) } pub fn min_ongoing_scan_root(&self) -> Option { @@ -1719,12 +1693,10 @@ impl AccountsIndex { max_clean_root: Option, ) { let mut is_slot_list_empty = false; - if let Some(mut locked_entry) = self.get_account_write_entry(pubkey) { - locked_entry.slot_list_mut(|slot_list| { - self.purge_older_root_entries(slot_list, reclaims, max_clean_root); - is_slot_list_empty = slot_list.is_empty(); - }); - } + self.slot_list_mut(pubkey, |slot_list| { + self.purge_older_root_entries(slot_list, reclaims, max_clean_root); + is_slot_list_empty = slot_list.is_empty(); + }); // If the slot list is empty, remove the pubkey from `account_maps`. Make sure to grab the // lock and double check the slot list is still empty, because another writer could have @@ -1910,12 +1882,12 @@ impl AccountsIndex { // if this account has no more entries. Note this does not update the secondary // indexes! pub fn purge_roots(&self, pubkey: &Pubkey) -> (SlotList, bool) { - let mut write_account_map_entry = self.get_account_write_entry(pubkey).unwrap(); - write_account_map_entry.slot_list_mut(|slot_list| { + self.slot_list_mut(pubkey, |slot_list| { let reclaims = self.get_rooted_entries(slot_list, None); slot_list.retain(|(slot, _)| !self.is_root(*slot)); (reclaims, slot_list.is_empty()) }) + .unwrap() } } @@ -3922,10 +3894,7 @@ pub mod tests { secondary_indexes.keys = None; - index - .get_account_write_entry(&account_key) - .unwrap() - .slot_list_mut(|slot_list| slot_list.clear()); + index.slot_list_mut(&account_key, |slot_list| slot_list.clear()); // Everything should be deleted index.handle_dead_keys(&[&account_key], &secondary_indexes); @@ -4028,12 +3997,9 @@ pub mod tests { // was outdated by the update in the later slot, the primary account key is still alive, // so both secondary keys will still be kept alive. index.add_root(later_slot, false); - index - .get_account_write_entry(&account_key) - .unwrap() - .slot_list_mut(|slot_list| { - index.purge_older_root_entries(slot_list, &mut vec![], None) - }); + index.slot_list_mut(&account_key, |slot_list| { + index.purge_older_root_entries(slot_list, &mut vec![], None) + }); check_secondary_index_mapping_correct( secondary_index,