AcctIdx: condense upsert to always use read lock (#20148)

This commit is contained in:
Jeff Washington (jwash)
2021-09-23 19:19:27 -05:00
committed by GitHub
parent 5402a77c63
commit b3bb079d9f
2 changed files with 4 additions and 52 deletions

View File

@@ -244,6 +244,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
) {
let m = Measure::start("entry");
let mut map = self.map().write().unwrap();
// note: an optimization is to use read lock and use get here instead of write lock entry
let entry = map.entry(*pubkey);
let stats = &self.stats();
let (count, time) = if matches!(entry, Entry::Occupied(_)) {
@@ -363,28 +364,6 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
))
}
// returns true if upsert was successful. new_value is modified in this case. new_value contains a RwLock
// otherwise, new_value has not been modified and the pubkey has to be added to the maps with a write lock. call upsert_new
pub fn update_key_if_exists(
&self,
pubkey: &Pubkey,
new_value: PreAllocatedAccountMapEntry<T>,
reclaims: &mut SlotList<T>,
previous_slot_entry_was_cached: bool,
) -> (bool, Option<PreAllocatedAccountMapEntry<T>>) {
if let Some(current) = self.map().read().unwrap().get(pubkey) {
Self::lock_and_update_slot_list(
current,
new_value.into(),
reclaims,
previous_slot_entry_was_cached,
);
(true, None)
} else {
(false, Some(new_value))
}
}
pub fn len(&self) -> usize {
self.map().read().unwrap().len()
}