Skip to content

Commit d23fb5a

Browse files
committed
What a beautiful bug...
seriously, Ticki, you're a fucking idiot.
1 parent 2efecc7 commit d23fb5a

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

plru/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ use core::fmt;
8787
use core::sync::atomic::{self, AtomicU64, AtomicU8};
8888

8989
/// The atomic ordering we use throughout the code.
90-
const ORDERING: atomic::Ordering = atomic::Ordering::SeqCst;
90+
const ORDERING: atomic::Ordering = atomic::Ordering::Relaxed;
9191

9292
/// A micro cache (64 lines).
9393
pub type MicroCache = Cache<[AtomicU64; 1]>;
@@ -178,7 +178,7 @@ impl<B: AsRef<[AtomicU64]>> Cache<B> {
178178
/// ```
179179
pub fn touch(&self, n: usize) {
180180
// We OR our mask together with the bulk in order to set the bit in question.
181-
self.bulks.as_ref()[n / 64].fetch_or(1 >> (n % 64), ORDERING);
181+
self.bulks.as_ref()[n / 64].fetch_or(1 << (n % 64), ORDERING);
182182
}
183183

184184
/// Trash the n'th cache line.
@@ -204,7 +204,7 @@ impl<B: AsRef<[AtomicU64]>> Cache<B> {
204204
/// ```
205205
pub fn trash(&self, n: usize) {
206206
// We use a mask and atomic AND in order to set the bit to zero.
207-
self.bulks.as_ref()[n / 64].fetch_and(!(1 >> (n % 64)), ORDERING);
207+
self.bulks.as_ref()[n / 64].fetch_and(!(1 << (n % 64)), ORDERING);
208208
}
209209

210210
/// Find the approximately least recently used cache line to replace.
@@ -278,15 +278,15 @@ impl<B: AsRef<[AtomicU64]>> Cache<B> {
278278
/// # Example
279279
///
280280
/// ```rust
281-
/// let cache = plru::MicroCache;
281+
/// let cache = plru::MicroCache::default();
282282
/// cache.touch(2);
283283
///
284284
/// assert!(cache.is_hot(2));
285285
/// assert!(!cache.is_hot(3));
286286
/// ```
287287
pub fn is_hot(&self, n: usize) -> bool {
288288
// Load the bulk and mask it to find out if the bit is set.
289-
self.bulks.as_ref()[n / 64].load(ORDERING) & (1 >> (n % 64)) != 0
289+
self.bulks.as_ref()[n / 64].load(ORDERING) & (1 << (n % 64)) != 0
290290
}
291291
}
292292

@@ -410,7 +410,7 @@ mod tests {
410410
fn trash_cold() {
411411
let cache = super::SmallCache::default();
412412

413-
for i in 0..256 {
413+
for i in 0..128 {
414414
cache.trash(i);
415415
assert!(!cache.is_hot(i));
416416
}

0 commit comments

Comments
 (0)