S3FIFO

Struct S3FIFO 

Source
pub struct S3FIFO<K, V> { /* private fields */ }
Expand description

S3FIFO is a non-thread-safe implementation of an S3-FIFO

Paper here: https://jasony.me/publication/sosp23-s3fifo.pdf

S3FIFO is a cache that is split into three parts:

  1. A small cache that holds the most recently used items
  2. A main cache that holds the most frequently used items
  3. A ghost cache that holds keys that have been evicted from the main cache
use s3_fifo::{S3FIFO, S3FIFOKey};

// The cached value must be Clone.
// Hash is optional and allows using the S3FIFOKey struct
#[derive(Clone, Hash)]
struct Foobar { a: i32 }

// Create a cache with a capacity of 128 (small: 12, main: 115, ghost: 115)
let mut cache = S3FIFO::new(128);
let value = Foobar { a: 1 };
let key = S3FIFOKey::new(&value);

// Check if the item is in the cache before inserting
if let None = cache.get(&key) {
    cache.put(key.clone(), value);
    assert!(cache.get(&key).is_some());
}

Implementations§

Source§

impl<K: PartialEq + Clone, V> S3FIFO<K, V>

Source

pub fn new(capacity: usize) -> Self

Create a new S3FIFO cache with 10% of the capacity for the small cache and 90% of the capacity for the main cache.

The ghost cache is also 90% of the capacity but only holds keys and not values.

Source

pub fn get(&self, key: &K) -> Option<&V>

Read an item from the cache. If the item is present, then its frequency is incremented and a reference is returned.

Source

pub fn get_mut(&mut self, key: &K) -> Option<&mut V>

Read an item from the cache. If the item is present, then its frequency is incremented and a mutable reference is returned.

Source

pub fn put(&mut self, key: K, value: V) -> (&mut V, Option<V>)

Write an item to the cache. This may evict an item from the cache. The returnted tuple is a mutable reference to the value in the cache and any evicted value.

Source

pub fn pop(&mut self) -> Option<V>

Remove an item from the cache.

Source

pub fn drain(&mut self) -> Vec<V>

Remove all items from the cache, leaving it empty and with the same capacity.

Auto Trait Implementations§

§

impl<K, V> Freeze for S3FIFO<K, V>

§

impl<K, V> RefUnwindSafe for S3FIFO<K, V>

§

impl<K, V> Send for S3FIFO<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for S3FIFO<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for S3FIFO<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for S3FIFO<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.