gossip: replace LRU cache (#4137)

* peerset: remove unused dependencies

* gossip: replace lru cache

* header-metadata: replace lru cache
This commit is contained in:
André Silva
2019-11-19 09:58:06 +00:00
committed by Robert Habermeier
parent 10925510de
commit 9194b2a3c0
6 changed files with 21 additions and 25 deletions
+6 -8
View File
@@ -2470,11 +2470,11 @@ dependencies = [
]
[[package]]
name = "lru-cache"
version = "0.1.2"
name = "lru"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"hashbrown 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -5792,7 +5792,7 @@ dependencies = [
name = "substrate-header-metadata"
version = "2.0.0"
dependencies = [
"lru-cache 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"lru 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 2.0.0",
]
@@ -5853,7 +5853,7 @@ dependencies = [
"linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"linked_hash_set 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"lru-cache 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"lru 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"quickcheck 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -5939,9 +5939,7 @@ version = "2.0.0"
dependencies = [
"futures-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)",
"libp2p 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
"linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"lru-cache 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -7850,7 +7848,7 @@ dependencies = [
"checksum lock_api 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f8912e782533a93a167888781b836336a6ca5da6175c05944c86cf28c31104dc"
"checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b"
"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
"checksum lru-cache 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c"
"checksum lru 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "26b0dca4ac5b5083c5169ab12205e6473df1c7659940e4978b94f363c6b54b22"
"checksum mach 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "86dd2487cdfea56def77b88438a2c915fb45113c5319bfe7e14306ca4cd0b0e1"
"checksum malloc_size_of_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e37c5d4cd9473c5f4c9c111f033f15d4df9bd378fdf615944e360a4f55a05f0b"
"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
+1 -1
View File
@@ -5,6 +5,6 @@ authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
lru-cache = { version = "0.1.2" }
lru = { version = "0.4.0" }
parking_lot = { version = "0.9.0" }
sr-primitives = { path = "../../primitives/sr-primitives" }
+4 -4
View File
@@ -19,7 +19,7 @@
use sr_primitives::traits::{Block as BlockT, NumberFor, Header};
use parking_lot::RwLock;
use lru_cache::LruCache;
use lru::LruCache;
/// Set to the expected max difference between `best` and `finalized` blocks at sync.
const LRU_CACHE_SIZE: usize = 5_000;
@@ -243,16 +243,16 @@ impl<Block: BlockT> HeaderMetadata<Block> for HeaderMetadataCache<Block> {
type Error = String;
fn header_metadata(&self, hash: Block::Hash) -> Result<CachedHeaderMetadata<Block>, Self::Error> {
self.cache.write().get_mut(&hash).cloned()
self.cache.write().get(&hash).cloned()
.ok_or("header metadata not found in cache".to_owned())
}
fn insert_header_metadata(&self, hash: Block::Hash, metadata: CachedHeaderMetadata<Block>) {
self.cache.write().insert(hash, metadata);
self.cache.write().put(hash, metadata);
}
fn remove_header_metadata(&self, hash: Block::Hash) {
self.cache.write().remove(&hash);
self.cache.write().pop(&hash);
}
}
+1 -1
View File
@@ -19,7 +19,7 @@ futures03 = { package = "futures-preview", version = "0.3.0-alpha.19", features
futures-timer = "0.4.0"
linked-hash-map = "0.5.2"
linked_hash_set = "0.1.3"
lru-cache = "0.1.2"
lru = "0.4.0"
rustc-hex = "2.0.1"
rand = "0.7.2"
libp2p = { version = "0.13.0", default-features = false, features = ["libp2p-websocket"] }
@@ -49,7 +49,7 @@ use std::iter;
use std::time;
use log::{trace, debug};
use futures03::channel::mpsc;
use lru_cache::LruCache;
use lru::LruCache;
use libp2p::PeerId;
use sr_primitives::traits::{Block as BlockT, Hash, HashFor};
use sr_primitives::ConsensusEngineId;
@@ -354,7 +354,7 @@ impl<B: BlockT> ConsensusGossip<B> {
message: ConsensusMessage,
sender: Option<PeerId>,
) {
if self.known_messages.insert(message_hash.clone(), ()).is_none() {
if self.known_messages.put(message_hash.clone(), ()).is_none() {
self.messages.push(MessageEntry {
message_hash,
topic,
@@ -447,8 +447,8 @@ impl<B: BlockT> ConsensusGossip<B> {
);
for (_, ref mut peer) in self.peers.iter_mut() {
peer.known_messages.retain(|h| known_messages.contains_key(h));
peer.filtered_messages.retain(|h, _| known_messages.contains_key(h));
peer.known_messages.retain(|h| known_messages.contains(h));
peer.filtered_messages.retain(|h, _| known_messages.contains(h));
}
}
@@ -486,7 +486,7 @@ impl<B: BlockT> ConsensusGossip<B> {
for message in messages {
let message_hash = HashFor::<B>::hash(&message.data[..]);
if self.known_messages.contains_key(&message_hash) {
if self.known_messages.contains(&message_hash) {
trace!(target:"gossip", "Ignored already known message from {}", who);
protocol.report_peer(who.clone(), DUPLICATE_GOSSIP_REPUTATION_CHANGE);
continue;
@@ -673,7 +673,7 @@ mod tests {
macro_rules! push_msg {
($consensus:expr, $topic:expr, $hash: expr, $m:expr) => {
if $consensus.known_messages.insert($hash, ()).is_none() {
if $consensus.known_messages.put($hash, ()).is_none() {
$consensus.messages.push(MessageEntry {
message_hash: $hash,
topic: $topic,
@@ -728,8 +728,8 @@ mod tests {
push_msg!(consensus, prev_hash, m1_hash, m1);
push_msg!(consensus, best_hash, m2_hash, m2);
consensus.known_messages.insert(m1_hash, ());
consensus.known_messages.insert(m2_hash, ());
consensus.known_messages.put(m1_hash, ());
consensus.known_messages.put(m2_hash, ());
let test_engine_id = Default::default();
consensus.register_validator_internal(test_engine_id, Arc::new(AllowAll));
@@ -744,7 +744,7 @@ mod tests {
assert_eq!(consensus.messages.len(), 1);
// known messages are only pruned based on size.
assert_eq!(consensus.known_messages.len(), 2);
assert!(consensus.known_messages.contains_key(&m2_hash));
assert!(consensus.known_messages.contains(&m2_hash));
}
#[test]
-2
View File
@@ -10,9 +10,7 @@ edition = "2018"
[dependencies]
futures-preview = "0.3.0-alpha.19"
libp2p = { version = "0.13.0", default-features = false }
linked-hash-map = "0.5.2"
log = "0.4.8"
lru-cache = "0.1.2"
serde_json = "1.0.41"
[dev-dependencies]