Faster BeaconState enc/dec (#671)

* Add state enc/dec benches

* Add example for flamegraph

* Use `PublicKeyBytes` for `Validator`

* Ripple PublicKeyBytes change through codebase

* Add benches, optimizations to store BeaconState

* Store BeaconState in StorageContainer too

* Optimize StorageContainer with std::mem magic

* Fix rest_api tests
This commit is contained in:
Paul Hauner
2019-12-06 16:44:03 +11:00
committed by GitHub
parent d0319320ce
commit 75efed305c
26 changed files with 508 additions and 124 deletions
+2 -2
View File
@@ -268,7 +268,7 @@ impl<T: EthSpec> BeaconState<T> {
/// returns `None`.
///
/// Requires a fully up-to-date `pubkey_cache`, returns an error if this is not the case.
pub fn get_validator_index(&self, pubkey: &PublicKey) -> Result<Option<usize>, Error> {
pub fn get_validator_index(&self, pubkey: &PublicKeyBytes) -> Result<Option<usize>, Error> {
if self.pubkey_cache.len() == self.validators.len() {
Ok(self.pubkey_cache.get(pubkey))
} else {
@@ -860,7 +860,7 @@ impl<T: EthSpec> BeaconState<T> {
.enumerate()
.skip(self.pubkey_cache.len())
{
let success = self.pubkey_cache.insert(validator.pubkey.clone(), i);
let success = self.pubkey_cache.insert(validator.pubkey.clone().into(), i);
if !success {
return Err(Error::PubkeyCacheInconsistent);
}
+3 -3
View File
@@ -10,7 +10,7 @@ pub struct PubkeyCache {
/// len, as it does not increase when duplicate keys are added. Duplicate keys are used during
/// testing.
len: usize,
map: HashMap<PublicKey, ValidatorIndex>,
map: HashMap<PublicKeyBytes, ValidatorIndex>,
}
impl PubkeyCache {
@@ -23,7 +23,7 @@ impl PubkeyCache {
///
/// The added index must equal the number of validators already added to the map. This ensures
/// that an index is never skipped.
pub fn insert(&mut self, pubkey: PublicKey, index: ValidatorIndex) -> bool {
pub fn insert(&mut self, pubkey: PublicKeyBytes, index: ValidatorIndex) -> bool {
if index == self.len {
self.map.insert(pubkey, index);
self.len += 1;
@@ -34,7 +34,7 @@ impl PubkeyCache {
}
/// Looks up a validator index's by their public key.
pub fn get(&self, pubkey: &PublicKey) -> Option<ValidatorIndex> {
pub fn get(&self, pubkey: &PublicKeyBytes) -> Option<ValidatorIndex> {
self.map.get(pubkey).copied()
}
}
@@ -109,7 +109,7 @@ impl<T: EthSpec> TestingBeaconStateBuilder<T> {
));
Validator {
pubkey: keypair.pk.clone(),
pubkey: keypair.pk.clone().into(),
withdrawal_credentials,
// All validators start active.
activation_eligibility_epoch: T::genesis_epoch(),
+3 -3
View File
@@ -1,4 +1,4 @@
use crate::{test_utils::TestRandom, Epoch, Hash256, PublicKey};
use crate::{test_utils::TestRandom, Epoch, Hash256, PublicKeyBytes};
use serde_derive::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode};
@@ -10,7 +10,7 @@ use tree_hash_derive::TreeHash;
/// Spec v0.9.1
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TestRandom, TreeHash)]
pub struct Validator {
pub pubkey: PublicKey,
pub pubkey: PublicKeyBytes,
pub withdrawal_credentials: Hash256,
pub effective_balance: u64,
pub slashed: bool,
@@ -46,7 +46,7 @@ impl Default for Validator {
/// Yields a "default" `Validator`. Primarily used for testing.
fn default() -> Self {
Self {
pubkey: PublicKey::default(),
pubkey: PublicKeyBytes::empty(),
withdrawal_credentials: Hash256::default(),
activation_eligibility_epoch: Epoch::from(std::u64::MAX),
activation_epoch: Epoch::from(std::u64::MAX),