Repository navigation
Description
It looks like that the insert and get_value_or_guard functions chose the element to evict differently on a full cache.
Here's the code:
#[derive(Clone, Debug, Default)]
pub struct LoggingLifecycle {
is_pinned: Arc<Mutex<HashSet<usize>>>,
}
impl<K> Lifecycle<K, usize> for LoggingLifecycle {
type RequestState = Option<K>;
fn is_pinned(&self, _key: &K, value: &usize) -> bool {
self.is_pinned.lock().unwrap().contains(value)
}
fn begin_request(&self) -> Self::RequestState {
None
}
fn on_evict(&self, state: &mut Self::RequestState, key: K, _value: usize) {
*state = Some(key);
}
}
#[test]
fn quick_cache_insert_behavior() {
type Cache =
quick_cache::sync::Cache<u64, usize, UnitWeighter, RandomState, LoggingLifecycle>;
let num_elements: usize = 3;
let num_iter = 6;
let is_pinned = Arc::new(Mutex::new(HashSet::new()));
let cache = quick_cache::sync::Cache::with_options(
OptionsBuilder::new()
.shards(1)
.weight_capacity(num_elements as u64)
.estimated_items_capacity(num_elements)
.build()
.unwrap(),
UnitWeighter,
RandomState::default(),
LoggingLifecycle {
is_pinned: is_pinned.clone(),
},
);
let cases: Vec<(String, Arc<dyn Fn(_, _, _) -> _>)> = vec![
(
"insert".to_owned(),
Arc::new(move |id, pos, cache: &Cache| cache.insert_with_lifecycle(id, pos)),
),
(
"get_value_or_guard".to_owned(),
Arc::new(
move |id, pos, cache: &Cache| match cache.get_value_or_guard(&id, None) {
GuardResult::Guard(guard) => guard.insert_with_lifecycle(pos).unwrap(),
GuardResult::Timeout | GuardResult::Value(_) => unreachable!(),
},
),
),
];
for pinning in [false, true] {
for (test_name, insert_fn) in cases.clone() {
cache.clear();
println!("Testing case: {test_name} with pinning={pinning}");
let mut pos = 0;
for i in 0..num_iter {
if pinning {
is_pinned.lock().unwrap().insert(pos);
};
let evicted = insert_fn(i, pos, &cache);
if pinning {
is_pinned.lock().unwrap().remove(&pos);
};
eprintln!("Inserted key {i:?} --> Evicted key: {evicted:?}");
pos = if let Some(key) = evicted {
key as usize
} else {
pos + 1
};
}
}
}
}This simulates insertions with a weak access pattern, i.e. inserting without touching the elements, with two insertion methods: insert or get_value_or_guard with placeholder replacement. There is also a pinned parameter used to manually pin the current element to avoid this being immediately evicted.
Here's the output with pinned = false
Testing case: insert with pinning=false
Inserted key 0 --> Evicted key: None
Inserted key 1 --> Evicted key: None
Inserted key 2 --> Evicted key: None
Inserted key 3 --> Evicted key: Some(2)
Inserted key 4 --> Evicted key: Some(3)
Inserted key 5 --> Evicted key: Some(4)
Testing case: get_value_or_guard with pinning=false
Inserted key 0 --> Evicted key: None
Inserted key 1 --> Evicted key: None
Inserted key 2 --> Evicted key: None
Inserted key 3 --> Evicted key: Some(3)
Inserted key 4 --> Evicted key: Some(4)
Inserted key 5 --> Evicted key: Some(5)
insert always inserts the current element in the cache and removes the last inserted one, while get_value_or_guard evicts it immediately. In my understanding, the latter would be the correct behavior as the current element is the one with the highest reuse distance.
With pinned = true instead, I don't really understand the results:
Testing case: insert with pinning=true
Inserted key 0 --> Evicted key: None
Inserted key 1 --> Evicted key: None
Inserted key 2 --> Evicted key: None
Inserted key 3 --> Evicted key: Some(2)
Inserted key 4 --> Evicted key: Some(3)
Inserted key 5 --> Evicted key: Some(4)
Testing case: get_value_or_guard with pinning=true
Inserted key 0 --> Evicted key: None
Inserted key 1 --> Evicted key: None
Inserted key 2 --> Evicted key: None
Inserted key 3 --> Evicted key: Some(0)
Inserted key 4 --> Evicted key: Some(3)
Inserted key 5 --> Evicted key: Some(4)
Nothing changes for insert as the current element is anyway inserted in the cache.
On the other hand, get_value_or_guard when inserts in a full cache (key 3) decides to evict the first element inserted (key 0), like in LRU. At the next insertion (key 4) it goes back to removing the last inserted element (key 3), and so on.
I am not sure about what is an intended behavior and what not, but I suppose that inserting in the cache should have the same behavior regardless of the kind of function you use. Am I missing something here?
Activity
[-]`insert` and `get_value_or_guard` triggers different eviction policies[/-][+]`insert` and `get_value_or_guard` trigger different eviction policies[/+]arthurprs commentedon Oct 10, 2025
The behavior is not guaranteed to be strictly the same. Right now, plain insert performs evictions before placing the new item, whereas the guarded insertion will perform evictions after insertion. In some cases, these can theoretically evict different items
In a cache of capacity 3 allocates 2 units are allocated for the hot section and 1 unit for the cold section. While the cache is getting filled initially, the first 2 items will go directly into hot, and the 3rd into cold.
The eviction behavior prioritizes evicting from cold first. In the absence of cache hits (like in the example), none of the cold items will be promoted into hot, and the expected behavior is that the new inserts will replace the cold item.
You've stumbled upon a small bug here, where all 3 insertions from guard get admitted into hot, whereas plain insert only puts the first 2 into hot (expected behavior).
With the upcoming PR
ihtefyw commentedon Oct 11, 2025
Thanks for the fix, it makes sense now.