5 releases

new 0.2.3 May 14, 2024
0.2.2 May 8, 2024
0.2.1 Jan 18, 2023
0.1.1 Dec 20, 2022

#20 in Caching

Download history 45031/week @ 2024-01-23 43909/week @ 2024-01-30 43711/week @ 2024-02-06 46227/week @ 2024-02-13 50529/week @ 2024-02-20 49364/week @ 2024-02-27 44735/week @ 2024-03-05 45247/week @ 2024-03-12 51279/week @ 2024-03-19 44150/week @ 2024-03-26 64518/week @ 2024-04-02 59569/week @ 2024-04-09 50376/week @ 2024-04-16 49525/week @ 2024-04-23 39940/week @ 2024-04-30 29833/week @ 2024-05-07

180,350 downloads per month
Used in 468 crates (24 directly)

MIT/Apache

98KB
2K SLoC

A fast and flexible LRU map

Documentation

This repository contains a fast and flexible LRU map.

  • Blazingly fast. Up to twice as fast as the lru crate, and with less memory overhead.
  • Can be also used as an ordered map, with roughly the same performance as indexmap, but with added support for O(1) removals without changing the element order (where indexmap only supports O(n) non-perturbing removals).
  • Customizable. Out-of-box can be limited by length or by memory usage, but supports custom limiters which can be made to limit the map by whatever you want.
  • Tested, miri-clean, clippy-clean and fuzzed.
  • Supports no_std.

Examples

use schnellru::{LruMap, ByLength};
let mut map = LruMap::new(ByLength::new(3));

// Insert three elements.
map.insert(1, "one");
map.insert(2, "two");
map.insert(3, "three");
assert_eq!(map.len(), 3);

// They're ordered according to which one was inserted last.
let mut iter = map.iter();
assert_eq!(iter.next().unwrap(), (&3, &"three"));
assert_eq!(iter.next().unwrap(), (&2, &"two"));
assert_eq!(iter.next().unwrap(), (&1, &"one"));

// Access the least recently inserted one.
assert_eq!(*map.get(&1).unwrap(), "one");

// Now the order's changed.
// The element we've accessed was moved to the front.
let mut iter = map.iter();
assert_eq!(iter.next().unwrap(), (&1, &"one"));
assert_eq!(iter.next().unwrap(), (&3, &"three"));
assert_eq!(iter.next().unwrap(), (&2, &"two"));

// Insert a fourth element.
// This will automatically pop the least recently accessed one.
map.insert(4, "four");

// Still the same number of elements.
assert_eq!(map.len(), 3);

// And this is the one which was removed.
assert!(map.peek(&2).is_none());

// And here's the new order.
let mut iter = map.iter();
assert_eq!(iter.next().unwrap(), (&4, &"four"));
assert_eq!(iter.next().unwrap(), (&1, &"one"));
assert_eq!(iter.next().unwrap(), (&3, &"three"));

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~2MB
~26K SLoC