From 1e4ab0e1ac94c7898dd43d589bf25f9db7e7c3ac Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 9 Mar 2023 16:20:40 +0100 Subject: [PATCH] chore: replace lru with schnellru (#1688) --- Cargo.lock | 2 +- crates/net/dns/Cargo.toml | 2 +- crates/net/dns/src/config.rs | 10 +++++++--- crates/net/dns/src/lib.rs | 8 ++++---- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4607cebde..71b8ad0fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4487,12 +4487,12 @@ dependencies = [ "data-encoding", "enr", "linked_hash_set", - "lru 0.9.0", "parking_lot 0.12.1", "reth-net-common", "reth-primitives", "reth-rlp", "reth-tracing", + "schnellru", "secp256k1 0.24.3", "serde", "serde_with", diff --git a/crates/net/dns/Cargo.toml b/crates/net/dns/Cargo.toml index 80bea744c..f8f673fd7 100644 --- a/crates/net/dns/Cargo.toml +++ b/crates/net/dns/Cargo.toml @@ -32,7 +32,7 @@ trust-dns-resolver = "0.22" data-encoding = "2" async-trait = "0.1" linked_hash_set = "0.1" -lru = "0.9" +schnellru = "0.2" thiserror = "1.0" tracing = "0.1" parking_lot = "0.12" diff --git a/crates/net/dns/src/config.rs b/crates/net/dns/src/config.rs index 092ddbfce..6a31c36ad 100644 --- a/crates/net/dns/src/config.rs +++ b/crates/net/dns/src/config.rs @@ -1,5 +1,9 @@ use crate::tree::LinkEntry; -use std::{collections::HashSet, num::NonZeroUsize, time::Duration}; +use std::{ + collections::HashSet, + num::{NonZeroU32, NonZeroUsize}, + time::Duration, +}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -21,7 +25,7 @@ pub struct DnsDiscoveryConfig { /// Default: 30min pub recheck_interval: Duration, /// 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. pub bootstrap_dns_networks: Option>, } @@ -32,7 +36,7 @@ impl Default for DnsDiscoveryConfig { lookup_timeout: Duration::from_secs(5), max_requests_per_sec: NonZeroUsize::new(3).unwrap(), 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()), } } diff --git a/crates/net/dns/src/lib.rs b/crates/net/dns/src/lib.rs index 77ea3e876..82cea25df 100644 --- a/crates/net/dns/src/lib.rs +++ b/crates/net/dns/src/lib.rs @@ -16,8 +16,8 @@ use crate::{ pub use config::DnsDiscoveryConfig; use enr::Enr; use error::ParseDnsEntryError; -use lru::LruCache; use reth_primitives::{ForkId, NodeRecord, PeerId}; +use schnellru::{ByLength, LruMap}; use secp256k1::SecretKey; use std::{ collections::{hash_map::Entry, HashMap, HashSet, VecDeque}, @@ -95,7 +95,7 @@ pub struct DnsDiscoveryService { /// All queries currently in progress queries: QueryPool, /// Cached dns records - dns_record_cache: LruCache>, + dns_record_cache: LruMap>, /// all buffered events queued_events: VecDeque, /// The rate at which trees should be updated. @@ -133,7 +133,7 @@ impl DnsDiscoveryService { node_record_listeners: Default::default(), trees: Default::default(), 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(), recheck_interval, bootstrap_dns_networks: bootstrap_dns_networks.unwrap_or_default(), @@ -250,7 +250,7 @@ impl DnsDiscoveryService { } Some(Ok(entry)) => { // cache entry - self.dns_record_cache.push(hash.clone(), entry.clone()); + self.dns_record_cache.insert(hash.clone(), entry.clone()); match entry { DnsEntry::Root(root) => {