yegor256/micromap
À propos
📈 The fastest (for very small maps!) alternative of Rust HashMap, which doesn't use hashing and doesn't use heap (aka "linear map")
Apprentissage et éducationProgression mesurée des étoiles
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- +1
- in 1 month
- Historique en cours
Variations nettes sur des périodes qui se chevauchent, pas des totaux quotidiens. Les mesures absentes restent vides. Les étoiles indiquent l’intérêt, pas la qualité.
README.md
OriginalThe Fastest Linear Map in Rust
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.
In our measurements, when a map contains more than ~32 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.
First, add this to Cargo.toml:
[dependencies]
micromap = "0.1.0"
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 to be 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 a gain for micromap::Map (the baseline, 1.00),
while the numbers below 1.0 demonstrate a loss for it.
| 2 | 4 | 8 | 16 | 32 | 64 | 128 | |
|---|---|---|---|---|---|---|---|
flurry::HashMap |
435.91 | 114.30 | 30.46 | 30.61 | 14.01 | 7.63 | 2.67 |
hashbrown::HashMap |
31.71 | 11.71 | 3.65 | 3.52 | 1.31 | 0.68 | 0.21 |
heapless::LinearMap |
1.01 | 1.79 | 0.68 | 1.20 | 0.98 | 1.07 | 0.88 |
indexmap::IndexMap |
25.03 | 17.36 | 5.84 | 5.93 | 2.76 | 1.57 | 0.53 |
linear_map::LinearMap |
1.94 | 1.84 | 0.87 | 0.98 | 0.83 | 1.19 | 1.02 |
linked_hash_map::LinkedHashMap |
38.31 | 22.19 | 7.52 | 7.36 | 3.16 | 1.94 | 0.71 |
litemap::LiteMap |
1.91 | 2.72 | 3.76 | 5.69 | 2.32 | 1.45 | 0.59 |
micromap::Map 👍 |
1.00 | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 |
nohash_hasher::BuildNoHashHasher |
29.84 | 12.36 | 3.86 | 2.86 | 1.23 | 0.68 | 0.25 |
rustc_hash::FxHashMap |
29.65 | 12.26 | 3.67 | 3.15 | 1.07 | 0.56 | 0.21 |
std::collections::BTreeMap |
27.76 | 17.78 | 6.43 | 7.25 | 3.01 | 1.79 | 0.68 |
std::collections::HashMap |
31.64 | 15.84 | 5.09 | 5.16 | 2.51 | 1.36 | 0.48 |
tinymap::array_map::ArrayMap |
4.02 | 5.25 | 2.67 | 5.50 | 5.19 | 6.45 | 4.84 |
The experiment was performed on 03-11-2026. There were 1000000 repetitions. The entire benchmark took 191s. 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, update to the
last version by rustup update stable, 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:
cargo bench --bench bench
If you modified the comment docs, run this to check:
- Linux:
RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --all-features --no-deps
- Windows(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
. 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:
[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.
Explorer des projets similaires
Chargement…