From 478897651730297c12eefa25a55d2223c4ad84c9 Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" <75863576+jeffwashington@users.noreply.github.com> Date: Wed, 19 May 2021 08:48:46 -0500 Subject: [PATCH] rework slot list update (#17232) --- runtime/src/accounts_index.rs | 39 +++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/runtime/src/accounts_index.rs b/runtime/src/accounts_index.rs index 85708983d9..9fe583450f 100644 --- a/runtime/src/accounts_index.rs +++ b/runtime/src/accounts_index.rs @@ -183,28 +183,31 @@ impl WriteAccountMapEntry { // already exists in the list, remove the older item, add it to `reclaims`, and insert // the new item. pub fn update(&mut self, slot: Slot, account_info: T, reclaims: &mut SlotList) { - // filter out other dirty entries from the same slot - let mut same_slot_previous_updates: Vec<(usize, &(Slot, T))> = self - .slot_list() - .iter() - .enumerate() - .filter(|(_, (s, _))| *s == slot) - .collect(); - assert!(same_slot_previous_updates.len() <= 1); - if let Some((list_index, (s, previous_update_value))) = same_slot_previous_updates.pop() { - let is_flush_from_cache = - previous_update_value.is_cached() && !account_info.is_cached(); - reclaims.push((*s, previous_update_value.clone())); - self.slot_list_mut(|list| list.remove(list_index)); - if is_flush_from_cache { - self.ref_count().fetch_add(1, Ordering::Relaxed); + let mut addref = !account_info.is_cached(); + self.slot_list_mut(|list| { + // find other dirty entries from the same slot + for list_index in 0..list.len() { + let (s, previous_update_value) = &list[list_index]; + if *s == slot { + addref = addref && previous_update_value.is_cached(); + + let mut new_item = (slot, account_info); + std::mem::swap(&mut new_item, &mut list[list_index]); + reclaims.push(new_item); + list[(list_index + 1)..] + .iter() + .for_each(|item| assert!(item.0 != slot)); + return; // this returns from self.slot_list_mut above + } } - } else if !account_info.is_cached() { + + // if we make it here, we did not find the slot in the list + list.push((slot, account_info)); + }); + if addref { // If it's the first non-cache insert, also bump the stored ref count self.ref_count().fetch_add(1, Ordering::Relaxed); } - - self.slot_list_mut(|list| list.push((slot, account_info))); } }