Remove duplicate def of MAX_SLOT_HASHES (#7574)

This commit is contained in:
Rob Walker 2019-12-19 21:24:12 -08:00 committed by GitHub
parent 9bd5888f5e
commit 2ebfab8e07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 10 deletions

View File

@ -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();
});
}

View File

@ -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<SlotHash>);
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<I: IntoIterator<Item = (Slot, Hash)>>(iter: I) -> Self {
Self(iter.into_iter().collect())
}
}
impl Deref for SlotHashes {
type Target = Vec<SlotHash>;
fn deref(&self) -> &Self::Target {

View File

@ -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::<Self>()
}
}