From 2ebfab8e070963b03b7c1a29fb172a71816f65e4 Mon Sep 17 00:00:00 2001 From: Rob Walker Date: Thu, 19 Dec 2019 21:24:12 -0800 Subject: [PATCH] Remove duplicate def of MAX_SLOT_HASHES (#7574) --- sdk/benches/slot_hashes.rs | 23 +++++++++++++++++++++++ sdk/src/slot_hashes.rs | 16 +++++++++++----- sdk/src/sysvar/slot_hashes.rs | 10 +++++----- 3 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 sdk/benches/slot_hashes.rs diff --git a/sdk/benches/slot_hashes.rs b/sdk/benches/slot_hashes.rs new file mode 100644 index 0000000000..be4b94cb80 --- /dev/null +++ b/sdk/benches/slot_hashes.rs @@ -0,0 +1,23 @@ +#![feature(test)] + +extern crate test; +use solana_sdk::{ + hash::Hash, + slot_hashes::{Slot, SlotHashes, MAX_SLOT_HASHES}, + sysvar::Sysvar, +}; +use test::Bencher; + +#[bench] +fn bench_to_from_account(b: &mut Bencher) { + let mut slot_hashes = SlotHashes::new(&[]); + for i in 0..MAX_SLOT_HASHES { + slot_hashes.add(i as Slot, Hash::default()); + } + let mut reps = 0; + b.iter(|| { + reps += 1; + let account = slot_hashes.create_account(0); + slot_hashes = SlotHashes::from_account(&account).unwrap(); + }); +} diff --git a/sdk/src/slot_hashes.rs b/sdk/src/slot_hashes.rs index df499c5407..0a0419e567 100644 --- a/sdk/src/slot_hashes.rs +++ b/sdk/src/slot_hashes.rs @@ -3,9 +3,9 @@ //! this account carries the Bank's most recent blockhashes for some N parents //! use crate::hash::Hash; -use std::ops::Deref; +use std::{iter::FromIterator, ops::Deref}; -pub const MAX_SLOT_HASHES: usize = 512; // 512 slots to get your vote in +pub const MAX_SLOT_HASHES: usize = 512; // about 2.5 minutes to get your vote in pub use crate::clock::Slot; @@ -17,7 +17,7 @@ pub struct SlotHashes(Vec); impl SlotHashes { pub fn add(&mut self, slot: Slot, hash: Hash) { - match self.binary_search_by(|probe| slot.cmp(&probe.0)) { + match self.binary_search_by(|(probe, _)| slot.cmp(&probe)) { Ok(index) => (self.0)[index] = (slot, hash), Err(index) => (self.0).insert(index, (slot, hash)), } @@ -25,17 +25,23 @@ impl SlotHashes { } #[allow(clippy::trivially_copy_pass_by_ref)] pub fn get(&self, slot: &Slot) -> Option<&Hash> { - self.binary_search_by(|probe| slot.cmp(&probe.0)) + self.binary_search_by(|(probe, _)| slot.cmp(&probe)) .ok() .map(|index| &self[index].1) } pub fn new(slot_hashes: &[SlotHash]) -> Self { let mut slot_hashes = slot_hashes.to_vec(); - slot_hashes.sort_by(|a, b| b.0.cmp(&a.0)); // reverse order + slot_hashes.sort_by(|(a, _), (b, _)| b.cmp(a)); Self(slot_hashes) } } +impl FromIterator<(Slot, Hash)> for SlotHashes { + fn from_iter>(iter: I) -> Self { + Self(iter.into_iter().collect()) + } +} + impl Deref for SlotHashes { type Target = Vec; fn deref(&self) -> &Self::Target { diff --git a/sdk/src/sysvar/slot_hashes.rs b/sdk/src/sysvar/slot_hashes.rs index 8029fd7c4e..7bab4b31e1 100644 --- a/sdk/src/sysvar/slot_hashes.rs +++ b/sdk/src/sysvar/slot_hashes.rs @@ -2,17 +2,17 @@ //! //! this account carries the Bank's most recent blockhashes for some N parents //! -pub use crate::slot_hashes::{SlotHash, SlotHashes}; -use crate::{account::Account, sysvar::Sysvar}; +pub use crate::slot_hashes::{Slot, SlotHash, SlotHashes, MAX_SLOT_HASHES}; +use crate::{account::Account, hash::Hash, sysvar::Sysvar}; crate::declare_sysvar_id!("SysvarS1otHashes111111111111111111111111111", SlotHashes); -pub const MAX_SLOT_HASHES: usize = 512; // 512 slots to get your vote in - impl Sysvar for SlotHashes { fn biggest() -> Self { // override - SlotHashes::new(&[SlotHash::default(); MAX_SLOT_HASHES]) + (0..MAX_SLOT_HASHES) + .map(|slot| (slot as Slot, Hash::default())) + .collect::() } }