Remove `fn slot_deltas()` from StatusCache (#26931)

This commit is contained in:
Brooks Prumo 2022-08-16 06:08:29 -04:00 committed by GitHub
parent 3a9bc4bf37
commit bc0d01110c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 40 deletions

View File

@ -28,26 +28,12 @@ fn bench_status_cache_serialize(bencher: &mut Bencher) {
status_cache.insert(&blockhash, &sig, 0, Ok(()));
}
}
assert!(status_cache.roots().contains(&0));
bencher.iter(|| {
let _ = serialize(&status_cache.slot_deltas(&[0])).unwrap();
let _ = serialize(&status_cache.root_slot_deltas()).unwrap();
});
}
#[bench]
fn bench_status_cache_slot_deltas(bencher: &mut Bencher) {
let mut status_cache = BankStatusCache::default();
// fill the status cache
let slots: Vec<_> = (42..).take(MAX_CACHE_ENTRIES).collect();
for slot in &slots {
for _ in 0..5 {
status_cache.insert(&Hash::new_unique(), Hash::new_unique(), *slot, Ok(()));
}
}
bencher.iter(|| test::black_box(status_cache.slot_deltas(&slots)));
}
#[bench]
fn bench_status_cache_root_slot_deltas(bencher: &mut Bencher) {
let mut status_cache = BankStatusCache::default();

View File

@ -221,30 +221,15 @@ impl<T: Serialize + Clone> StatusCache<T> {
.for_each(|(_, status)| status.lock().unwrap().clear());
}
// returns the statuses for each slot in the slots provided
pub fn slot_deltas(&self, slots: &[Slot]) -> Vec<SlotDelta<T>> {
let empty = Arc::new(Mutex::new(HashMap::new()));
slots
.iter()
.map(|slot| {
(
*slot,
self.roots.contains(slot),
Arc::clone(self.slot_deltas.get(slot).unwrap_or(&empty)),
)
})
.collect()
}
/// Get the statuses for all the root slots
pub fn root_slot_deltas(&self) -> Vec<SlotDelta<T>> {
self.roots
self.roots()
.iter()
.map(|slot| {
.map(|root| {
(
*slot,
true,
self.slot_deltas.get(slot).cloned().unwrap_or_default(),
*root,
true, // <-- is_root
self.slot_deltas.get(root).cloned().unwrap_or_default(),
)
})
.collect()
@ -444,10 +429,11 @@ mod tests {
let blockhash = hash(Hash::default().as_ref());
status_cache.clear();
status_cache.insert(&blockhash, &sig, 0, ());
let slot_deltas = status_cache.slot_deltas(&[0]);
assert!(status_cache.roots().contains(&0));
let slot_deltas = status_cache.root_slot_deltas();
let cache = StatusCache::from_slot_deltas(&slot_deltas);
assert_eq!(cache, status_cache);
let slot_deltas = cache.slot_deltas(&[0]);
let slot_deltas = cache.root_slot_deltas();
let cache = StatusCache::from_slot_deltas(&slot_deltas);
assert_eq!(cache, status_cache);
}
@ -464,10 +450,9 @@ mod tests {
for i in 0..(MAX_CACHE_ENTRIES + 1) {
status_cache.add_root(i as u64);
}
let slots: Vec<_> = (0..MAX_CACHE_ENTRIES as u64 + 1).collect();
assert_eq!(status_cache.slot_deltas.len(), 1);
assert!(status_cache.slot_deltas.get(&1).is_some());
let slot_deltas = status_cache.slot_deltas(&slots);
let slot_deltas = status_cache.root_slot_deltas();
let cache = StatusCache::from_slot_deltas(&slot_deltas);
assert_eq!(cache, status_cache);
}