# The Fastest Linear Map in Rust
[](https://github.com/yegor256/micromap/actions/workflows/cargo.yml)
[](https://crates.io/crates/micromap)
[](https://docs.rs/micromap/latest/micromap/)
[](https://blog.rust-lang.org/2024/06/13/Rust-1.79.0.html)
[](https://github.com/yegor256/micromap/blob/master/LICENSE.txt)
[](https://codecov.io/gh/yegor256/micromap)
[](https://hitsofcode.com/view/github/yegor256/micromap)
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fyegor256%2Fmicromap?ref=badge_shield&issueType=license)
A much faster alternative of
[`HashMap`](https://doc.rust-lang.org/std/collections/struct.HashMap.html),
for very small maps.
It is also faster than
[FxHashMap](https://github.com/rust-lang/rustc-hash),
[hashbrown](https://github.com/rust-lang/hashbrown),
[ArrayMap](https://github.com/robjtede/tinymap),
[IndexMap](https://crates.io/crates/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`](https://doc.rust-lang.org/std/collections/struct.HashMap.html),
since the performance of `micromap::Map` _may_ start to degrade.
See the [benchmarking results](#benchmark) below.
First, add this to `Cargo.toml`:
```toml
[dependencies]
micromap = "0.1.0"
```
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](https://docs.rs/micromap/latest/micromap/).
The struct
[`micromap::Map`](https://docs.rs/micromap/latest/micromap/struct.Map.html)
is designed to be as closely similar to
[`std::collections::HashMap`][std] 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`][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.
| `flurry::HashMap` | 306.47 | 73.70 | 29.08 | 18.83 | 7.14 | 4.48 | 2.70 |
| `hashbrown::HashMap` | 20.72 | 9.55 | 4.80 | 3.13 | 1.08 | 0.55 | 0.32 |
| `heapless::LinearMap` | 1.44 | 1.32 | 0.87 | 1.19 | 0.80 | 1.01 | 1.02 |
| `indexmap::IndexMap` | 16.51 | 13.55 | 6.93 | 4.96 | 1.95 | 1.16 | 0.68 |
| `linear_map::LinearMap` | 1.69 | 1.37 | 0.75 | 0.87 | 0.67 | 1.02 | 0.98 |
| `linked_hash_map::LinkedHashMap` | 27.19 | 19.37 | 8.85 | 6.15 | 2.38 | 1.43 | 0.84 |
| `litemap::LiteMap` | 1.73 | 1.93 | 3.60 | 2.95 | 1.29 | 0.93 | 0.62 |
| `micromap::Map` 👍 | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 |
| `nohash_hasher::BuildNoHashHasher` | 20.25 | 10.16 | 4.98 | 2.93 | 1.03 | 0.68 | 0.39 |
| `rustc_hash::FxHashMap` | 20.14 | 9.93 | 4.56 | 2.73 | 0.86 | 0.53 | 0.31 |
| `std::collections::BTreeMap` | 21.44 | 13.51 | 7.05 | 5.71 | 2.00 | 1.28 | 0.84 |
| `std::collections::HashMap` | 20.50 | 12.19 | 6.24 | 4.36 | 1.67 | 1.03 | 0.60 |
| `tinymap::array_map::ArrayMap` | 3.22 | 3.91 | 2.97 | 4.22 | 3.49 | 4.47 | 5.05 |
The experiment [was performed][action] on 11-03-2026.
There were 1000000 repetitions.
The entire benchmark took 278s.
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.
## MSRV (Minimum Supported Rust Version)
**`Rust 1.79`**
(Enabling some features will affect MSRV, the documentation will note it.)
## How to Contribute
First, install [Rust](https://www.rust-lang.org/tools/install), update to the
last version by `rustup update stable`, and then:
```bash
cargo test -vv
```
If everything goes well, fork repository, make changes, send us a
[pull request](https://www.yegor256.com/2014/04/15/github-guidelines.html).
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:
```bash
cargo bench --bench bench
```
If you modified the comment docs, run this to check:
* Linux:
```bash
RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --all-features --no-deps
```
* Windows(PowerShell):
```PowerShell
$env:RUSTDOCFLAGS="--cfg docsrs"; cargo +nightly doc --all-features --no-deps --open; Remove-Item Env:\RUSTDOCFLAGS
```
Then, after the changes you make, run it again.
Compare the results.
If your changes degrade the performance,
think twice before submitting a pull request.
About the **version change**, we follow the rules of this
[Cargo SemVer reference](https://doc.rust-lang.org/cargo/reference/semver.html)
. If your code has an impact on semver compatibility, such as
**breaking changes**, then you may also need to explicitly upgrade the version.
Because our project version uses a placeholder, you can
_add a hint note after the version number `0.0.0`_ in Cargo.toml
`package.version` to mark that you want to update the version, which we call
"version hint", as follows:
```toml
[package]
name = "micromap"
version = "0.0.0" # hint: 1.2.3
# ...
```
If no version change is required, do not add any comments after the version
number `0.0.0`.
[std]: https://doc.rust-lang.org/std/collections/struct.HashMap.html
[rs]: https://github.com/yegor256/micromap/blob/master/tests/benchmark.rs
[action]: https://github.com/yegor256/micromap/actions/workflows/benchmark.yml