diff --git a/Cargo.lock b/Cargo.lock
index 339f650ad6ba..8581ec64c59f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4027,15 +4027,6 @@ dependencies = [
"parity-util-mem",
]
-[[package]]
-name = "memory-lru"
-version = "0.1.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ce95ae042940bad7e312857b929ee3d11b8f799a80cb7b9c7ec5125516906395"
-dependencies = [
- "lru",
-]
-
[[package]]
name = "memory_units"
version = "0.4.0"
@@ -6531,8 +6522,7 @@ name = "polkadot-node-core-runtime-api"
version = "0.9.31"
dependencies = [
"futures",
- "memory-lru",
- "parity-util-mem",
+ "lru",
"polkadot-node-primitives",
"polkadot-node-subsystem",
"polkadot-node-subsystem-test-helpers",
diff --git a/node/core/runtime-api/Cargo.toml b/node/core/runtime-api/Cargo.toml
index ff7ea662603c..8c941228cd95 100644
--- a/node/core/runtime-api/Cargo.toml
+++ b/node/core/runtime-api/Cargo.toml
@@ -7,8 +7,7 @@ edition = "2021"
[dependencies]
futures = "0.3.21"
gum = { package = "tracing-gum", path = "../../gum" }
-memory-lru = "0.1.1"
-parity-util-mem = { version = "0.12.0", default-features = false }
+lru = "0.8"
sp-consensus-babe = { git = "https://github.com/paritytech/substrate", branch = "master" }
diff --git a/node/core/runtime-api/src/cache.rs b/node/core/runtime-api/src/cache.rs
index 0fe9b74dc86d..d202b46d0da3 100644
--- a/node/core/runtime-api/src/cache.rs
+++ b/node/core/runtime-api/src/cache.rs
@@ -14,10 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see .
-use std::collections::btree_map::BTreeMap;
+use std::{collections::btree_map::BTreeMap, num::NonZeroUsize};
-use memory_lru::{MemoryLruCache, ResidentSize};
-use parity_util_mem::{MallocSizeOf, MallocSizeOfExt};
+use lru::LruCache;
use sp_consensus_babe::Epoch;
use polkadot_primitives::v2::{
@@ -28,126 +27,67 @@ use polkadot_primitives::v2::{
ValidationCodeHash, ValidatorId, ValidatorIndex, ValidatorSignature,
};
-const AUTHORITIES_CACHE_SIZE: usize = 128 * 1024;
-const VALIDATORS_CACHE_SIZE: usize = 64 * 1024;
-const VALIDATOR_GROUPS_CACHE_SIZE: usize = 64 * 1024;
-const AVAILABILITY_CORES_CACHE_SIZE: usize = 64 * 1024;
-const PERSISTED_VALIDATION_DATA_CACHE_SIZE: usize = 64 * 1024;
-const ASSUMED_VALIDATION_DATA_CACHE_SIZE: usize = 64 * 1024;
-const CHECK_VALIDATION_OUTPUTS_CACHE_SIZE: usize = 64 * 1024;
-const SESSION_INDEX_FOR_CHILD_CACHE_SIZE: usize = 64 * 1024;
-const VALIDATION_CODE_CACHE_SIZE: usize = 10 * 1024 * 1024;
-const CANDIDATE_PENDING_AVAILABILITY_CACHE_SIZE: usize = 64 * 1024;
-const CANDIDATE_EVENTS_CACHE_SIZE: usize = 64 * 1024;
-const SESSION_INFO_CACHE_SIZE: usize = 64 * 1024;
-const DMQ_CONTENTS_CACHE_SIZE: usize = 64 * 1024;
-const INBOUND_HRMP_CHANNELS_CACHE_SIZE: usize = 64 * 1024;
-const CURRENT_BABE_EPOCH_CACHE_SIZE: usize = 64 * 1024;
-const ON_CHAIN_VOTES_CACHE_SIZE: usize = 3 * 1024;
-const PVFS_REQUIRE_PRECHECK_SIZE: usize = 1024;
-const VALIDATION_CODE_HASH_CACHE_SIZE: usize = 64 * 1024;
-const VERSION_CACHE_SIZE: usize = 4 * 1024;
-const DISPUTES_CACHE_SIZE: usize = 64 * 1024;
-
-struct ResidentSizeOf(T);
-
-impl ResidentSize for ResidentSizeOf {
- fn resident_size(&self) -> usize {
- std::mem::size_of::() + self.0.malloc_size_of()
- }
-}
-
-struct DoesNotAllocate(T);
-
-impl ResidentSize for DoesNotAllocate {
- fn resident_size(&self) -> usize {
- std::mem::size_of::()
- }
-}
-
-// this is an ugly workaround for `AuthorityDiscoveryId`
-// not implementing `MallocSizeOf`
-struct VecOfDoesNotAllocate(Vec);
-
-impl ResidentSize for VecOfDoesNotAllocate {
- fn resident_size(&self) -> usize {
- std::mem::size_of::() * self.0.capacity()
- }
-}
+/// For consistency we have the same capacity for all caches. We use 128 as we'll only need that
+/// much if finality stalls (we only query state for unfinalized blocks + maybe latest finalized).
+/// In any case, a cache is an optimization. We should avoid a situation where having a large cache
+/// leads to OOM or puts pressure on other important stuff like PVF execution/preparation.
+const DEFAULT_CACHE_CAP: NonZeroUsize = match NonZeroUsize::new(128) {
+ Some(cap) => cap,
+ None => panic!("lru capacity must be non-zero"),
+};
pub(crate) struct RequestResultCache {
- authorities: MemoryLruCache>,
- validators: MemoryLruCache>>,
- validator_groups:
- MemoryLruCache>, GroupRotationInfo)>>,
- availability_cores: MemoryLruCache>>,
- persisted_validation_data: MemoryLruCache<
- (Hash, ParaId, OccupiedCoreAssumption),
- ResidentSizeOf