diff --git a/Cargo.lock b/Cargo.lock index 36f2d1f96e..c380de1136 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2214,20 +2214,6 @@ dependencies = [ "syn 2.0.85", ] -[[package]] -name = "dashmap" -version = "6.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf" -dependencies = [ - "cfg-if", - "crossbeam-utils", - "hashbrown 0.14.5", - "lock_api", - "once_cell", - "parking_lot_core", -] - [[package]] name = "data-encoding" version = "2.6.0" @@ -3570,7 +3556,6 @@ dependencies = [ "anyhow", "async-trait", "ctor", - "dashmap", "fuel-core-chain-config", "fuel-core-metrics", "fuel-core-p2p", @@ -3589,6 +3574,7 @@ dependencies = [ "prometheus-client", "quick-protobuf", "quick-protobuf-codec 0.3.1", + "quick_cache", "rand", "rayon", "serde", @@ -7364,6 +7350,18 @@ dependencies = [ "unsigned-varint 0.8.0", ] +[[package]] +name = "quick_cache" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d7c94f8935a9df96bb6380e8592c70edf497a643f94bd23b2f76b399385dbf4" +dependencies = [ + "ahash", + "equivalent", + "hashbrown 0.14.5", + "parking_lot", +] + [[package]] name = "quinn" version = "0.11.5" diff --git a/crates/services/p2p/Cargo.toml b/crates/services/p2p/Cargo.toml index aeab90bc95..da5e55d3f9 100644 --- a/crates/services/p2p/Cargo.toml +++ b/crates/services/p2p/Cargo.toml @@ -13,7 +13,6 @@ description = "Fuel client networking" [dependencies] anyhow = { workspace = true } async-trait = { workspace = true } -dashmap = "6.1.0" fuel-core-chain-config = { workspace = true } fuel-core-metrics = { workspace = true } # TODO make this a feature fuel-core-services = { workspace = true, features = ["sync-processor"] } @@ -42,6 +41,7 @@ libp2p = { version = "0.53.2", default-features = false, features = [ libp2p-mplex = "0.41.0" postcard = { workspace = true, features = ["use-std"] } prometheus-client = { workspace = true } +quick_cache = "0.6.9" quick-protobuf = "0.8.1" quick-protobuf-codec = "0.3.0" rand = { workspace = true } diff --git a/crates/services/p2p/src/cached_view.rs b/crates/services/p2p/src/cached_view.rs index b042ee5b9c..b641c5a1eb 100644 --- a/crates/services/p2p/src/cached_view.rs +++ b/crates/services/p2p/src/cached_view.rs @@ -1,5 +1,4 @@ use crate::ports::P2pDb; -use dashmap::DashMap; use fuel_core_metrics::p2p_metrics::{ increment_p2p_req_res_cache_hits, increment_p2p_req_res_cache_misses, @@ -9,78 +8,25 @@ use fuel_core_types::{ blockchain::SealedBlockHeader, services::p2p::Transactions, }; +use quick_cache::sync::Cache; use std::{ - collections::VecDeque, - hash::Hash, ops::Range, - sync::{ - Arc, - Mutex, - }, + sync::Arc, }; type BlockHeight = u32; -struct LruCache { - cache: DashMap>, - order: Mutex>, - capacity: usize, -} - -impl LruCache -where - K: Eq + Hash + Clone, -{ - fn new(capacity: usize) -> Self { - Self { - cache: DashMap::new(), - order: Mutex::new(VecDeque::new()), - capacity, - } - } - - fn insert(&self, key: K, value: V) { - let mut order = self.order.lock().expect("Poisoned lock"); - - if self.cache.len() >= self.capacity { - if let Some(least_used) = order.pop_front() { - self.cache.remove(&least_used); - } - } - - self.cache.insert(key.clone(), Arc::new(value)); - - // Update the access order. - order.retain(|k| k != &key); - order.push_back(key); - } - - fn get(&self, key: &K) -> Option> { - let mut order = self.order.lock().expect("Poisoned lock"); - - if let Some(value) = self.cache.get(key) { - // Update the access order. - order.retain(|k| k != key); - order.push_back(key.clone()); - - Some(Arc::clone(&value)) - } else { - None - } - } -} - pub(super) struct CachedView { - sealed_block_headers: LruCache, - transactions_on_blocks: LruCache, + sealed_block_headers: Cache, + transactions_on_blocks: Cache, metrics: bool, } impl CachedView { pub fn new(capacity: usize, metrics: bool) -> Self { Self { - sealed_block_headers: LruCache::new(capacity), - transactions_on_blocks: LruCache::new(capacity), + sealed_block_headers: Cache::new(capacity), + transactions_on_blocks: Cache::new(capacity), metrics, } } @@ -96,7 +42,7 @@ impl CachedView { fn get_from_cache_or_db( &self, - cache: &LruCache, + cache: &Cache, view: &V, range: Range, fetch_fn: F, @@ -111,7 +57,7 @@ impl CachedView { for height in range.clone() { if let Some(item) = cache.get(&height) { - items.push(item.clone()); + items.push(item.clone().into()); } else { missing_start = Some(height); break;