Skip to content
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 6 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion iroh-p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ async-stream = "0.3.3"
async-trait = "0.1.56"
asynchronous-codec = "0.6.0"
bytes = "1.1.0"
caches = "0.2.2"
cid = "0.8.0"
clap = { version = "4.0.9", features = ["derive"] }
config = "0.13.1"
Expand All @@ -27,6 +26,7 @@ iroh-rpc-client = { path = "../iroh-rpc-client", default-features = false }
iroh-rpc-types = { path = "../iroh-rpc-types", default-features = false }
iroh-util = { path = "../iroh-util" }
lazy_static = "1.4"
lru = "0.8"
names = { version = "0.14.0", default-features = false }
rand = "0.8.5"
serde = { version = "1.0", features = ["derive"] }
Expand Down Expand Up @@ -71,9 +71,13 @@ default-features = false
features = ["std", "multihash-impl", "identity", "sha2"]

[dev-dependencies]
criterion = "0.4"

[features]
default = ["rpc-grpc", "rpc-mem"]
rpc-grpc = ["iroh-rpc-types/grpc", "iroh-rpc-client/grpc", "iroh-metrics/rpc-grpc"]
rpc-mem = ["iroh-rpc-types/mem", "iroh-rpc-client/mem"]

[[bench]]
name = "lru_cache"
harness = false
187 changes: 187 additions & 0 deletions iroh-p2p/benches/lru_cache.rs
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
//! ```
Comment on lines +6 to +18
Copy link
Member

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 use

Copy link
Contributor

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#quickstart

Copy link
Contributor Author

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


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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
14 changes: 7 additions & 7 deletions iroh-p2p/src/behaviour/peer_manager.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{
num::NonZeroUsize,
task::{Context, Poll},
time::Duration,
};

use ahash::AHashMap;
use caches::{Cache, PutResult};
use iroh_metrics::{core::MRecorder, inc, p2p::P2PMetrics};
use libp2p::{
core::{connection::ConnectionId, transport::ListenerId, ConnectedPoint},
Expand All @@ -16,10 +16,11 @@ use libp2p::{
},
Multiaddr, PeerId,
};
use lru::LruCache;

pub struct PeerManager {
info: AHashMap<PeerId, Info>,
bad_peers: caches::RawLRU<PeerId, ()>,
bad_peers: LruCache<PeerId, ()>,
}

#[derive(Default, Debug, Clone)]
Expand All @@ -35,13 +36,13 @@ impl Info {
}
}

const DEFAULT_BAD_PEER_CAP: usize = 10 * 4096;
const DEFAULT_BAD_PEER_CAP: Option<NonZeroUsize> = NonZeroUsize::new(10 * 4096);

impl Default for PeerManager {
fn default() -> Self {
PeerManager {
info: Default::default(),
bad_peers: caches::RawLRU::new(DEFAULT_BAD_PEER_CAP).unwrap(),
bad_peers: LruCache::new(DEFAULT_BAD_PEER_CAP.unwrap()),
}
}
}
Expand Down Expand Up @@ -94,7 +95,7 @@ impl NetworkBehaviour for PeerManager {
other_established: usize,
) {
if other_established == 0 {
let p = self.bad_peers.remove(peer_id);
let p = self.bad_peers.pop(peer_id);
if p.is_some() {
inc!(P2PMetrics::BadPeerRemoved);
}
Expand Down Expand Up @@ -150,10 +151,9 @@ impl NetworkBehaviour for PeerManager {
match error {
DialError::ConnectionLimit(_) | DialError::DialPeerConditionFalse(_) => {}
_ => {
if PutResult::Put == self.bad_peers.put(peer_id, ()) {
if self.bad_peers.put(peer_id, ()).is_none() {
inc!(P2PMetrics::BadPeer);
}

self.info.remove(&peer_id);
}
}
Expand Down