Skip to content

Commit f50685f

Browse files
authored
chore: favor hashlink::LruCache over lru::LruCache
This PR replaces `lru::LruCache` with `hashlink::LruCache` to unify dependencies with similar functionalities. It also removes `lru` from `Cargo.lock`. (`hashlink` is also used to provide `LinkedHashMap` in `gossipsub`) Pull-Request: #6138.
1 parent 172bbed commit f50685f

File tree

7 files changed

+29
-48
lines changed

7 files changed

+29
-48
lines changed

Cargo.lock

Lines changed: 3 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

misc/peer-store/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ publish = false
99
rust-version.workspace = true
1010

1111
[dependencies]
12+
hashlink = { workspace = true }
1213
libp2p-core = { workspace = true }
1314
libp2p-swarm = { workspace = true }
14-
lru = "0.12.3"
1515
libp2p-identity = { workspace = true, optional = true }
1616

1717
[dev-dependencies]

misc/peer-store/src/memory_store.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use std::{
1414
task::{Poll, Waker},
1515
};
1616

17+
use hashlink::LruCache;
1718
use libp2p_core::{Multiaddr, PeerId};
1819
use libp2p_swarm::{behaviour::ConnectionEstablished, DialError, FromSwarm};
19-
use lru::LruCache;
2020

2121
use super::Store;
2222

@@ -317,15 +317,15 @@ pub struct PeerRecord<T> {
317317
impl<T> PeerRecord<T> {
318318
pub(crate) fn new(cap: NonZeroUsize) -> Self {
319319
Self {
320-
addresses: LruCache::new(cap),
320+
addresses: LruCache::new(cap.get()),
321321
custom_data: None,
322322
}
323323
}
324324

325325
/// Iterate over all addresses. More recently-used address comes first.
326326
/// Does not change the order.
327327
pub fn addresses(&self) -> impl Iterator<Item = &Multiaddr> {
328-
self.addresses.iter().map(|(addr, _)| addr)
328+
self.addresses.iter().rev().map(|(addr, _)| addr)
329329
}
330330

331331
/// Update the address in the LRU cache, promote it to the front if it exists,
@@ -335,14 +335,13 @@ impl<T> PeerRecord<T> {
335335
pub fn add_address(&mut self, address: &Multiaddr, is_permanent: bool) -> bool {
336336
if let Some(was_permanent) = self.addresses.get(address) {
337337
if !*was_permanent && is_permanent {
338-
self.addresses
339-
.get_or_insert(address.clone(), || is_permanent);
338+
self.addresses.insert(address.clone(), is_permanent);
340339
}
341-
return false;
340+
false
341+
} else {
342+
self.addresses.insert(address.clone(), is_permanent);
343+
true
342344
}
343-
self.addresses
344-
.get_or_insert(address.clone(), || is_permanent);
345-
true
346345
}
347346

348347
/// Remove the address in the LRU cache regardless of its position.
@@ -353,7 +352,7 @@ impl<T> PeerRecord<T> {
353352
if !force && self.addresses.peek(address) == Some(&true) {
354353
return false;
355354
}
356-
self.addresses.pop(address).is_some()
355+
self.addresses.remove(address).is_some()
357356
}
358357

359358
pub fn get_custom_data(&self) -> Option<&T> {
@@ -399,14 +398,14 @@ mod test {
399398
store.add_address(&peer, &addr1);
400399
store.add_address(&peer, &addr2);
401400
store.add_address(&peer, &addr3);
402-
assert!(
401+
assert_eq!(
403402
store
404403
.records
405404
.get(&peer)
406405
.expect("peer to be in the store")
407406
.addresses()
408-
.collect::<Vec<_>>()
409-
== vec![&addr3, &addr2, &addr1]
407+
.collect::<Vec<_>>(),
408+
vec![&addr3, &addr2, &addr1]
410409
);
411410
store.add_address(&peer, &addr1);
412411
assert!(

protocols/dcutr/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ quick-protobuf = "0.8"
2323
quick-protobuf-codec = { workspace = true }
2424
thiserror = { workspace = true }
2525
tracing = { workspace = true }
26-
lru = "0.12.3"
26+
hashlink = { workspace = true }
2727
futures-bounded = { workspace = true }
2828

2929
[dev-dependencies]

protocols/dcutr/src/behaviour.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
use std::{
2424
collections::{HashMap, HashSet, VecDeque},
2525
convert::Infallible,
26-
num::NonZeroUsize,
2726
task::{Context, Poll},
2827
};
2928

3029
use either::Either;
30+
use hashlink::LruCache;
3131
use libp2p_core::{
3232
connection::ConnectedPoint, multiaddr::Protocol, transport::PortUse, Endpoint, Multiaddr,
3333
};
@@ -38,7 +38,6 @@ use libp2p_swarm::{
3838
dummy, ConnectionDenied, ConnectionHandler, ConnectionId, NetworkBehaviour,
3939
NewExternalAddrCandidate, NotifyHandler, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
4040
};
41-
use lru::LruCache;
4241
use thiserror::Error;
4342

4443
use crate::{handler, protocol};
@@ -361,7 +360,7 @@ struct Candidates {
361360
impl Candidates {
362361
fn new(me: PeerId) -> Self {
363362
Self {
364-
inner: LruCache::new(NonZeroUsize::new(20).expect("20 > 0")),
363+
inner: LruCache::new(20),
365364
me,
366365
}
367366
}
@@ -375,7 +374,7 @@ impl Candidates {
375374
address.push(Protocol::P2p(self.me));
376375
}
377376

378-
self.inner.push(address, ());
377+
self.inner.insert(address, ());
379378
}
380379

381380
fn iter(&self) -> impl Iterator<Item = &Multiaddr> {

swarm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ futures = { workspace = true }
1717
futures-timer = "3.0.3"
1818
getrandom = { workspace = true, features = ["js"], optional = true } # Explicit dependency to be used in `wasm-bindgen` feature
1919
web-time = { workspace = true }
20+
hashlink = { workspace = true }
2021
libp2p-core = { workspace = true }
2122
libp2p-identity = { workspace = true }
2223
libp2p-swarm-derive = { workspace = true, optional = true }
23-
lru = "0.12.3"
2424
multistream-select = { workspace = true }
2525
rand = "0.8"
2626
smallvec = "1.13.2"

swarm/src/behaviour/peer_addresses.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::num::NonZeroUsize;
22

3+
use hashlink::LruCache;
34
use libp2p_core::Multiaddr;
45
use libp2p_identity::PeerId;
5-
use lru::LruCache;
66

77
use crate::{behaviour::FromSwarm, DialError, DialFailure, NewExternalAddrOfPeer};
88

@@ -15,7 +15,7 @@ impl PeerAddresses {
1515
///
1616
/// For each peer, we will at most store 10 addresses.
1717
pub fn new(number_of_peers: NonZeroUsize) -> Self {
18-
Self(LruCache::new(number_of_peers))
18+
Self(LruCache::new(number_of_peers.get()))
1919
}
2020

2121
/// Feed a [`FromSwarm`] event to this struct.
@@ -48,11 +48,11 @@ impl PeerAddresses {
4848
match prepare_addr(&peer, &address) {
4949
Ok(address) => {
5050
if let Some(cached) = self.0.get_mut(&peer) {
51-
cached.put(address, ()).is_none()
51+
cached.insert(address, ()).is_none()
5252
} else {
53-
let mut set = LruCache::new(NonZeroUsize::new(10).expect("10 > 0"));
54-
set.put(address, ());
55-
self.0.put(peer, set);
53+
let mut set = LruCache::new(10);
54+
set.insert(address, ());
55+
self.0.insert(peer, set);
5656

5757
true
5858
}
@@ -75,7 +75,7 @@ impl PeerAddresses {
7575
pub fn remove(&mut self, peer: &PeerId, address: &Multiaddr) -> bool {
7676
match self.0.get_mut(peer) {
7777
Some(addrs) => match prepare_addr(peer, address) {
78-
Ok(address) => addrs.pop(&address).is_some(),
78+
Ok(address) => addrs.remove(&address).is_some(),
7979
Err(_) => false,
8080
},
8181
None => false,
@@ -89,7 +89,7 @@ fn prepare_addr(peer: &PeerId, addr: &Multiaddr) -> Result<Multiaddr, Multiaddr>
8989

9090
impl Default for PeerAddresses {
9191
fn default() -> Self {
92-
Self(LruCache::new(NonZeroUsize::new(100).unwrap()))
92+
Self(LruCache::new(100))
9393
}
9494
}
9595

0 commit comments

Comments
 (0)