chore: replace lru with schnellru (#1688)

This commit is contained in:
Matthias Seitz
2023-03-09 16:20:40 +01:00
committed by GitHub
parent d164a16be7
commit 1e4ab0e1ac
4 changed files with 13 additions and 9 deletions

2
Cargo.lock generated
View File

@ -4487,12 +4487,12 @@ dependencies = [
"data-encoding", "data-encoding",
"enr", "enr",
"linked_hash_set", "linked_hash_set",
"lru 0.9.0",
"parking_lot 0.12.1", "parking_lot 0.12.1",
"reth-net-common", "reth-net-common",
"reth-primitives", "reth-primitives",
"reth-rlp", "reth-rlp",
"reth-tracing", "reth-tracing",
"schnellru",
"secp256k1 0.24.3", "secp256k1 0.24.3",
"serde", "serde",
"serde_with", "serde_with",

View File

@ -32,7 +32,7 @@ trust-dns-resolver = "0.22"
data-encoding = "2" data-encoding = "2"
async-trait = "0.1" async-trait = "0.1"
linked_hash_set = "0.1" linked_hash_set = "0.1"
lru = "0.9" schnellru = "0.2"
thiserror = "1.0" thiserror = "1.0"
tracing = "0.1" tracing = "0.1"
parking_lot = "0.12" parking_lot = "0.12"

View File

@ -1,5 +1,9 @@
use crate::tree::LinkEntry; use crate::tree::LinkEntry;
use std::{collections::HashSet, num::NonZeroUsize, time::Duration}; use std::{
collections::HashSet,
num::{NonZeroU32, NonZeroUsize},
time::Duration,
};
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -21,7 +25,7 @@ pub struct DnsDiscoveryConfig {
/// Default: 30min /// Default: 30min
pub recheck_interval: Duration, pub recheck_interval: Duration,
/// Maximum number of cached DNS records. /// Maximum number of cached DNS records.
pub dns_record_cache_limit: NonZeroUsize, pub dns_record_cache_limit: NonZeroU32,
/// Links to the DNS networks to bootstrap. /// Links to the DNS networks to bootstrap.
pub bootstrap_dns_networks: Option<HashSet<LinkEntry>>, pub bootstrap_dns_networks: Option<HashSet<LinkEntry>>,
} }
@ -32,7 +36,7 @@ impl Default for DnsDiscoveryConfig {
lookup_timeout: Duration::from_secs(5), lookup_timeout: Duration::from_secs(5),
max_requests_per_sec: NonZeroUsize::new(3).unwrap(), max_requests_per_sec: NonZeroUsize::new(3).unwrap(),
recheck_interval: Duration::from_secs(60 * 30), recheck_interval: Duration::from_secs(60 * 30),
dns_record_cache_limit: NonZeroUsize::new(1_000).unwrap(), dns_record_cache_limit: NonZeroU32::new(1_000).unwrap(),
bootstrap_dns_networks: Some(Default::default()), bootstrap_dns_networks: Some(Default::default()),
} }
} }

View File

@ -16,8 +16,8 @@ use crate::{
pub use config::DnsDiscoveryConfig; pub use config::DnsDiscoveryConfig;
use enr::Enr; use enr::Enr;
use error::ParseDnsEntryError; use error::ParseDnsEntryError;
use lru::LruCache;
use reth_primitives::{ForkId, NodeRecord, PeerId}; use reth_primitives::{ForkId, NodeRecord, PeerId};
use schnellru::{ByLength, LruMap};
use secp256k1::SecretKey; use secp256k1::SecretKey;
use std::{ use std::{
collections::{hash_map::Entry, HashMap, HashSet, VecDeque}, collections::{hash_map::Entry, HashMap, HashSet, VecDeque},
@ -95,7 +95,7 @@ pub struct DnsDiscoveryService<R: Resolver = DnsResolver> {
/// All queries currently in progress /// All queries currently in progress
queries: QueryPool<R, SecretKey>, queries: QueryPool<R, SecretKey>,
/// Cached dns records /// Cached dns records
dns_record_cache: LruCache<String, DnsEntry<SecretKey>>, dns_record_cache: LruMap<String, DnsEntry<SecretKey>>,
/// all buffered events /// all buffered events
queued_events: VecDeque<DnsDiscoveryEvent>, queued_events: VecDeque<DnsDiscoveryEvent>,
/// The rate at which trees should be updated. /// The rate at which trees should be updated.
@ -133,7 +133,7 @@ impl<R: Resolver> DnsDiscoveryService<R> {
node_record_listeners: Default::default(), node_record_listeners: Default::default(),
trees: Default::default(), trees: Default::default(),
queries, queries,
dns_record_cache: LruCache::new(dns_record_cache_limit), dns_record_cache: LruMap::new(ByLength::new(dns_record_cache_limit.get())),
queued_events: Default::default(), queued_events: Default::default(),
recheck_interval, recheck_interval,
bootstrap_dns_networks: bootstrap_dns_networks.unwrap_or_default(), bootstrap_dns_networks: bootstrap_dns_networks.unwrap_or_default(),
@ -250,7 +250,7 @@ impl<R: Resolver> DnsDiscoveryService<R> {
} }
Some(Ok(entry)) => { Some(Ok(entry)) => {
// cache entry // cache entry
self.dns_record_cache.push(hash.clone(), entry.clone()); self.dns_record_cache.insert(hash.clone(), entry.clone());
match entry { match entry {
DnsEntry::Root(root) => { DnsEntry::Root(root) => {