37 stable releases

2.6.0 Oct 1, 2024
2.3.0 Jul 31, 2024
2.2.6 Mar 23, 2024
2.1.0 Oct 31, 2023
0.4.1 Feb 14, 2018

#2 in Data structures

Download history 4148441/week @ 2024-07-31 4332600/week @ 2024-08-07 4319889/week @ 2024-08-14 4486637/week @ 2024-08-21 4305992/week @ 2024-08-28 4723289/week @ 2024-09-04 4410133/week @ 2024-09-11 4239161/week @ 2024-09-18 4789849/week @ 2024-09-25 5397377/week @ 2024-10-02 5569794/week @ 2024-10-09 5285029/week @ 2024-10-16 4681207/week @ 2024-10-23 4525581/week @ 2024-10-30 4491187/week @ 2024-11-06 4742949/week @ 2024-11-13

19,362,281 downloads per month
Used in 39,145 crates (2,320 directly)

Apache-2.0 OR MIT

365KB
7.5K SLoC

indexmap

build status crates.io docs rustc

A pure-Rust hash table which preserves (in a limited sense) insertion order.

This crate implements compact map and set data-structures, where the iteration order of the keys is independent from their hash or value. It preserves insertion order (except after removals), and it allows lookup of entries by either hash table key or numerical index.

Note: this crate was originally released under the name ordermap, but it was renamed to indexmap to better reflect its features. The ordermap crate now exists as a wrapper over indexmap with stronger ordering properties.

Background

This was inspired by Python 3.6's new dict implementation (which remembers the insertion order and is fast to iterate, and is compact in memory).

Some of those features were translated to Rust, and some were not. The result was indexmap, a hash table that has following properties:

  • Order is independent of hash function and hash values of keys.
  • Fast to iterate.
  • Indexed in compact space.
  • Preserves insertion order as long as you don't call .remove(), .swap_remove(), or other methods that explicitly change order. The alternate .shift_remove() does preserve relative order.
  • Uses hashbrown for the inner table, just like Rust's libstd HashMap does.

Performance

IndexMap derives a couple of performance facts directly from how it is constructed, which is roughly:

A raw hash table of key-value indices, and a vector of key-value pairs.

  • Iteration is very fast since it is on the dense key-values.

  • Removal is fast since it moves memory areas only in the table, and uses a single swap in the vector.

  • Lookup is fast-ish because the initial 7-bit hash lookup uses SIMD, and indices are densely stored. Lookup also is slow-ish since the actual key-value pairs are stored separately. (Visible when cpu caches size is limiting.)

  • In practice, IndexMap has been tested out as the hashmap in rustc in PR45282 and the performance was roughly on par across the whole workload.

  • If you want the properties of IndexMap, or its strongest performance points fits your workload, it might be the best hash table implementation.

Recent Changes

See RELEASES.md.

Dependencies

~0.5–1.6MB
~28K SLoC