You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.9 KiB

4 years ago
# smallmap
4 years ago
A small table map using single byte key indecies. Designed for maps with tiny keys.
4 years ago
4 years ago
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.
4 years ago
## Usage
The API is a similar subset to `HashMap`, containing the same `insert`, `get`, and `entry` functions:
``` rust
fn max_char(chars: &str) -> (char, usize)
{
let mut map = Map::new();
for x in chars.chars() {
4 years ago
*map.entry(x).or_insert(0usize) += 1;
4 years ago
}
4 years ago
map.into_iter().max_by_key(|&(_, v)| v).unwrap_or_default()
4 years ago
}
```
4 years ago
## Use cases
4 years ago
Designed for instances where you want a small map with relatively trivial keys (e.g. primitive type).
4 years ago
Performance can greately outpace hash-based by an order of magnitude or more in these cases.
4 years ago
4 years ago
### Maybe use if
* You have small keys
* Your map is not at risk of Denial of Service attacks.
* Your keys will have a lot of collisions
### Don't use if
* 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`.
4 years ago
4 years ago
# Benchmarks
4 years ago
Some crude and basic benchmarks
4 years ago
## char
| Which | ns/iter |
|-----------------|---------|
| `HashMap` | 16 |
| `smallmap::Map` | 7 |
4 years ago
4 years ago
## Iterating a string's chars and counting each
4 years ago
4 years ago
| Which | ns/iter (entry) | ns/iter (get/insert) |
|-----------------|-----------------|----------------------|
| `HashMap` | 8,418 | 8,367 |
| `BTreeMap` | 9,742 | 6,329 |
| `smallmap::Map` | 4,416 | 1,739 |
4 years ago
4 years ago
## u8
4 years ago
| Which | ns/iter |
|-----------------|---------|
| `HashMap` | 15 |
| `smallmap::Map` | 2 |
# License
4 years ago
MIT licensed