Skip to content

Commit

Permalink
fix: replace dashmap with quick_cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Nov 18, 2024
1 parent 496071f commit 1bdcf00
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 78 deletions.
28 changes: 13 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 crates/services/p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down Expand Up @@ -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 }
Expand Down
70 changes: 8 additions & 62 deletions crates/services/p2p/src/cached_view.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<K, V> {
cache: DashMap<K, Arc<V>>,
order: Mutex<VecDeque<K>>,
capacity: usize,
}

impl<K, V> LruCache<K, V>
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<Arc<V>> {
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<BlockHeight, SealedBlockHeader>,
transactions_on_blocks: LruCache<BlockHeight, Transactions>,
sealed_block_headers: Cache<BlockHeight, SealedBlockHeader>,
transactions_on_blocks: Cache<BlockHeight, Transactions>,
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,
}
}
Expand All @@ -96,7 +42,7 @@ impl CachedView {

fn get_from_cache_or_db<V, T, F>(
&self,
cache: &LruCache<u32, T>,
cache: &Cache<u32, T>,
view: &V,
range: Range<u32>,
fetch_fn: F,
Expand All @@ -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;
Expand Down

0 comments on commit 1bdcf00

Please sign in to comment.