Implement tree hash caching (#584)

* Implement basic tree hash caching

* Use spaces to indent top-level Cargo.toml

* Optimize BLS tree hash by hashing bytes directly

* Implement tree hash caching for validator registry

* Persist BeaconState tree hash cache to disk

* Address Paul's review comments
This commit is contained in:
Michael Sproul
2019-11-05 15:46:52 +11:00
committed by GitHub
parent 4ef66a544a
commit c1a2238f1a
38 changed files with 1112 additions and 248 deletions

View File

@@ -1,8 +1,6 @@
#[macro_use]
extern crate lazy_static;
use criterion::Criterion;
use criterion::{black_box, criterion_group, criterion_main, Benchmark};
use lazy_static::lazy_static;
use types::test_utils::{generate_deterministic_keypairs, TestingBeaconStateBuilder};
use types::{BeaconState, EthSpec, Keypair, MainnetEthSpec, MinimalEthSpec};
@@ -27,25 +25,61 @@ fn build_state<T: EthSpec>(validator_count: usize) -> BeaconState<T> {
state
}
// Note: `state.canonical_root()` uses whatever `tree_hash` that the `types` crate
// uses, which is not necessarily this crate. If you want to ensure that types is
// using this local version of `tree_hash`, ensure you add a workspace-level
// [dependency
// patch](https://doc.rust-lang.org/cargo/reference/manifest.html#the-patch-section).
fn bench_suite<T: EthSpec>(c: &mut Criterion, spec_desc: &str, validator_count: usize) {
let state = build_state::<T>(validator_count);
let state1 = build_state::<T>(validator_count);
let state2 = state1.clone();
let mut state3 = state1.clone();
state3.build_tree_hash_cache().unwrap();
c.bench(
&format!("{}/{}_validators", spec_desc, validator_count),
&format!("{}/{}_validators/no_cache", spec_desc, validator_count),
Benchmark::new("genesis_state", move |b| {
b.iter_batched_ref(
|| state.clone(),
// Note: `state.canonical_root()` uses whatever `tree_hash` that the `types` crate
// uses, which is not necessarily this crate. If you want to ensure that types is
// using this local version of `tree_hash`, ensure you add a workspace-level
// [dependency
// patch](https://doc.rust-lang.org/cargo/reference/manifest.html#the-patch-section).
|| state1.clone(),
|state| black_box(state.canonical_root()),
criterion::BatchSize::SmallInput,
)
})
.sample_size(10),
);
c.bench(
&format!("{}/{}_validators/empty_cache", spec_desc, validator_count),
Benchmark::new("genesis_state", move |b| {
b.iter_batched_ref(
|| state2.clone(),
|state| {
assert!(!state.tree_hash_cache.is_initialized());
black_box(state.update_tree_hash_cache().unwrap())
},
criterion::BatchSize::SmallInput,
)
})
.sample_size(10),
);
c.bench(
&format!(
"{}/{}_validators/up_to_date_cache",
spec_desc, validator_count
),
Benchmark::new("genesis_state", move |b| {
b.iter_batched_ref(
|| state3.clone(),
|state| {
assert!(state.tree_hash_cache.is_initialized());
black_box(state.update_tree_hash_cache().unwrap())
},
criterion::BatchSize::SmallInput,
)
})
.sample_size(10),
);
}
fn all_benches(c: &mut Criterion) {