From 4b85a16a363458eeb2e453b3178bb28dd2d374fb Mon Sep 17 00:00:00 2001 From: Rigidity Date: Tue, 30 Jul 2024 08:02:29 -0500 Subject: [PATCH] Make sure the benchmarks are accurate --- crates/chia-bls/benches/cache.rs | 23 ++++++++++++++--------- crates/chia-bls/src/bls_cache.rs | 8 ++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/crates/chia-bls/benches/cache.rs b/crates/chia-bls/benches/cache.rs index 9d3e22fb7..c2960373c 100644 --- a/crates/chia-bls/benches/cache.rs +++ b/crates/chia-bls/benches/cache.rs @@ -23,45 +23,50 @@ fn cache_benchmark(c: &mut Criterion) { pks.push(pk); } + let bls_cache = BlsCache::default(); c.bench_function("bls_cache.aggregate_verify, 0% cache hits", |b| { b.iter(|| { - let bls_cache = BlsCache::default(); + let bls_cache = bls_cache.clone(); assert!(bls_cache.aggregate_verify(pks.iter().zip([&msg].iter().cycle()), &agg_sig)); }); }); // populate 10% of keys + let bls_cache = BlsCache::default(); + bls_cache.aggregate_verify(pks[0..100].iter().zip([&msg].iter().cycle()), &agg_sig); c.bench_function("bls_cache.aggregate_verify, 10% cache hits", |b| { b.iter(|| { - let bls_cache = BlsCache::default(); - bls_cache.aggregate_verify(pks[0..100].iter().zip([&msg].iter().cycle()), &agg_sig); + let bls_cache = bls_cache.clone(); assert!(bls_cache.aggregate_verify(pks.iter().zip([&msg].iter().cycle()), &agg_sig)); }); }); // populate another 10% of keys + let bls_cache = BlsCache::default(); + bls_cache.aggregate_verify(pks[0..200].iter().zip([&msg].iter().cycle()), &agg_sig); c.bench_function("bls_cache.aggregate_verify, 20% cache hits", |b| { b.iter(|| { - let bls_cache = BlsCache::default(); - bls_cache.aggregate_verify(pks[0..200].iter().zip([&msg].iter().cycle()), &agg_sig); + let bls_cache = bls_cache.clone(); assert!(bls_cache.aggregate_verify(pks.iter().zip([&msg].iter().cycle()), &agg_sig)); }); }); // populate another 30% of keys + let bls_cache = BlsCache::default(); + bls_cache.aggregate_verify(pks[0..500].iter().zip([&msg].iter().cycle()), &agg_sig); c.bench_function("bls_cache.aggregate_verify, 50% cache hits", |b| { b.iter(|| { - let bls_cache = BlsCache::default(); - bls_cache.aggregate_verify(pks[0..500].iter().zip([&msg].iter().cycle()), &agg_sig); + let bls_cache = bls_cache.clone(); assert!(bls_cache.aggregate_verify(pks.iter().zip([&msg].iter().cycle()), &agg_sig)); }); }); // populate all other keys + let bls_cache = BlsCache::default(); + bls_cache.aggregate_verify(pks[0..1000].iter().zip([&msg].iter().cycle()), &agg_sig); c.bench_function("bls_cache.aggregate_verify, 100% cache hits", |b| { b.iter(|| { - let bls_cache = BlsCache::default(); - bls_cache.aggregate_verify(pks[0..1000].iter().zip([&msg].iter().cycle()), &agg_sig); + let bls_cache = bls_cache.clone(); assert!(bls_cache.aggregate_verify(pks.iter().zip([&msg].iter().cycle()), &agg_sig)); }); }); diff --git a/crates/chia-bls/src/bls_cache.rs b/crates/chia-bls/src/bls_cache.rs index 355c2e017..e97af3f2e 100644 --- a/crates/chia-bls/src/bls_cache.rs +++ b/crates/chia-bls/src/bls_cache.rs @@ -22,6 +22,14 @@ pub struct BlsCache { cache: Mutex>, } +impl Clone for BlsCache { + fn clone(&self) -> Self { + Self { + cache: Mutex::new(self.cache.lock().clone()), + } + } +} + impl Default for BlsCache { fn default() -> Self { Self::new(NonZeroUsize::new(50000).unwrap())