intrusive_lru_cache/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
#![doc = include_str!("../README.md")]
#![no_std]
extern crate alloc;
use alloc::rc::Rc;
use core::borrow::Borrow;
use core::cell::UnsafeCell;
use intrusive_collections::intrusive_adapter;
use intrusive_collections::rbtree::Entry as RBTreeEntry;
use intrusive_collections::{KeyAdapter, LinkedList, LinkedListLink, RBTree, RBTreeLink};
struct Entry<K, V> {
list_link: LinkedListLink,
tree_link: RBTreeLink,
key: K,
value: UnsafeCell<V>,
}
impl<K, V> Entry<K, V> {
#[inline(always)]
fn new_rc(key: K, value: V) -> Rc<Self> {
Rc::new(Self {
list_link: LinkedListLink::new(),
tree_link: RBTreeLink::new(),
key,
value: UnsafeCell::new(value),
})
}
#[inline(always)]
fn value(&self) -> &V {
unsafe { &*self.value.get() }
}
/// SAFETY: Only use with exclusive access to the Entry
#[inline(always)]
unsafe fn replace_value(&self, value: V) -> V {
core::ptr::replace(self.value.get(), value)
}
}
intrusive_adapter!(EntryListAdapter<K, V> = Rc<Entry<K, V>>: Entry<K, V> { list_link: LinkedListLink });
intrusive_adapter!(EntryTreeAdapter<K, V> = Rc<Entry<K, V>>: Entry<K, V> { tree_link: RBTreeLink });
// Because KeyAdapter returns a reference, and `find` uses the returned type as `K`,
// I ran into issues where `&K: Borrow<Q>` was not satisfied. Therefore, we need
// to convince the compiler that some `Q` can be borrowed from `&K` by using a
// transparent wrapper type for both halves, and casting `&Q` to `&Borrowed<Q>`.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
struct Key<K>(K);
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
struct Borrowed<Q: ?Sized>(Q);
impl<'a, Q: ?Sized> Borrowed<Q> {
#[inline(always)]
const fn new(value: &'a Q) -> &'a Self {
// SAFETY: &Q == &Borrowed<Q> due to transparent repr
unsafe { core::mem::transmute(value) }
}
}
// Magic that allows `&K: Borrow<Q>` to be satisfied
impl<K, Q: ?Sized> Borrow<Borrowed<Q>> for Key<&K>
where
K: Borrow<Q>,
{
#[inline(always)]
fn borrow(&self) -> &Borrowed<Q> {
Borrowed::new(self.0.borrow())
}
}
impl<'a, K: 'a, V> KeyAdapter<'a> for EntryTreeAdapter<K, V> {
type Key = Key<&'a K>; // Allows `Key<&K>: Borrow<Borrowed<Q>>`
#[inline(always)]
fn get_key(&self, value: &'a Entry<K, V>) -> Self::Key {
// SAFETY: &K == Key<&K> == &Key<K> due to transparent repr
unsafe { core::mem::transmute(&value.key) }
}
}
/// LRU Cache implementation using intrusive collections.
///
/// This cache uses an [`intrusive_collections::LinkedList`] to maintain the LRU order,
/// and an [`intrusive_collections::RBTree`] to allow for efficient lookups by key,
/// while maintaining only one allocation per key-value pair. Unfortunately, this
/// is a linked structure, so cache locality is likely poor, but memory usage
/// and flexibility are improved.
///
/// The cache is unbounded by default, but can be limited to a maximum capacity.
///
/// # Example
/// ```rust
/// use intrusive_lru_cache::LRUCache;
///
/// let mut lru: LRUCache<&'static str, &'static str> = LRUCache::default();
///
/// lru.insert("a", "1");
/// lru.insert("b", "2");
/// lru.insert("c", "3");
///
/// let _ = lru.get("b"); // updates LRU order
///
/// assert_eq!(lru.pop(), Some(("a", "1")));
/// assert_eq!(lru.pop(), Some(("c", "3")));
/// assert_eq!(lru.pop(), Some(("b", "2")));
/// assert_eq!(lru.pop(), None);
/// ```
///
/// # Notes
///
/// - The cache is not thread-safe, and requires external synchronization.
/// - Cloning the cache will preserve the LRU order.
pub struct LRUCache<K, V> {
list: LinkedList<EntryListAdapter<K, V>>,
tree: RBTree<EntryTreeAdapter<K, V>>,
size: usize,
max_capacity: usize,
}
impl<K, V> LRUCache<K, V> {
/// Creates a new unbounded LRU cache.
///
/// This cache has no limit on the number of entries it can hold,
/// so entries must be manually removed via [`pop`](Self::pop),
/// or you can use [`set_max_capacity`](Self::set_max_capacity) to set a limit.
pub fn new() -> Self {
Self::new_with_max_capacity(usize::MAX)
}
/// Creates a new LRU cache with a maximum capacity, after which
/// old entries will be evicted to make room for new ones.
///
/// This does not preallocate any memory, only sets an upper limit.
pub fn new_with_max_capacity(max_capacity: usize) -> Self {
Self {
list: LinkedList::new(EntryListAdapter::new()),
tree: RBTree::new(EntryTreeAdapter::new()),
size: 0,
max_capacity,
}
}
}
impl<K, V> Default for LRUCache<K, V> {
fn default() -> Self {
Self::new()
}
}
impl<K, V> Clone for LRUCache<K, V>
where
K: Clone + Ord + 'static,
V: Clone,
{
fn clone(&self) -> Self {
let mut new = Self::new_with_max_capacity(self.max_capacity);
// preserves the LRU ordering
for (key, value) in self.iter_lru() {
new.insert(key.clone(), value.clone());
}
new
}
}
impl<K, V> LRUCache<K, V>
where
K: Ord + 'static,
{
/// Returns a reference to the value corresponding to the key,
/// and bumps the key to the front of the LRU list.
pub fn get<'a, 'b, Q>(&'a mut self, key: &Q) -> Option<&'a V>
where
K: Borrow<Q>,
Q: Ord + ?Sized,
'a: 'b,
{
let entry = self.tree.find(Borrowed::new(key)).get()?;
let cursor = unsafe {
self.list
.cursor_mut_from_ptr(entry)
.remove()
.expect("tree and list are inconsistent")
};
self.list.front_mut().insert_before(cursor);
Some(entry.value())
}
/// Returns a reference to the value corresponding to the key,
/// without updating the LRU list.
pub fn peek<'a, 'b, Q>(&'a self, key: &Q) -> Option<&'a V>
where
K: Borrow<Q>,
Q: Ord + ?Sized,
'a: 'b,
{
self.tree
.find(Borrowed::new(key))
.get()
.map(|entry| entry.value())
}
/// Inserts a key-value pair into the cache, returning
/// the old value if the key was already present.
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
match self.tree.entry(Borrowed::new(&key)) {
RBTreeEntry::Occupied(cursor) => unsafe {
let entry = cursor.get().unwrap();
// NOTE: Treat cursor/entry as if it were mutable for replace_value
// since we can't ever actually acquire a mutable reference to the entry
// as per the restrictions of `intrusive_collections`
let old_value = Some(entry.replace_value(value));
let lru = self
.list
.cursor_mut_from_ptr(entry)
.remove()
.expect("tree and list are inconsistent");
self.list.push_front(lru);
old_value
},
RBTreeEntry::Vacant(cursor) => {
let entry = Entry::new_rc(key, value);
cursor.insert(entry.clone());
self.list.push_front(entry);
self.size += 1;
self.shrink();
None
}
}
}
/// Removes the value corresponding to the key from the cache,
/// and returning it if it was present.
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: Ord + ?Sized,
{
let entry = self.tree.find_mut(Borrowed::new(key)).remove()?;
let _ = unsafe {
self.list
.cursor_mut_from_ptr(&*entry)
.remove()
.expect("tree and list are inconsistent")
};
self.size -= 1;
let Ok(Entry { value, .. }) = Rc::try_unwrap(entry) else {
unreachable!("tree and list are inconsistent")
};
Some(value.into_inner())
}
}
impl<K, V> LRUCache<K, V> {
/// Sets the maximum capacity of the cache.
///
/// This does not remove any entries, but will cause the cache to evict
/// entries when inserting new ones if the length exceeds the new capacity.
///
/// Use [`shrink`](Self::shrink) to manually trigger removal of entries
/// to meet the new capacity.
#[inline(always)]
pub fn set_max_capacity(&mut self, max_capacity: usize) {
self.max_capacity = max_capacity;
}
/// Clears the cache, removing all key-value pairs.
///
/// This is `O(2n)` where n is the number of key-value pairs in the cache,
/// as both the LRU list _and_ key-value tree require iteration to be cleared.
pub fn clear(&mut self) {
self.list.clear();
self.tree.clear();
}
/// Removes the oldest entries from the cache until the length is less than or equal to the maximum capacity.
pub fn shrink(&mut self) {
while self.size > self.max_capacity {
let _ = self.pop();
}
}
/// Removes the oldest entries from the cache until the length is less than or equal to the maximum capacity,
/// and calls the provided closure with the removed key-value pairs.
///
/// # Example
/// ```rust
/// # use intrusive_lru_cache::LRUCache;
/// let mut lru: LRUCache<&'static str, &'static str> = LRUCache::default();
///
/// lru.insert("a", "1");
/// lru.insert("b", "2");
/// lru.insert("c", "3");
///
/// lru.set_max_capacity(1);
///
/// let mut removed = Vec::new();
///
/// lru.shrink_with(|key, value| {
/// removed.push((key, value));
/// });
///
/// assert_eq!(removed, vec![("a", "1"), ("b", "2")]);
/// ```
pub fn shrink_with<F>(&mut self, mut cb: F)
where
F: FnMut(K, V),
{
while self.size > self.max_capacity {
let Some((key, value)) = self.pop() else {
break;
};
cb(key, value);
}
}
/// Returns the number of key-value pairs in the cache.
#[inline(always)]
pub const fn len(&self) -> usize {
self.size
}
/// Returns `true` if the cache is empty.
#[inline(always)]
pub fn is_empty(&self) -> bool {
debug_assert_eq!(self.size == 0, self.list.is_empty());
self.size == 0
}
/// Removes and returns the least recently used key-value pair.
///
/// This is an `O(1)` operation.
pub fn pop(&mut self) -> Option<(K, V)> {
let entry = self.list.pop_back()?;
let _ = unsafe {
self.tree
.cursor_mut_from_ptr(&*entry)
.remove()
.expect("tree and list are inconsistent")
};
self.size -= 1;
let Ok(Entry { key, value, .. }) = Rc::try_unwrap(entry) else {
unreachable!("tree and list are inconsistent")
};
Some((key, value.into_inner()))
}
/// Returns an iterator over the key-value pairs in the cache,
/// in order of least recently used to most recently used.
pub fn iter_lru(&self) -> impl DoubleEndedIterator<Item = (&K, &V)> {
self.list.iter().map(|entry| (&entry.key, entry.value()))
}
/// Returns an iterator over the key-value pairs in the cache,
/// in order of key `Ord` order.
pub fn iter_ord(&self) -> impl DoubleEndedIterator<Item = (&K, &V)> {
self.tree.iter().map(|entry| (&entry.key, entry.value()))
}
}
impl<K, V> Extend<(K, V)> for LRUCache<K, V>
where
K: Ord + 'static,
{
fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = (K, V)>,
{
for (key, value) in iter {
self.insert(key, value);
}
}
}
impl<K, V> FromIterator<(K, V)> for LRUCache<K, V>
where
K: Ord + 'static,
{
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = (K, V)>,
{
let mut cache = Self::new();
cache.extend(iter);
cache
}
}
pub struct IntoIter<K, V> {
inner: intrusive_collections::linked_list::IntoIter<EntryListAdapter<K, V>>,
}
impl<K, V> IntoIterator for LRUCache<K, V>
where
K: Ord + 'static,
{
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(mut self) -> Self::IntoIter {
self.tree.clear();
IntoIter {
inner: self.list.into_iter(),
}
}
}
impl<K, V> Iterator for IntoIter<K, V> {
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> {
let entry = self.inner.next()?;
let Ok(Entry { key, value, .. }) = Rc::try_unwrap(entry) else {
unreachable!("tree and list are inconsistent")
};
Some((key, value.into_inner()))
}
}
impl<K, V> DoubleEndedIterator for IntoIter<K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
let entry = self.inner.next_back()?;
let Ok(Entry { key, value, .. }) = Rc::try_unwrap(entry) else {
unreachable!("tree and list are inconsistent")
};
Some((key, value.into_inner()))
}
}