Crates.io | smallmap |
lib.rs | smallmap |
version | 1.4.2 |
source | src |
created_at | 2020-09-20 12:30:38.910899 |
updated_at | 2023-10-24 13:50:19.541867 |
description | Small byte-sized generic key-value map type |
homepage | https://git.flanchan.moe/flanchan/smallmap |
repository | https://github.com/notflan/smallmap |
max_upload_size | |
id | 290758 |
size | 49,809 |
A small table map using single byte key indecies. Designed for maps with tiny keys.
Pages are stored as 256 entry key-value arrays which are indexed by the byte key index. The key is compared for collision check and on collision the next page is checked or inserted if needed.
smallmap
does not ever need to allocate more than 1 page for types which all invariants can be represented as unique bytes.
The API is a similar subset to HashMap
, containing the same insert
, get
, and entry
functions:
fn max_char(chars: &str) -> (char, usize)
{
let mut map = Map::new();
for x in chars.chars() {
*map.entry(x).or_insert(0usize) += 1;
}
map.into_iter().max_by_key(|&(_, v)| v).unwrap_or_default()
}
Designed for instances where you want a small map with relatively trivial keys (e.g. primitive type). Performance can greately outpace hash-based by an order of magnitude or more in these cases.
u8
.You have complex keys
Denial of service is a concern
Your map will contain a large volume of entries
Your keys may have a large number of collisions when represented as u8
.
Some crude and basic benchmarks
Which | ns/iter |
---|---|
HashMap |
16 |
smallmap::Map |
7 |
Which | ns/iter (entry) | ns/iter (get/insert) |
---|---|---|
HashMap |
8,418 | 8,367 |
BTreeMap |
9,742 | 6,329 |
smallmap::Map |
4,416 | 1,739 |
Which | ns/iter |
---|---|
HashMap |
15 |
smallmap::Map |
2 |
MIT licensed