Struct holochain_wasmer_host::plru::Cache

source ·
pub struct Cache<B> { /* private fields */ }
Expand description

A pseudo-LRU cache tracker.

This manages a set of cache lines (enumerated by usize) in an efficient manner, such that you can touch cache lines (mark usage) and efficiently find a cache line which can be replaced (unlikely to be used in the near future).

Implementations§

source§

impl<B: AsRef<[AtomicU64]>> Cache<B>

source

pub fn new(bulks: B) -> Cache<B>

Create a new cache based on some array.

Generally, this should not be used unless you’re building abstractions. You should likely use Cache::<SomeType>::default() or plru::create() instead.

§Example
use holochain_wasmer_host::plru::Cache;

Cache::new([Default::default(), Default::default()]);
source

pub fn touch(&self, n: usize)

Touch the n’th cache line.

Whenever you modify/use the cache line, you should call touch in order to mark that it was recently used.

Each cache line has a bitflag defining if it was recently used. This will set said flag.

§Example
let cache = holochain_wasmer_host::plru::SmallCache::default();

cache.touch(10);
assert!(cache.is_hot(10));
source

pub fn trash(&self, n: usize)

Trash the n’th cache line.

Trashing is generally only used if you know that this line is not going to be used later on.

Trashing will merely mark this line as cold, and hence be queued for replacement until it is used again. As such, it is not a terrible performance loss if you trash a line which is used later, and decision can be made heuristically (“this is likely not going to be used later again”).

§Example
let cache = holochain_wasmer_host::plru::SmallCache::default();

cache.touch(10);
assert!(cache.is_hot(10));

cache.trash(10);
assert!(!cache.is_hot(10));
source

pub fn replace(&self) -> usize

Find the approximately least recently used cache line to replace.

A particular bulk is selected based on a counter incremented on every replace call. The first unset bit of this bulk determines the cold cache line we will return. If all the flags in the 64-bit bulk are set, the whole bulk will be reset to zero in order to inflate the cache.

This is approximately the least-recently-used cache line.

Note that it will not set the found bit. If you use the line right after requesting it, you still need to call touch. In fact, it will always return a cold line.

You cannot rely on uniqueness of the output. It might return the same result twice, although it is unlikely.

§Example
let cache = holochain_wasmer_host::plru::MediumCache::default();
cache.touch(10);
cache.touch(20);
cache.touch(1);

assert_ne!(cache.replace(), 1);
assert_ne!(cache.replace(), 20);
assert_ne!(cache.replace(), 10);
source

pub fn len(&self) -> usize

Find the number of cache lines in this cache.

This is subject equal to the length of B mutliplied by 64.

§Example
assert_eq!(holochain_wasmer_host::plru::create(10).len(), 64);
assert_eq!(holochain_wasmer_host::plru::create(64).len(), 64);
assert_eq!(holochain_wasmer_host::plru::create(65).len(), 128);
source

pub fn is_empty(&self) -> bool

Clippy wants this in addition to len.

source

pub fn is_hot(&self, n: usize) -> bool

Is the n’th cache line hot?

This returns a boolean defining if it has recently be used or not. true means that the cache line is registered as “hot” and false that it is registered as “cold”.

§Example
let cache = holochain_wasmer_host::plru::MicroCache::default();
cache.touch(2);

assert!(cache.is_hot(2));
assert!(!cache.is_hot(3));

Trait Implementations§

source§

impl<B: AsRef<[AtomicU64]>> Debug for Cache<B>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<B: Default> Default for Cache<B>

source§

fn default() -> Cache<B>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<B> !Freeze for Cache<B>

§

impl<B> RefUnwindSafe for Cache<B>
where B: RefUnwindSafe,

§

impl<B> Send for Cache<B>
where B: Send,

§

impl<B> Sync for Cache<B>
where B: Sync,

§

impl<B> Unpin for Cache<B>
where B: Unpin,

§

impl<B> UnwindSafe for Cache<B>
where B: 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> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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<F, W, T, D> Deserialize<With<T, W>, D> for F
where W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

source§

fn deserialize( &self, deserializer: &mut D, ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> LayoutRaw for T

source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Gets the layout of the type.
source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Pointee for T

§

type Metadata = ()

The type for metadata in pointers and references to Self.
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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.
source§

impl<T> Upcastable for T
where T: Any + Send + Sync + 'static,

source§

fn upcast_any_ref(&self) -> &(dyn Any + 'static)

upcast ref
source§

fn upcast_any_mut(&mut self) -> &mut (dyn Any + 'static)

upcast mut ref
source§

fn upcast_any_box(self: Box<T>) -> Box<dyn Any>

upcast boxed dyn
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more