Giter Club home page Giter Club logo

micromap's Introduction

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

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.

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.

2 4 8 16 32 64 128
hashbrown::HashMap 21.93 12.11 6.92 3.91 1.23 0.65 0.31
heapless::LinearMap 0.99 1.57 1.31 1.35 0.93 1.05 1.14
indexmap::IndexMap 12.44 13.56 8.14 5.05 1.80 0.99 0.50
linear_map::LinearMap 2.07 1.69 1.18 1.21 0.81 1.22 0.87
linked_hash_map::LinkedHashMap 26.76 22.23 12.60 7.84 2.81 1.54 0.77
litemap::LiteMap 1.57 2.08 1.78 1.49 0.96 0.88 0.55
micromap::Map ๐Ÿ‘ 1.00 1.00 1.00 1.00 1.00 1.00 1.00
nohash_hasher::BuildNoHashHasher 20.84 12.47 7.62 3.46 1.23 0.69 0.35
rustc_hash::FxHashMap 21.30 12.35 6.94 4.18 1.03 0.58 0.29
std::collections::BTreeMap 17.65 10.07 8.48 6.55 2.96 1.28 0.72
std::collections::HashMap 20.56 15.50 9.31 5.62 2.06 1.14 0.56
tinymap::array_map::ArrayMap 1.99 4.81 4.88 5.17 4.18 4.84 4.74

The experiment was performed on 31-03-2024. There were 1000000 repetition cycles. The entire benchmark took 197s. 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.

How to Contribute

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.

micromap's People

Contributors

yegor256 avatar renovate[bot] avatar rultor avatar alexkazik avatar zefick avatar pinkforest avatar flareflo avatar wiebecnossen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.