https://github.com/koute/schnellru
A fast and flexible LRU map.
https://github.com/koute/schnellru
Last synced: about 1 year ago
JSON representation
A fast and flexible LRU map.
- Host: GitHub
- URL: https://github.com/koute/schnellru
- Owner: koute
- License: apache-2.0
- Created: 2022-12-20T15:39:11.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-01-03T08:23:55.000Z (over 1 year ago)
- Last Synced: 2025-04-14T22:13:12.369Z (about 1 year ago)
- Language: Rust
- Size: 86.9 KB
- Stars: 181
- Watchers: 4
- Forks: 12
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# A fast and flexible LRU map
[](https://docs.rs/schnellru/*/schnellru/)
This repository contains a fast and flexible LRU map.
* Blazingly fast. Up to twice as fast as the [`lru`](https://github.com/jeromefroe/lru-rs) crate, and with less memory overhead.
* Can be also used as an ordered map, with roughly the same performance as [`indexmap`](https://github.com/bluss/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
```rust
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
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or )
* MIT license ([LICENSE-MIT](LICENSE-MIT) or )
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.