Skip to content

Commit

Permalink
deps: replace lru with schnellru (paritytech#1217)
Browse files Browse the repository at this point in the history
* deps: replace lru with schnellru

* bring the peace to the galaxy
  • Loading branch information
ordian authored Aug 28, 2023
1 parent 7125f65 commit c168a77
Show file tree
Hide file tree
Showing 32 changed files with 208 additions and 242 deletions.
27 changes: 12 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cumulus/client/consensus/aura/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async-trait = "0.1.73"
codec = { package = "parity-scale-codec", version = "3.0.0", features = [ "derive" ] }
futures = "0.3.28"
tracing = "0.1.37"
lru = "0.10.0"
schnellru = "0.2.1"

# Substrate
sc-client-api = { path = "../../../../substrate/client/api" }
Expand Down
17 changes: 9 additions & 8 deletions cumulus/client/consensus/aura/src/equivocation_import_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/// should be thrown out and which ones should be kept.
use codec::Codec;
use cumulus_client_consensus_common::ParachainBlockImportMarker;
use lru::LruCache;
use schnellru::{ByLength, LruMap};

use sc_consensus::{
import_queue::{BasicQueue, Verifier as VerifierT},
Expand All @@ -36,27 +36,28 @@ use sp_consensus_aura::{AuraApi, Slot, SlotDuration};
use sp_core::crypto::Pair;
use sp_inherents::{CreateInherentDataProviders, InherentDataProvider};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use std::{fmt::Debug, num::NonZeroUsize, sync::Arc};
use std::{fmt::Debug, sync::Arc};

const LRU_WINDOW: usize = 256;
const LRU_WINDOW: u32 = 256;
const EQUIVOCATION_LIMIT: usize = 16;

struct NaiveEquivocationDefender {
cache: LruCache<u64, usize>,
cache: LruMap<u64, usize>,
}

impl Default for NaiveEquivocationDefender {
fn default() -> Self {
NaiveEquivocationDefender {
cache: LruCache::new(NonZeroUsize::new(LRU_WINDOW).expect("window > 0; qed")),
}
NaiveEquivocationDefender { cache: LruMap::new(ByLength::new(LRU_WINDOW)) }
}
}

impl NaiveEquivocationDefender {
// return `true` if equivocation is beyond the limit.
fn insert_and_check(&mut self, slot: Slot) -> bool {
let val = self.cache.get_or_insert_mut(*slot, || 0);
let val = self
.cache
.get_or_insert(*slot, || 0)
.expect("insertion with ByLength limiter always succeeds; qed");
if *val == EQUIVOCATION_LIMIT {
true
} else {
Expand Down
2 changes: 1 addition & 1 deletion cumulus/client/relay-chain-minimal-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ cumulus-relay-chain-rpc-interface = { path = "../relay-chain-rpc-interface" }
cumulus-primitives-core = { path = "../../primitives/core" }

array-bytes = "6.1"
lru = "0.11.0"
schnellru = "0.2.1"
tracing = "0.1.37"
async-trait = "0.1.73"
futures = "0.3.28"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use futures::{select, StreamExt};
use lru::LruCache;
use schnellru::{ByLength, LruMap};
use std::sync::Arc;

use polkadot_availability_recovery::AvailabilityRecoverySubsystem;
Expand Down Expand Up @@ -157,7 +157,7 @@ fn build_overseer(
.span_per_active_leaf(Default::default())
.active_leaves(Default::default())
.supports_parachains(runtime_client)
.known_leaves(LruCache::new(KNOWN_LEAVES_CACHE_SIZE))
.known_leaves(LruMap::new(ByLength::new(KNOWN_LEAVES_CACHE_SIZE)))
.metrics(Metrics::register(registry)?)
.spawner(spawner);

Expand Down
2 changes: 1 addition & 1 deletion cumulus/client/relay-chain-rpc-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async-trait = "0.1.73"
url = "2.4.0"
serde_json = "1.0.105"
serde = "1.0.183"
lru = "0.11.0"
schnellru = "0.2.1"
smoldot = { version = "0.11.0", default_features = false, features = ["std"]}
smoldot-light = { version = "0.9.0", default_features = false, features = ["std"] }
either = "1.8.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ use jsonrpsee::{
},
ws_client::WsClientBuilder,
};
use lru::LruCache;
use sc_rpc_api::chain::ChainApiClient;
use schnellru::{ByLength, LruMap};
use sp_runtime::generic::SignedBlock;
use std::{num::NonZeroUsize, sync::Arc};
use std::sync::Arc;
use tokio::sync::mpsc::{
channel as tokio_channel, Receiver as TokioReceiver, Sender as TokioSender,
};
Expand Down Expand Up @@ -307,8 +307,7 @@ impl ReconnectingWebsocketWorker {
return
};

let mut imported_blocks_cache =
LruCache::new(NonZeroUsize::new(40).expect("40 is nonzero; qed."));
let mut imported_blocks_cache = LruMap::new(ByLength::new(40));
let mut should_reconnect = ConnectionStatus::Connected;
let mut last_seen_finalized_num: RelayNumber = 0;
loop {
Expand Down Expand Up @@ -365,7 +364,7 @@ impl ReconnectingWebsocketWorker {
match import_event {
Some(Ok(header)) => {
let hash = header.hash();
if imported_blocks_cache.contains(&hash) {
if imported_blocks_cache.peek(&hash).is_some() {
tracing::debug!(
target: LOG_TARGET,
number = header.number,
Expand All @@ -374,7 +373,7 @@ impl ReconnectingWebsocketWorker {
);
continue;
}
imported_blocks_cache.put(hash, ());
imported_blocks_cache.insert(hash, ());
distribute_header(header, &mut self.imported_header_listeners);
},
None => {
Expand Down
2 changes: 1 addition & 1 deletion polkadot/node/core/approval-voting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ futures-timer = "3.0.2"
parity-scale-codec = { version = "3.6.1", default-features = false, features = ["bit-vec", "derive"] }
gum = { package = "tracing-gum", path = "../../gum" }
bitvec = { version = "1.0.0", default-features = false, features = ["alloc"] }
lru = "0.11.0"
schnellru = "0.2.1"
merlin = "2.0"
schnorrkel = "0.9.1"
kvdb = "0.13.0"
Expand Down
10 changes: 5 additions & 5 deletions polkadot/node/core/approval-voting/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ pub(crate) mod tests {
blank_state(),
RuntimeInfo::new_with_config(RuntimeInfoConfig {
keystore: None,
session_cache_lru_size: DISPUTE_WINDOW.into(),
session_cache_lru_size: DISPUTE_WINDOW.get(),
}),
)
}
Expand Down Expand Up @@ -755,7 +755,7 @@ pub(crate) mod tests {

let mut runtime_info = RuntimeInfo::new_with_config(RuntimeInfoConfig {
keystore: None,
session_cache_lru_size: DISPUTE_WINDOW.into(),
session_cache_lru_size: DISPUTE_WINDOW.get(),
});

let header = header.clone();
Expand Down Expand Up @@ -878,7 +878,7 @@ pub(crate) mod tests {
let test_fut = {
let mut runtime_info = RuntimeInfo::new_with_config(RuntimeInfoConfig {
keystore: None,
session_cache_lru_size: DISPUTE_WINDOW.into(),
session_cache_lru_size: DISPUTE_WINDOW.get(),
});

let header = header.clone();
Expand Down Expand Up @@ -994,7 +994,7 @@ pub(crate) mod tests {
let test_fut = {
let mut runtime_info = RuntimeInfo::new_with_config(RuntimeInfoConfig {
keystore: None,
session_cache_lru_size: DISPUTE_WINDOW.into(),
session_cache_lru_size: DISPUTE_WINDOW.get(),
});

let header = header.clone();
Expand Down Expand Up @@ -1092,7 +1092,7 @@ pub(crate) mod tests {

let mut runtime_info = RuntimeInfo::new_with_config(RuntimeInfoConfig {
keystore: None,
session_cache_lru_size: DISPUTE_WINDOW.into(),
session_cache_lru_size: DISPUTE_WINDOW.get(),
});

let header = header.clone();
Expand Down
Loading

0 comments on commit c168a77

Please sign in to comment.