cargo crates.io codecov Hits-of-Code Lines of code License docs.rs

A much faster alternative of HashMap, for very small maps. It is also faster than FxHashMap, hashbrown, ArrayMap, and nohash-hasher. The smaller the map, the higher the performance. When the map contains more than 20 keys, it may be better to use the standard HashMap, since the performance of micromap::Map may start to degrade. See the benchmarking results below.

The only important restriction is that both key and value must implement the Copy trait.

WELCOME: Not all functions that you might expect to have in a map are implemented. I will appreciate if you contribute by implementing these missing functions. Here is a full list of missed features.

First, add this to Cargo.toml:

toml [dependencies] micromap = "0.0.5"

Then, use it like a standard hash map... well, almost:

rust use micromap::Map; let mut m : Map<u64, &str, 10> = Map::new(); // allocation on stack m.insert(1, "foo"); m.insert(2, "bar"); assert_eq!(2, m.len());

Pay attention, here the map is created with an extra generic argument 10. This is the total size of the map, which is allocated on stack when ::new() is called. Unlike HashMap, the Map doesn't use heap at all. If more than ten keys will be added to the map, it will panic.

Read the API documentation. The struct micromap::Map is designed as closely similar to std::collections::HashMap as possible.

Benchmark

There is a summary of a simple benchmark, where we compared micromap::Map with a few other Rust maps, changing the total capacity of the map (horizontal axis). We applied the same interactions (benchmark.rs) to them and measured how fast they performed. In the following table, the numbers over 1.0 indicate performance gain, while the numbers below 1.0 demonstrate performance loss.

| | 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | | --- | --- | --- | --- | --- | --- | --- | --- | --- | | hashbrown::HashMap | 255K | 255K | 3.54 | 2.29 | 1.25 | 0.47 | 0.22 | 0.12 | | linked_hash_map::LinkedHashMap | 438K | 440K | 8.06 | 5.21 | 3.08 | 1.35 | 0.64 | 0.37 | | nohash_hasher::BuildNoHashHasher | 197K | 197K | 3.68 | 2.74 | 1.02 | 0.43 | 0.20 | 0.11 | | rustc_hash::FxHashMap | 197K | 197K | 3.43 | 2.22 | 1.37 | 0.44 | 0.21 | 0.12 | | std::collections::BTreeMap | 349K | 358K | 5.03 | 3.31 | 2.53 | 1.16 | 0.55 | 0.34 | | std::collections::HashMap | 300K | 301K | 5.90 | 3.92 | 2.26 | 1.06 | 0.51 | 0.26 | | tinymap::array_map::ArrayMap | 13K | 16K | 2.13 | 2.36 | 2.25 | 2.15 | 2.00 | 2.04 |

There were 1000000 repetition cycles. The entire benchmark took 263s.

As you see, the highest performance gain was achieved for the maps that were smaller than ten keys. For the maps of just a few keys, the gain was enormous (most probably due to compiler optimizations).

How to Contribute

First, install Rust and then:

bash $ cargo test -vv

If everything goes well, fork repository, make changes, send us a pull request. We will review your changes and apply them to the master branch shortly, provided they don't violate our quality standards. To avoid frustration, before sending us your pull request please run cargo test again. Also, run cargo fmt and cargo clippy.