pub struct LRUCache<T, M> {
entries: FreeList<LRUCacheEntry<T>, M>,
lru: Vec<LRUTracker<FreeListHandle<M>>>,
}
Expand description
The main public interface to the LRU cache
Fields§
§entries: FreeList<LRUCacheEntry<T>, M>
A free list of cache entries, and indices into the LRU tracking list
lru: Vec<LRUTracker<FreeListHandle<M>>>
The LRU tracking list, allowing O(1) access to the oldest element
Implementations§
source§impl<T, M> LRUCache<T, M>
impl<T, M> LRUCache<T, M>
sourcepub fn push_new(
&mut self,
partition_index: u8,
value: T,
) -> WeakFreeListHandle<M>
pub fn push_new( &mut self, partition_index: u8, value: T, ) -> WeakFreeListHandle<M>
Insert a new element into the cache. Returns a weak handle for callers to access the data, since the lifetime is managed by the LRU algorithm and it may be evicted at any time.
sourcepub fn get_opt(&self, handle: &WeakFreeListHandle<M>) -> Option<&T>
pub fn get_opt(&self, handle: &WeakFreeListHandle<M>) -> Option<&T>
Get immutable access to the data at a given slot. Since this takes a weak handle, it may have been evicted, so returns an Option.
sourcepub fn get_opt_mut(&mut self, handle: &WeakFreeListHandle<M>) -> Option<&mut T>
pub fn get_opt_mut(&mut self, handle: &WeakFreeListHandle<M>) -> Option<&mut T>
Get mutable access to the data at a given slot. Since this takes a weak handle, it may have been evicted, so returns an Option.
sourcepub fn peek_oldest(&self, partition_index: u8) -> Option<&T>
pub fn peek_oldest(&self, partition_index: u8) -> Option<&T>
Return a reference to the oldest item in the cache, keeping it in the cache. If the cache is empty, this will return None.
sourcepub fn pop_oldest(&mut self, partition_index: u8) -> Option<T>
pub fn pop_oldest(&mut self, partition_index: u8) -> Option<T>
Remove the oldest item from the cache. This is used to select elements to be evicted. If the cache is empty, this will return None.
sourcepub fn replace_or_insert(
&mut self,
handle: &mut WeakFreeListHandle<M>,
partition_index: u8,
data: T,
) -> Option<T>
pub fn replace_or_insert( &mut self, handle: &mut WeakFreeListHandle<M>, partition_index: u8, data: T, ) -> Option<T>
This is a special case of push_new
, which is a requirement for the texture
cache. Sometimes, we want to replace the content of an existing handle if it
exists, or insert a new element if the handle is invalid (for example, if an
image is resized and it moves to a new location in the texture atlas). This
method returns the old cache entry if it existed, so it can be freed by the caller.
sourcepub fn remove(&mut self, handle: &WeakFreeListHandle<M>) -> Option<T>
pub fn remove(&mut self, handle: &WeakFreeListHandle<M>) -> Option<T>
Manually evict a specific item.
sourcepub fn touch(&mut self, handle: &WeakFreeListHandle<M>) -> Option<&mut T>
pub fn touch(&mut self, handle: &WeakFreeListHandle<M>) -> Option<&mut T>
This is used by the calling code to signal that the element that this handle references has been used on this frame. Internally, it updates the links in the LRU tracking element to move this item to the end of the LRU list. Returns the underlying data in case the client wants to mutate it.
Trait Implementations§
source§impl<T, M> MallocSizeOf for LRUCache<T, M>where
T: MallocSizeOf,
M: MallocSizeOf,
impl<T, M> MallocSizeOf for LRUCache<T, M>where
T: MallocSizeOf,
M: MallocSizeOf,
source§fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize
Auto Trait Implementations§
impl<T, M> Freeze for LRUCache<T, M>
impl<T, M> RefUnwindSafe for LRUCache<T, M>where
M: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, M> Send for LRUCache<T, M>
impl<T, M> Sync for LRUCache<T, M>
impl<T, M> Unpin for LRUCache<T, M>
impl<T, M> UnwindSafe for LRUCache<T, M>where
M: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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