Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ where
const EPOCH_MAX: usize = (1 << EPOCH_BITS) - 1;
const { assert!(C::EPOCHS, "can only .clear() when C::EPOCHS is true") }
let prev = self.epoch.fetch_add(1, Ordering::Release);
if prev == EPOCH_MAX {
// Tags store only the low EPOCH_BITS of the epoch, clear the cache on every low-bit wrap.
if (prev & EPOCH_MAX) == EPOCH_MAX {
self.clear_slow();
}
}
Expand Down Expand Up @@ -1206,6 +1207,23 @@ mod tests {
}
}

#[test]
fn epoch_repeated_wraparound_stays_cleared() {
let cache: EpochCache<u64, u64> = EpochCache::new(4096, Default::default());

for _ in 0..1024 {
cache.clear();
}

cache.insert(42, 123);
assert_eq!(cache.get(&42), Some(123));

for i in 0..1024 {
cache.clear();
assert_eq!(cache.get(&42), None, "failed at clear #{i}");
}
}

#[test]
fn remove_copy_type() {
let cache = new_cache::<u64, u64>(64);
Expand Down
Loading