Skip to content

Commit

Permalink
Make sure the benchmarks are accurate
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Jul 30, 2024
1 parent 06587e0 commit 4b85a16
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
23 changes: 14 additions & 9 deletions crates/chia-bls/benches/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
Expand Down
8 changes: 8 additions & 0 deletions crates/chia-bls/src/bls_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ pub struct BlsCache {
cache: Mutex<LruCache<[u8; 32], GTElement>>,
}

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())
Expand Down

0 comments on commit 4b85a16

Please sign in to comment.