• Stars
    star
    167
  • Rank 219,479 (Top 5 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 1 year ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A fast and flexible LRU map.

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.

More Repositories

1

bytehound

A memory profiler for Linux.
C
3,848
star
2

stdweb

A standard library for the client-side Web
Rust
3,433
star
3

cargo-web

A Cargo subcommand for the client-side Web
Rust
1,095
star
4

not-perf

A sampling CPU profiler for Linux
Rust
849
star
5

pinky

An NES emulator written in Rust
Rust
758
star
6

speedy

A fast binary serialization framework
Rust
331
star
7

polkavm

A fast and secure RISC-V based virtual machine
Rust
152
star
8

parcel-plugin-cargo-web

JavaScript
48
star
9

libretro-backend

Libretro API bindings for Rust
Rust
46
star
10

sarek

A work-in-progress, experimental neural network library for Rust
Rust
44
star
11

polkadot-starship

Ruby
23
star
12

polkavm-experiment

An experimental RISC-V recompiler
Rust
18
star
13

hooky

A convenient LD_PRELOAD hooker
Rust
11
star
14

rustc-rv32e

Rust toolchain for RV32E
Shell
8
star
15

libretro-sys

Raw FFI bindings to the libretro API
Rust
6
star
16

rwkv_tokenizer

Python
6
star
17

static_test

Attribute macro for writing tests which check that a given condition ALWAYS holds true or that a given code path is ALWAYS unreachable
Rust
6
star
18

serde-bench

A simple benchmark of various Rust serialization frameworks
Rust
6
star
19

polkadoom

Yet another DOOM port
C++
4
star
20

nsctrld

Nintendo Switch Pro Controller userspace driver
Rust
4
star
21

linux-input

Rust
4
star
22

memory-pool

A global, thread-safe memory pool.
Rust
4
star
23

dspr

A DSP plugin server
C++
3
star
24

unsafe_target_feature

A more convenient `#[target_feature]` replacement
Rust
3
star
25

cargo-shim

A helper library for Cargo subcommands
Rust
2
star
26

mmalloc

A simple mmap-based allocator
Rust
2
star
27

chromium-build

Shell
1
star
28

softfloat

Rust
1
star
29

koute.github.io

JavaScript
1
star
30

linux-elf-loading-bug

Rust
1
star