A much faster alternative of
HashMap
,
for very small maps.
It is also faster than
FxHashMap,
hashbrown,
ArrayMap,
IndexMap,
and all others.
The smaller the map, the higher the performance.
It was observed that when a 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.
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.
First, add this to Cargo.toml
:
[dependencies]
micromap = "0.0.15"
Then, use it like a standard hash map... well, almost:
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.
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.
2 | 4 | 8 | 16 | 32 | 64 | 128 | |
---|---|---|---|---|---|---|---|
hashbrown::HashMap |
21.74 | 12.00 | 6.82 | 3.87 | 1.29 | 0.61 | 0.32 |
heapless::LinearMap |
0.96 | 1.50 | 1.33 | 1.38 | 0.89 | 1.14 | 1.17 |
indexmap::IndexMap |
12.80 | 12.51 | 7.99 | 5.75 | 1.89 | 0.90 | 0.49 |
linear_map::LinearMap |
1.49 | 1.56 | 1.08 | 1.30 | 1.01 | 0.90 | 0.91 |
linked_hash_map::LinkedHashMap |
27.98 | 21.70 | 12.78 | 8.20 | 2.99 | 1.42 | 0.80 |
litemap::LiteMap |
1.55 | 2.12 | 1.68 | 1.57 | 1.02 | 0.78 | 0.54 |
micromap::Map 👍 |
1.00 | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 |
nohash_hasher::BuildNoHashHasher |
20.74 | 12.44 | 7.83 | 3.49 | 1.33 | 0.61 | 0.36 |
rustc_hash::FxHashMap |
20.96 | 12.19 | 6.93 | 4.72 | 1.10 | 0.53 | 0.30 |
std::collections::BTreeMap |
19.57 | 8.60 | 5.40 | 4.91 | 2.38 | 1.16 | 0.74 |
std::collections::HashMap |
21.54 | 14.65 | 9.15 | 5.80 | 2.16 | 1.05 | 0.58 |
tinymap::array_map::ArrayMap |
1.63 | 4.57 | 4.90 | 5.45 | 4.24 | 4.38 | 4.81 |
The experiment was performed on 16-07-2024. There were 1000000 repetition cycles. The entire benchmark took 196s. Uname: 'Linux'.
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.
First, install Rust and then:
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
.
Also, before you start making changes, run benchmarks:
rustup run nightly cargo bench
Then, after the changes you make, run it again. Compare the results. If your changes degrade performance, think twice before submitting a pull request.