Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Remove fn slot_deltas() from StatusCache #26931

Merged
merged 2 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions runtime/benches/status_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
33 changes: 9 additions & 24 deletions runtime/src/status_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down