Skip to content

Commit

Permalink
Bump dependency on lru to from version 0.7.5 to version 0.9.0. (quick…
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold authored and Jamie Hodkinson committed Jan 30, 2023
1 parent 03308bd commit c0465a2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 22 additions & 9 deletions src/store/reader.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -33,23 +34,29 @@ pub struct StoreReader {

/// The cache for decompressed blocks.
struct BlockCache {
cache: Mutex<LruCache<usize, Block>>,
cache_hits: Arc<AtomicUsize>,
cache_misses: Arc<AtomicUsize>,
cache: Option<Mutex<LruCache<usize, Block>>>,
cache_hits: AtomicUsize,
cache_misses: AtomicUsize,
}

impl BlockCache {
fn get_from_cache(&self, pos: usize) -> Option<Block> {
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 {
Expand All @@ -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<usize> {
self.cache.lock().unwrap().peek_lru().map(|(&k, _)| k)
self.cache
.as_ref()
.and_then(|cache| cache.lock().unwrap().peek_lru().map(|(&k, _)| k))
}
}

Expand Down Expand Up @@ -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(),
},
Expand Down

0 comments on commit c0465a2

Please sign in to comment.