-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ref(lru-cache): Change lru cache crate to avoid vulnerable dependency #478
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fac5a52
perf(lru cache): Benchmarks for LRU caches
flub 786e63e
Improve benchmarks: measure just the operation
flub 3a2fe0d
feat(lru): Switch implementation to lru crate
flub cb3e4fc
Use current version of criterion
flub 30acbc4
Remove caches crate entirely
flub 17396f6
remove old crate entirely
flub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
//! Test the LRU cache implementation. | ||
//! | ||
//! These are a few simple tests of the operations we do on empty and full caches. Mostly | ||
//! how populated the cache is doesn't seem to affect things much. | ||
//! | ||
//! # Running the benchmarks | ||
//! | ||
//! Install `cargo-criterion`: | ||
//! | ||
//! ```shell | ||
//! cargo install cargo-criterion | ||
//! ``` | ||
//! | ||
//! Run the benchmarks: | ||
//! | ||
//! ```shell | ||
//! cargo criterion -p iroh-p2p | ||
//! ``` | ||
|
||
use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; | ||
use libp2p::PeerId; | ||
|
||
// The size of the cache to make. Taken from behaviour::peer_manager::DEFAULT_BAD_PEER_CAP. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be |
||
const CACHE_SIZE: usize = 10 * 4096; | ||
|
||
fn bench_contains_empty(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("Contains, almost empty cache"); | ||
group.bench_function("lru", |bencher| { | ||
bencher.iter_batched( | ||
// setup | ||
|| { | ||
let mut cache = lru::LruCache::new(CACHE_SIZE.try_into().unwrap()); | ||
let peer = PeerId::random(); | ||
cache.put(peer, ()); | ||
for _ in 0..16 { | ||
cache.put(PeerId::random(), ()); | ||
} | ||
let missing = PeerId::random(); | ||
assert!(cache.contains(&peer)); | ||
assert!(!cache.contains(&missing)); | ||
(cache, peer, missing) | ||
}, | ||
// routine | ||
|(cache, peer, missing)| { | ||
cache.contains(&peer); | ||
cache.contains(&missing); | ||
cache // drop outside of routine | ||
}, | ||
BatchSize::SmallInput, | ||
) | ||
}); | ||
group.finish(); | ||
} | ||
|
||
fn bench_contains_full(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("Contains, full cache"); | ||
group.bench_function("lru", |bencher| { | ||
bencher.iter_batched( | ||
// setup | ||
|| { | ||
let mut cache = lru::LruCache::new(CACHE_SIZE.try_into().unwrap()); | ||
for _ in 0..CACHE_SIZE { | ||
cache.put(PeerId::random(), ()); | ||
} | ||
let peer = PeerId::random(); | ||
cache.put(peer, ()); | ||
let missing = PeerId::random(); | ||
assert!(cache.contains(&peer)); | ||
assert!(!cache.contains(&missing)); | ||
(cache, peer, missing) | ||
}, | ||
// routine | ||
|(cache, peer, missing)| { | ||
cache.contains(&peer); | ||
cache.contains(&missing); | ||
cache // drop outside of routine | ||
}, | ||
BatchSize::LargeInput, | ||
) | ||
}); | ||
group.finish(); | ||
} | ||
|
||
fn bench_put_empty(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("put, almost empty cache"); | ||
group.bench_function("lru", |bencher| { | ||
bencher.iter_batched( | ||
// setup | ||
|| { | ||
let cache = lru::LruCache::new(CACHE_SIZE.try_into().unwrap()); | ||
let peer_id = PeerId::random(); | ||
(cache, peer_id) | ||
}, | ||
// routine | ||
|(mut cache, peer_id)| { | ||
cache.put(peer_id, ()); | ||
(cache, peer_id) // drop outside of routine | ||
}, | ||
BatchSize::SmallInput, | ||
) | ||
}); | ||
group.finish(); | ||
} | ||
|
||
fn bench_put_full(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("put, full cache"); | ||
group.bench_function("lru", |bencher| { | ||
bencher.iter_batched( | ||
// setup | ||
|| { | ||
let mut cache = lru::LruCache::new(CACHE_SIZE.try_into().unwrap()); | ||
for _ in 0..CACHE_SIZE { | ||
cache.put(PeerId::random(), ()); | ||
} | ||
let peer_id = PeerId::random(); | ||
(cache, peer_id) | ||
}, | ||
// routine | ||
|(mut cache, peer_id)| { | ||
cache.put(peer_id, ()); | ||
(cache, peer_id) // drop outside of routine | ||
}, | ||
BatchSize::LargeInput, | ||
) | ||
}); | ||
group.finish(); | ||
} | ||
|
||
fn bench_pop_empty(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("pop, almost empty cache"); | ||
group.bench_function("lru", |benches| { | ||
benches.iter_batched( | ||
// setup | ||
|| { | ||
let mut cache = lru::LruCache::new(CACHE_SIZE.try_into().unwrap()); | ||
for _ in 0..16 { | ||
cache.put(PeerId::random(), ()); | ||
} | ||
let peer_id = PeerId::random(); | ||
cache.put(peer_id, ()); | ||
(cache, peer_id) | ||
}, | ||
// routine | ||
|(mut cache, peer_id)| { | ||
cache.pop(&peer_id); | ||
(cache, peer_id) // drop outside of routine | ||
}, | ||
BatchSize::SmallInput, | ||
) | ||
}); | ||
group.finish(); | ||
} | ||
fn bench_pop_full(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("pop, full cache"); | ||
group.bench_function("lru", |benches| { | ||
benches.iter_batched( | ||
// setup | ||
|| { | ||
let mut cache = lru::LruCache::new(CACHE_SIZE.try_into().unwrap()); | ||
for _ in 0..CACHE_SIZE { | ||
cache.put(PeerId::random(), ()); | ||
} | ||
let peer_id = PeerId::random(); | ||
cache.put(peer_id, ()); | ||
(cache, peer_id) | ||
}, | ||
// routine | ||
|(mut cache, peer_id)| { | ||
cache.pop(&peer_id); | ||
(cache, peer_id) // drop outside of routine | ||
}, | ||
BatchSize::LargeInput, | ||
) | ||
}); | ||
group.finish(); | ||
} | ||
|
||
criterion_group!( | ||
benches, | ||
bench_contains_empty, | ||
bench_contains_full, | ||
bench_put_empty, | ||
bench_put_full, | ||
bench_pop_empty, | ||
bench_pop_full, | ||
); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ran these locally, works! If we add more benchmarks that use this pattern we can define an
xtask
to run them. I'd skip it for this one, as we're mainly hanging on to this bench for future useThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am confused, you can just run the benchmarks with
cargo bench
if the config is setup correctly: https://github.com/bheisler/criterion.rs#quickstartThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true, i forgot that worked by the time i was finished: #483