Skip to content

Commit

Permalink
Size-aware cache management
Browse files Browse the repository at this point in the history
Fix after merging the master branch. Note that added crossbeam-utils crate
to the dependency for `AtomicCell` (using for tracking weighted size).
  • Loading branch information
tatsuya6502 committed Dec 18, 2021
1 parent bd41c6d commit 4404e6f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 36 deletions.
17 changes: 3 additions & 14 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@
"CARGO_TARGET_DIR": "target/ra"
},
"cSpell.words": [
"CLFU",
"Deque",
"Deques",
"Einziger",
"Eytan",
"Hasher",
"Kawano",
"MSRV",
"Moka",
"Ohad",
"RUSTFLAGS",
"Ristretto",
"Tatsuya",
"Upsert",
"aarch",
"actix",
"ahash",
Expand All @@ -29,7 +15,9 @@
"deqs",
"Deque",
"Deques",
"Einziger",
"else's",
"Eytan",
"getrandom",
"Hasher",
"Kawano",
Expand All @@ -38,6 +26,7 @@
"MSRV",
"nanos",
"nocapture",
"Ohad",
"peekable",
"preds",
"reqwest",
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ atomic64 = []

[dependencies]
crossbeam-channel = "0.5"
crossbeam-utils = "0.8"
moka-cht = "0.4.2"
num_cpus = "1.13"
once_cell = "1.7"
Expand Down
5 changes: 2 additions & 3 deletions src/future/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ impl<K, V, C> CacheBuilder<K, V, C> {
mod tests {
use super::CacheBuilder;

use super::Cache;
use std::time::Duration;

#[tokio::test]
Expand Down Expand Up @@ -237,7 +236,7 @@ mod tests {
#[should_panic(expected = "time_to_live is longer than 1000 years")]
async fn build_cache_too_long_ttl() {
let thousand_years_secs: u64 = 1000 * 365 * 24 * 3600;
let builder: CacheBuilder<Cache<char, String>> = CacheBuilder::new(100);
let builder: CacheBuilder<char, String, _> = CacheBuilder::new(100);
let duration = Duration::from_secs(thousand_years_secs);
builder
.time_to_live(duration + Duration::from_secs(1))
Expand All @@ -248,7 +247,7 @@ mod tests {
#[should_panic(expected = "time_to_idle is longer than 1000 years")]
async fn build_cache_too_long_tti() {
let thousand_years_secs: u64 = 1000 * 365 * 24 * 3600;
let builder: CacheBuilder<Cache<char, String>> = CacheBuilder::new(100);
let builder: CacheBuilder<char, String, _> = CacheBuilder::new(100);
let duration = Duration::from_secs(thousand_years_secs);
builder
.time_to_idle(duration + Duration::from_secs(1))
Expand Down
22 changes: 11 additions & 11 deletions src/sync/base_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::{
},
PredicateError,
};

use crossbeam_channel::{Receiver, Sender, TrySendError};
use crossbeam_utils::atomic::AtomicCell;
use parking_lot::{Mutex, RwLock};
use smallvec::SmallVec;
use std::{
Expand Down Expand Up @@ -400,8 +400,8 @@ impl EntrySizeAndFrequency {
}

struct RawTimestamps {
last_accessed: Arc<AtomicU64>,
last_modified: Arc<AtomicU64>,
last_accessed: Arc<AtomicInstant>,
last_modified: Arc<AtomicInstant>,
}

impl RawTimestamps {
Expand Down Expand Up @@ -432,7 +432,7 @@ type CacheEntry<K, V> = (Arc<K>, Arc<ValueEntry<K, V>>);

pub(crate) struct Inner<K, V, S> {
max_capacity: Option<u64>,
weighted_size: AtomicU64,
weighted_size: AtomicCell<u64>,
cache: CacheStore<K, V, S>,
build_hasher: S,
deques: Mutex<Deques<K>>,
Expand Down Expand Up @@ -481,14 +481,14 @@ where

// Ensure skt_capacity fits in a range of `128u32..=u32::MAX`.
let skt_capacity = max_capacity
.map(|n| n.try_into().unwrap_or_default()) // Convert to u32.
.unwrap_or(u32::MAX)
.map(|n| n.try_into().unwrap_or(u32::MAX)) // Convert to u32.
.unwrap_or_default()
.max(128);
let frequency_sketch = FrequencySketch::with_capacity(skt_capacity);

Self {
max_capacity: max_capacity.map(|n| n as u64),
weighted_size: AtomicU64::default(),
weighted_size: AtomicCell::default(),
cache,
build_hasher,
deques: Mutex::new(Deques::default()),
Expand Down Expand Up @@ -657,7 +657,7 @@ where
let mut calls = 0;
let mut should_sync = true;

let current_ws = self.weighted_size.load(Ordering::Acquire);
let current_ws = self.weighted_size.load();
let mut ws = WeightedSize {
size: current_ws,
weigher: self.weigher.as_ref(),
Expand Down Expand Up @@ -695,8 +695,8 @@ where
}
}

debug_assert_eq!(self.weighted_size.load(Ordering::Acquire), current_ws);
self.weighted_size.store(ws.size, Ordering::Release);
debug_assert_eq!(self.weighted_size.load(), current_ws);
self.weighted_size.store(ws.size);

if should_sync {
Some(SyncPace::Fast)
Expand Down Expand Up @@ -1291,7 +1291,7 @@ mod tests {

let ensure_sketch_len = |max_capacity, len, name| {
let cache = BaseCache::<u8, u8>::new(
max_capacity,
Some(max_capacity),
None,
RandomState::default(),
None,
Expand Down
5 changes: 2 additions & 3 deletions src/sync/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ impl<K, V, C> CacheBuilder<K, V, C> {

#[cfg(test)]
mod tests {
use super::Cache;
use super::CacheBuilder;

use std::time::Duration;
Expand Down Expand Up @@ -329,7 +328,7 @@ mod tests {
#[should_panic(expected = "time_to_live is longer than 1000 years")]
async fn build_cache_too_long_ttl() {
let thousand_years_secs: u64 = 1000 * 365 * 24 * 3600;
let builder: CacheBuilder<Cache<char, String>> = CacheBuilder::new(100);
let builder: CacheBuilder<char, String, _> = CacheBuilder::new(100);
let duration = Duration::from_secs(thousand_years_secs);
builder
.time_to_live(duration + Duration::from_secs(1))
Expand All @@ -340,7 +339,7 @@ mod tests {
#[should_panic(expected = "time_to_idle is longer than 1000 years")]
async fn build_cache_too_long_tti() {
let thousand_years_secs: u64 = 1000 * 365 * 24 * 3600;
let builder: CacheBuilder<Cache<char, String>> = CacheBuilder::new(100);
let builder: CacheBuilder<char, String, _> = CacheBuilder::new(100);
let duration = Duration::from_secs(thousand_years_secs);
builder
.time_to_idle(duration + Duration::from_secs(1))
Expand Down
5 changes: 2 additions & 3 deletions src/unsync/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ impl<K, V, C> CacheBuilder<K, V, C> {

#[cfg(test)]
mod tests {
use super::Cache;
use super::CacheBuilder;

use std::time::Duration;
Expand Down Expand Up @@ -204,7 +203,7 @@ mod tests {
#[should_panic(expected = "time_to_live is longer than 1000 years")]
async fn build_cache_too_long_ttl() {
let thousand_years_secs: u64 = 1000 * 365 * 24 * 3600;
let builder: CacheBuilder<Cache<char, String>> = CacheBuilder::new(100);
let builder: CacheBuilder<char, String, _> = CacheBuilder::new(100);
let duration = Duration::from_secs(thousand_years_secs);
builder
.time_to_live(duration + Duration::from_secs(1))
Expand All @@ -215,7 +214,7 @@ mod tests {
#[should_panic(expected = "time_to_idle is longer than 1000 years")]
async fn build_cache_too_long_tti() {
let thousand_years_secs: u64 = 1000 * 365 * 24 * 3600;
let builder: CacheBuilder<Cache<char, String>> = CacheBuilder::new(100);
let builder: CacheBuilder<char, String, _> = CacheBuilder::new(100);
let duration = Duration::from_secs(thousand_years_secs);
builder
.time_to_idle(duration + Duration::from_secs(1))
Expand Down
4 changes: 2 additions & 2 deletions src/unsync/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ where

// Ensure skt_capacity fits in a range of `128u32..=u32::MAX`.
let skt_capacity = max_capacity
.map(|n| n.try_into().unwrap_or_default()) // Convert to u32.
.unwrap_or(u32::MAX)
.map(|n| n.try_into().unwrap_or(u32::MAX)) // Convert to u32.
.unwrap_or_default()
.max(128);
let frequency_sketch = FrequencySketch::with_capacity(skt_capacity);
Self {
Expand Down

0 comments on commit 4404e6f

Please sign in to comment.