Skip to content

Commit c168a77

Browse files
author
ordian
authored
deps: replace lru with schnellru (#1217)
* deps: replace lru with schnellru * bring the peace to the galaxy
1 parent 7125f65 commit c168a77

File tree

32 files changed

+208
-242
lines changed

32 files changed

+208
-242
lines changed

Cargo.lock

Lines changed: 12 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cumulus/client/consensus/aura/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ async-trait = "0.1.73"
1010
codec = { package = "parity-scale-codec", version = "3.0.0", features = [ "derive" ] }
1111
futures = "0.3.28"
1212
tracing = "0.1.37"
13-
lru = "0.10.0"
13+
schnellru = "0.2.1"
1414

1515
# Substrate
1616
sc-client-api = { path = "../../../../substrate/client/api" }

cumulus/client/consensus/aura/src/equivocation_import_queue.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/// should be thrown out and which ones should be kept.
2222
use codec::Codec;
2323
use cumulus_client_consensus_common::ParachainBlockImportMarker;
24-
use lru::LruCache;
24+
use schnellru::{ByLength, LruMap};
2525

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

41-
const LRU_WINDOW: usize = 256;
41+
const LRU_WINDOW: u32 = 256;
4242
const EQUIVOCATION_LIMIT: usize = 16;
4343

4444
struct NaiveEquivocationDefender {
45-
cache: LruCache<u64, usize>,
45+
cache: LruMap<u64, usize>,
4646
}
4747

4848
impl Default for NaiveEquivocationDefender {
4949
fn default() -> Self {
50-
NaiveEquivocationDefender {
51-
cache: LruCache::new(NonZeroUsize::new(LRU_WINDOW).expect("window > 0; qed")),
52-
}
50+
NaiveEquivocationDefender { cache: LruMap::new(ByLength::new(LRU_WINDOW)) }
5351
}
5452
}
5553

5654
impl NaiveEquivocationDefender {
5755
// return `true` if equivocation is beyond the limit.
5856
fn insert_and_check(&mut self, slot: Slot) -> bool {
59-
let val = self.cache.get_or_insert_mut(*slot, || 0);
57+
let val = self
58+
.cache
59+
.get_or_insert(*slot, || 0)
60+
.expect("insertion with ByLength limiter always succeeds; qed");
6061
if *val == EQUIVOCATION_LIMIT {
6162
true
6263
} else {

cumulus/client/relay-chain-minimal-node/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ cumulus-relay-chain-rpc-interface = { path = "../relay-chain-rpc-interface" }
3636
cumulus-primitives-core = { path = "../../primitives/core" }
3737

3838
array-bytes = "6.1"
39-
lru = "0.11.0"
39+
schnellru = "0.2.1"
4040
tracing = "0.1.37"
4141
async-trait = "0.1.73"
4242
futures = "0.3.28"

cumulus/client/relay-chain-minimal-node/src/collator_overseer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use futures::{select, StreamExt};
18-
use lru::LruCache;
18+
use schnellru::{ByLength, LruMap};
1919
use std::sync::Arc;
2020

2121
use polkadot_availability_recovery::AvailabilityRecoverySubsystem;
@@ -157,7 +157,7 @@ fn build_overseer(
157157
.span_per_active_leaf(Default::default())
158158
.active_leaves(Default::default())
159159
.supports_parachains(runtime_client)
160-
.known_leaves(LruCache::new(KNOWN_LEAVES_CACHE_SIZE))
160+
.known_leaves(LruMap::new(ByLength::new(KNOWN_LEAVES_CACHE_SIZE)))
161161
.metrics(Metrics::register(registry)?)
162162
.spawner(spawner);
163163

cumulus/client/relay-chain-rpc-interface/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async-trait = "0.1.73"
3434
url = "2.4.0"
3535
serde_json = "1.0.105"
3636
serde = "1.0.183"
37-
lru = "0.11.0"
37+
schnellru = "0.2.1"
3838
smoldot = { version = "0.11.0", default_features = false, features = ["std"]}
3939
smoldot-light = { version = "0.9.0", default_features = false, features = ["std"] }
4040
either = "1.8.1"

cumulus/client/relay-chain-rpc-interface/src/reconnecting_ws_client.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ use jsonrpsee::{
3131
},
3232
ws_client::WsClientBuilder,
3333
};
34-
use lru::LruCache;
3534
use sc_rpc_api::chain::ChainApiClient;
35+
use schnellru::{ByLength, LruMap};
3636
use sp_runtime::generic::SignedBlock;
37-
use std::{num::NonZeroUsize, sync::Arc};
37+
use std::sync::Arc;
3838
use tokio::sync::mpsc::{
3939
channel as tokio_channel, Receiver as TokioReceiver, Sender as TokioSender,
4040
};
@@ -307,8 +307,7 @@ impl ReconnectingWebsocketWorker {
307307
return
308308
};
309309

310-
let mut imported_blocks_cache =
311-
LruCache::new(NonZeroUsize::new(40).expect("40 is nonzero; qed."));
310+
let mut imported_blocks_cache = LruMap::new(ByLength::new(40));
312311
let mut should_reconnect = ConnectionStatus::Connected;
313312
let mut last_seen_finalized_num: RelayNumber = 0;
314313
loop {
@@ -365,7 +364,7 @@ impl ReconnectingWebsocketWorker {
365364
match import_event {
366365
Some(Ok(header)) => {
367366
let hash = header.hash();
368-
if imported_blocks_cache.contains(&hash) {
367+
if imported_blocks_cache.peek(&hash).is_some() {
369368
tracing::debug!(
370369
target: LOG_TARGET,
371370
number = header.number,
@@ -374,7 +373,7 @@ impl ReconnectingWebsocketWorker {
374373
);
375374
continue;
376375
}
377-
imported_blocks_cache.put(hash, ());
376+
imported_blocks_cache.insert(hash, ());
378377
distribute_header(header, &mut self.imported_header_listeners);
379378
},
380379
None => {

polkadot/node/core/approval-voting/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ futures-timer = "3.0.2"
1111
parity-scale-codec = { version = "3.6.1", default-features = false, features = ["bit-vec", "derive"] }
1212
gum = { package = "tracing-gum", path = "../../gum" }
1313
bitvec = { version = "1.0.0", default-features = false, features = ["alloc"] }
14-
lru = "0.11.0"
14+
schnellru = "0.2.1"
1515
merlin = "2.0"
1616
schnorrkel = "0.9.1"
1717
kvdb = "0.13.0"

polkadot/node/core/approval-voting/src/import.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ pub(crate) mod tests {
643643
blank_state(),
644644
RuntimeInfo::new_with_config(RuntimeInfoConfig {
645645
keystore: None,
646-
session_cache_lru_size: DISPUTE_WINDOW.into(),
646+
session_cache_lru_size: DISPUTE_WINDOW.get(),
647647
}),
648648
)
649649
}
@@ -755,7 +755,7 @@ pub(crate) mod tests {
755755

756756
let mut runtime_info = RuntimeInfo::new_with_config(RuntimeInfoConfig {
757757
keystore: None,
758-
session_cache_lru_size: DISPUTE_WINDOW.into(),
758+
session_cache_lru_size: DISPUTE_WINDOW.get(),
759759
});
760760

761761
let header = header.clone();
@@ -878,7 +878,7 @@ pub(crate) mod tests {
878878
let test_fut = {
879879
let mut runtime_info = RuntimeInfo::new_with_config(RuntimeInfoConfig {
880880
keystore: None,
881-
session_cache_lru_size: DISPUTE_WINDOW.into(),
881+
session_cache_lru_size: DISPUTE_WINDOW.get(),
882882
});
883883

884884
let header = header.clone();
@@ -994,7 +994,7 @@ pub(crate) mod tests {
994994
let test_fut = {
995995
let mut runtime_info = RuntimeInfo::new_with_config(RuntimeInfoConfig {
996996
keystore: None,
997-
session_cache_lru_size: DISPUTE_WINDOW.into(),
997+
session_cache_lru_size: DISPUTE_WINDOW.get(),
998998
});
999999

10001000
let header = header.clone();
@@ -1092,7 +1092,7 @@ pub(crate) mod tests {
10921092

10931093
let mut runtime_info = RuntimeInfo::new_with_config(RuntimeInfoConfig {
10941094
keystore: None,
1095-
session_cache_lru_size: DISPUTE_WINDOW.into(),
1095+
session_cache_lru_size: DISPUTE_WINDOW.get(),
10961096
});
10971097

10981098
let header = header.clone();

0 commit comments

Comments
 (0)