replace lru with schnellru (#14539)

This commit is contained in:
Qinxuan Chen
2023-07-10 05:24:23 +08:00
committed by GitHub
parent 6dd625d568
commit 6d2c1ed719
11 changed files with 58 additions and 71 deletions
@@ -26,8 +26,8 @@ use codec::{Decode, Encode};
use futures::{channel::oneshot, stream::StreamExt};
use libp2p::PeerId;
use log::debug;
use lru::LruCache;
use prost::Message;
use schnellru::{ByLength, LruMap};
use sc_client_api::BlockBackend;
use sc_network::{
@@ -44,7 +44,6 @@ use sp_runtime::{
use std::{
cmp::min,
hash::{Hash, Hasher},
num::NonZeroUsize,
sync::Arc,
time::Duration,
};
@@ -137,7 +136,7 @@ pub struct BlockRequestHandler<B: BlockT, Client> {
/// Maps from request to number of times we have seen this request.
///
/// This is used to check if a peer is spamming us with the same request.
seen_requests: LruCache<SeenRequestsKey<B>, SeenRequestsValue>,
seen_requests: LruMap<SeenRequestsKey<B>, SeenRequestsValue>,
}
impl<B, Client> BlockRequestHandler<B, Client>
@@ -167,9 +166,8 @@ where
);
protocol_config.inbound_queue = Some(tx);
let capacity =
NonZeroUsize::new(num_peer_hint.max(1) * 2).expect("cache capacity is not zero");
let seen_requests = LruCache::new(capacity);
let capacity = ByLength::new(num_peer_hint.max(1) as u32 * 2);
let seen_requests = LruMap::new(capacity);
(Self { client, request_receiver, seen_requests }, protocol_config)
}
@@ -236,7 +234,7 @@ where
.difference(BlockAttributes::HEADER | BlockAttributes::JUSTIFICATION)
.is_empty();
match self.seen_requests.get_mut(&key) {
match self.seen_requests.get(&key) {
Some(SeenRequestsValue::First) => {},
Some(SeenRequestsValue::Fulfilled(ref mut requests)) => {
*requests = requests.saturating_add(1);
@@ -250,7 +248,7 @@ where
}
},
None => {
self.seen_requests.put(key.clone(), SeenRequestsValue::First);
self.seen_requests.insert(key.clone(), SeenRequestsValue::First);
},
}
@@ -277,7 +275,7 @@ where
.iter()
.any(|b| !b.header.is_empty() || !b.body.is_empty() || b.is_empty_justification)
{
if let Some(value) = self.seen_requests.get_mut(&key) {
if let Some(value) = self.seen_requests.get(&key) {
// If this is the first time we have processed this request, we need to change
// it to `Fulfilled`.
if let SeenRequestsValue::First = value {
+7 -10
View File
@@ -28,10 +28,10 @@ use codec::{Decode, Encode};
use futures::{FutureExt, StreamExt};
use futures_timer::Delay;
use libp2p::PeerId;
use lru::LruCache;
use prometheus_endpoint::{
register, Gauge, GaugeVec, MetricSource, Opts, PrometheusError, Registry, SourcedGauge, U64,
};
use schnellru::{ByLength, LruMap};
use sc_client_api::{BlockBackend, HeaderBackend, ProofProvider};
use sc_consensus::import_queue::ImportQueueService;
@@ -239,7 +239,7 @@ pub struct SyncingEngine<B: BlockT, Client> {
default_peers_set_num_light: usize,
/// A cache for the data that was associated to a block announcement.
block_announce_data_cache: LruCache<B::Hash, Vec<u8>>,
block_announce_data_cache: LruMap<B::Hash, Vec<u8>>,
/// The `PeerId`'s of all boot nodes.
boot_node_ids: HashSet<PeerId>,
@@ -294,12 +294,9 @@ where
} else {
net_config.network_config.max_blocks_per_request
};
let cache_capacity = NonZeroUsize::new(
(net_config.network_config.default_peers_set.in_peers as usize +
net_config.network_config.default_peers_set.out_peers as usize)
.max(1),
)
.expect("cache capacity is not zero");
let cache_capacity = (net_config.network_config.default_peers_set.in_peers +
net_config.network_config.default_peers_set.out_peers)
.max(1);
let important_peers = {
let mut imp_p = HashSet::new();
for reserved in &net_config.network_config.default_peers_set.reserved_nodes {
@@ -381,7 +378,7 @@ where
network_service,
peers: HashMap::new(),
evicted: HashSet::new(),
block_announce_data_cache: LruCache::new(cache_capacity),
block_announce_data_cache: LruMap::new(ByLength::new(cache_capacity)),
block_announce_protocol_name,
num_connected: num_connected.clone(),
is_major_syncing: is_major_syncing.clone(),
@@ -465,7 +462,7 @@ where
if let Some(data) = announce.data {
if !data.is_empty() {
self.block_announce_data_cache.put(announce.header.hash(), data);
self.block_announce_data_cache.insert(announce.header.hash(), data);
}
}
},
@@ -23,8 +23,8 @@ use codec::{Decode, Encode};
use futures::{channel::oneshot, stream::StreamExt};
use libp2p::PeerId;
use log::{debug, trace};
use lru::LruCache;
use prost::Message;
use schnellru::{ByLength, LruMap};
use sc_client_api::{BlockBackend, ProofProvider};
use sc_network::{
@@ -35,7 +35,6 @@ use sp_runtime::traits::Block as BlockT;
use std::{
hash::{Hash, Hasher},
num::NonZeroUsize,
sync::Arc,
time::Duration,
};
@@ -115,7 +114,7 @@ pub struct StateRequestHandler<B: BlockT, Client> {
/// Maps from request to number of times we have seen this request.
///
/// This is used to check if a peer is spamming us with the same request.
seen_requests: LruCache<SeenRequestsKey<B>, SeenRequestsValue>,
seen_requests: LruMap<SeenRequestsKey<B>, SeenRequestsValue>,
}
impl<B, Client> StateRequestHandler<B, Client>
@@ -145,9 +144,8 @@ where
);
protocol_config.inbound_queue = Some(tx);
let capacity =
NonZeroUsize::new(num_peer_hint.max(1) * 2).expect("cache capacity is not zero");
let seen_requests = LruCache::new(capacity);
let capacity = ByLength::new(num_peer_hint.max(1) as u32 * 2);
let seen_requests = LruMap::new(capacity);
(Self { client, request_receiver, seen_requests }, protocol_config)
}
@@ -180,7 +178,7 @@ where
let mut reputation_changes = Vec::new();
match self.seen_requests.get_mut(&key) {
match self.seen_requests.get(&key) {
Some(SeenRequestsValue::First) => {},
Some(SeenRequestsValue::Fulfilled(ref mut requests)) => {
*requests = requests.saturating_add(1);
@@ -190,7 +188,7 @@ where
}
},
None => {
self.seen_requests.put(key.clone(), SeenRequestsValue::First);
self.seen_requests.insert(key.clone(), SeenRequestsValue::First);
},
}
@@ -247,7 +245,7 @@ where
.last()
.map(|e| sp_core::hexdisplay::HexDisplay::from(&e.key))),
);
if let Some(value) = self.seen_requests.get_mut(&key) {
if let Some(value) = self.seen_requests.get(&key) {
// If this is the first time we have processed this request, we need to change
// it to `Fulfilled`.
if let SeenRequestsValue::First = value {