From c0465a2fb2ccf89810f28e18ac74d38b1ca995b8 Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Tue, 10 Jan 2023 05:35:37 +0100 Subject: [PATCH] Bump dependency on lru to from version 0.7.5 to version 0.9.0. (#1755) --- Cargo.toml | 2 +- src/store/reader.rs | 31 ++++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 44c8a52786..29d778154d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,7 +49,7 @@ murmurhash32 = "0.2.0" time = { version = "0.3.10", features = ["serde-well-known"] } smallvec = "1.8.0" rayon = "1.5.2" -lru = "0.7.5" +lru = "0.9.0" fastdivide = "0.4.0" itertools = "0.10.3" measure_time = "0.8.2" diff --git a/src/store/reader.rs b/src/store/reader.rs index cc20de184a..32319c8c18 100644 --- a/src/store/reader.rs +++ b/src/store/reader.rs @@ -1,5 +1,6 @@ use std::io; use std::iter::Sum; +use std::num::NonZeroUsize; use std::ops::{AddAssign, Range}; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; @@ -33,23 +34,29 @@ pub struct StoreReader { /// The cache for decompressed blocks. struct BlockCache { - cache: Mutex>, - cache_hits: Arc, - cache_misses: Arc, + cache: Option>>, + cache_hits: AtomicUsize, + cache_misses: AtomicUsize, } impl BlockCache { fn get_from_cache(&self, pos: usize) -> Option { - if let Some(block) = self.cache.lock().unwrap().get(&pos) { + if let Some(block) = self + .cache + .as_ref() + .and_then(|cache| cache.lock().unwrap().get(&pos).cloned()) + { self.cache_hits.fetch_add(1, Ordering::SeqCst); - return Some(block.clone()); + return Some(block); } self.cache_misses.fetch_add(1, Ordering::SeqCst); None } fn put_into_cache(&self, pos: usize, data: Block) { - self.cache.lock().unwrap().put(pos, data); + if let Some(cache) = self.cache.as_ref() { + cache.lock().unwrap().put(pos, data); + } } fn stats(&self) -> CacheStats { @@ -59,13 +66,18 @@ impl BlockCache { num_entries: self.len(), } } + fn len(&self) -> usize { - self.cache.lock().unwrap().len() + self.cache + .as_ref() + .map_or(0, |cache| cache.lock().unwrap().len()) } #[cfg(test)] fn peek_lru(&self) -> Option { - self.cache.lock().unwrap().peek_lru().map(|(&k, _)| k) + self.cache + .as_ref() + .and_then(|cache| cache.lock().unwrap().peek_lru().map(|(&k, _)| k)) } } @@ -113,7 +125,8 @@ impl StoreReader { decompressor: footer.decompressor, data: data_file, cache: BlockCache { - cache: Mutex::new(LruCache::new(cache_size)), + cache: NonZeroUsize::new(cache_size) + .map(|cache_size| Mutex::new(LruCache::new(cache_size))), cache_hits: Default::default(), cache_misses: Default::default(), },