18 stable releases

1.4.2 Oct 24, 2023
1.4.1 Dec 26, 2022
1.4.0 Jul 14, 2022
1.3.3 Mar 11, 2022
1.2.0 Nov 17, 2020

#1009 in Database interfaces

Download history 386/week @ 2024-08-04 83/week @ 2024-08-11 263/week @ 2024-08-18 82/week @ 2024-08-25 30/week @ 2024-09-01 31/week @ 2024-09-08 48/week @ 2024-09-15 58/week @ 2024-09-22 133/week @ 2024-09-29 47/week @ 2024-10-06 52/week @ 2024-10-13 113/week @ 2024-10-20 108/week @ 2024-10-27 216/week @ 2024-11-03 134/week @ 2024-11-10 195/week @ 2024-11-17

723 downloads per month
Used in 3 crates

MIT license

49KB
1.5K SLoC

smallmap

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.

Usage

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()
}

Use cases

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.

Maybe use if

  • You have small keys
  • Your map is not at risk of Denial of Service attacks.
  • Your keys are not expected to have a lot of collisions when represented as u8.

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.

Benchmarks

Some crude and basic benchmarks

char

Which ns/iter
HashMap 16
smallmap::Map 7

Iterating a string's chars and counting each

Which ns/iter (entry) ns/iter (get/insert)
HashMap 8,418 8,367
BTreeMap 9,742 6,329
smallmap::Map 4,416 1,739

u8

Which ns/iter
HashMap 15
smallmap::Map 2

License

MIT licensed

Dependencies

~170KB