mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-07 00:42:42 +00:00
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:
79
eth2/types/benches/benches.rs
Normal file
79
eth2/types/benches/benches.rs
Normal file
@@ -0,0 +1,79 @@
|
||||
use criterion::Criterion;
|
||||
use criterion::{black_box, criterion_group, criterion_main, Benchmark};
|
||||
use rayon::prelude::*;
|
||||
use ssz::{Decode, Encode};
|
||||
use types::{
|
||||
test_utils::generate_deterministic_keypair, BeaconState, Eth1Data, EthSpec, Hash256,
|
||||
MainnetEthSpec, Validator,
|
||||
};
|
||||
|
||||
fn get_state<E: EthSpec>(validator_count: usize) -> BeaconState<E> {
|
||||
let spec = &E::default_spec();
|
||||
let eth1_data = Eth1Data {
|
||||
deposit_root: Hash256::zero(),
|
||||
deposit_count: 0,
|
||||
block_hash: Hash256::zero(),
|
||||
};
|
||||
|
||||
let mut state = BeaconState::new(0, eth1_data, spec);
|
||||
|
||||
for i in 0..validator_count {
|
||||
state.balances.push(i as u64).expect("should add balance");
|
||||
}
|
||||
|
||||
state.validators = (0..validator_count)
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>()
|
||||
.par_iter()
|
||||
.map(|&i| Validator {
|
||||
pubkey: generate_deterministic_keypair(i).pk.into(),
|
||||
withdrawal_credentials: Hash256::from_low_u64_le(i as u64),
|
||||
effective_balance: i as u64,
|
||||
slashed: i % 2 == 0,
|
||||
activation_eligibility_epoch: i.into(),
|
||||
activation_epoch: i.into(),
|
||||
exit_epoch: i.into(),
|
||||
withdrawable_epoch: i.into(),
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.into();
|
||||
|
||||
state
|
||||
}
|
||||
|
||||
fn all_benches(c: &mut Criterion) {
|
||||
let validator_count = 16_384;
|
||||
let state = get_state::<MainnetEthSpec>(validator_count);
|
||||
let state_bytes = state.as_ssz_bytes();
|
||||
|
||||
c.bench(
|
||||
&format!("{}_validators", validator_count),
|
||||
Benchmark::new("encode/beacon_state", move |b| {
|
||||
b.iter_batched_ref(
|
||||
|| state.clone(),
|
||||
|state| black_box(state.as_ssz_bytes()),
|
||||
criterion::BatchSize::SmallInput,
|
||||
)
|
||||
})
|
||||
.sample_size(10),
|
||||
);
|
||||
|
||||
c.bench(
|
||||
&format!("{}_validators", validator_count),
|
||||
Benchmark::new("decode/beacon_state", move |b| {
|
||||
b.iter_batched_ref(
|
||||
|| state_bytes.clone(),
|
||||
|bytes| {
|
||||
let state: BeaconState<MainnetEthSpec> =
|
||||
BeaconState::from_ssz_bytes(&bytes).expect("should decode");
|
||||
black_box(state)
|
||||
},
|
||||
criterion::BatchSize::SmallInput,
|
||||
)
|
||||
})
|
||||
.sample_size(10),
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, all_benches,);
|
||||
criterion_main!(benches);
|
||||
Reference in New Issue
Block a user