| 1 | # indexmap |
| 2 | |
| 3 | [](https://github.com/indexmap-rs/indexmap/actions) |
| 4 | [](https://crates.io/crates/indexmap) |
| 5 | [](https://docs.rs/indexmap) |
| 6 | [](https://img.shields.io/badge/rust-1.82%2B-orange.svg) |
| 7 | |
| 8 | A pure-Rust hash table which preserves (in a limited sense) insertion order. |
| 9 | |
| 10 | This crate implements compact map and set data-structures, |
| 11 | where the iteration order of the keys is independent from their hash or |
| 12 | value. It preserves insertion order (except after removals), and it |
| 13 | allows lookup of entries by either hash table key or numerical index. |
| 14 | |
| 15 | Note: this crate was originally released under the name `ordermap`, |
| 16 | but it was renamed to `indexmap` to better reflect its features. |
| 17 | The [`ordermap`](https://crates.io/crates/ordermap) crate now exists |
| 18 | as a wrapper over `indexmap` with stronger ordering properties. |
| 19 | |
| 20 | # Background |
| 21 | |
| 22 | This was inspired by Python 3.6's new dict implementation (which remembers |
| 23 | the insertion order and is fast to iterate, and is compact in memory). |
| 24 | |
| 25 | Some of those features were translated to Rust, and some were not. The result |
| 26 | was indexmap, a hash table that has following properties: |
| 27 | |
| 28 | - Order is **independent of hash function** and hash values of keys. |
| 29 | - Fast to iterate. |
| 30 | - Indexed in compact space. |
| 31 | - Preserves insertion order **as long** as you don't call `.remove()`, |
| 32 | `.swap_remove()`, or other methods that explicitly change order. |
| 33 | The alternate `.shift_remove()` does preserve relative order. |
| 34 | - Uses hashbrown for the inner table, just like Rust's libstd `HashMap` does. |
| 35 | |
| 36 | ## Performance |
| 37 | |
| 38 | `IndexMap` derives a couple of performance facts directly from how it is constructed, |
| 39 | which is roughly: |
| 40 | |
| 41 | > A raw hash table of key-value indices, and a vector of key-value pairs. |
| 42 | |
| 43 | - Iteration is very fast since it is on the dense key-values. |
| 44 | - Removal is fast since it moves memory areas only in the table, |
| 45 | and uses a single swap in the vector. |
| 46 | - Lookup is fast-ish because the initial 7-bit hash lookup uses SIMD, and indices are |
| 47 | densely stored. Lookup also is slow-ish since the actual key-value pairs are stored |
| 48 | separately. (Visible when cpu caches size is limiting.) |
| 49 | |
| 50 | - In practice, `IndexMap` has been tested out as the hashmap in rustc in [PR45282] and |
| 51 | the performance was roughly on par across the whole workload. |
| 52 | - If you want the properties of `IndexMap`, or its strongest performance points |
| 53 | fits your workload, it might be the best hash table implementation. |
| 54 | |
| 55 | [PR45282]: https://github.com/rust-lang/rust/pull/45282 |
| 56 | |
| 57 | # Recent Changes |
| 58 | |
| 59 | See [RELEASES.md](https://github.com/indexmap-rs/indexmap/blob/main/RELEASES.md). |