| 1 | # ordermap |
| 2 | |
| 3 | [](https://github.com/indexmap-rs/ordermap/actions) |
| 4 | [](https://crates.io/crates/ordermap) |
| 5 | [](https://docs.rs/ordermap) |
| 6 | [](https://img.shields.io/badge/rust-1.85%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 in most mutating operations, and it |
| 13 | allows lookup of entries by either hash table key or numerical index. |
| 14 | |
| 15 | Note: this crate was originally what became the `indexmap` crate, and |
| 16 | it was deprecated for a while in favor of that, but then `ordermap` |
| 17 | returned as a wrapper over `indexmap` with stronger ordering properties. |
| 18 | |
| 19 | # Background |
| 20 | |
| 21 | This was inspired by Python 3.6's new dict implementation (which remembers |
| 22 | the insertion order and is fast to iterate, and is compact in memory). |
| 23 | |
| 24 | Some of those features were translated to Rust, and some were not. The results |
| 25 | were `ordermap` and `indexmap`, hash tables that have following properties: |
| 26 | |
| 27 | - Order is **independent of hash function** and hash values of keys. |
| 28 | - Fast to iterate. |
| 29 | - Indexed in compact space. |
| 30 | - Preserves insertion order **as long** as you don't call `.swap_remove()` |
| 31 | or other methods that explicitly change order. |
| 32 | - In `ordermap`, the regular `.remove()` **does** preserve insertion order, |
| 33 | equivalent to what `indexmap` calls `.shift_remove()`. |
| 34 | - Uses hashbrown for the inner table, just like Rust's libstd `HashMap` does. |
| 35 | |
| 36 | Since its reintroduction in 0.5, `ordermap` has also used its entry order for |
| 37 | `PartialEq` and `Eq`, whereas `indexmap` considers the same entries in *any* order |
| 38 | to be equal for drop-in compatibility with `HashMap` semantics. Using the order |
| 39 | is faster, and also allows `ordermap` to implement `PartialOrd`, `Ord`, and `Hash`. |
| 40 | |
| 41 | ## Performance |
| 42 | |
| 43 | `OrderMap` derives a couple of performance facts directly from how it is constructed, |
| 44 | which is roughly: |
| 45 | |
| 46 | > A raw hash table of key-value indices, and a vector of key-value pairs. |
| 47 | |
| 48 | - As a wrapper, `OrderMap` should maintain the same performance as `IndexMap` |
| 49 | for most operations, with the main difference being the removal strategy. |
| 50 | - Iteration is very fast since it is on the dense key-values. |
| 51 | - Lookup is fast-ish because the initial 7-bit hash lookup uses SIMD, and indices are |
| 52 | densely stored. Lookup also is slow-ish since the actual key-value pairs are stored |
| 53 | separately. (Visible when cpu caches size is limiting.) |
| 54 | - In practice, `OrderMap` has been tested out as the hashmap in rustc in [PR45282] and |
| 55 | the performance was roughly on par across the whole workload. |
| 56 | - If you want the properties of `OrderMap`, or its strongest performance points |
| 57 | fits your workload, it might be the best hash table implementation. |
| 58 | |
| 59 | [PR45282]: https://github.com/rust-lang/rust/pull/45282 |
| 60 | |
| 61 | # Recent Changes |
| 62 | |
| 63 | See [RELEASES.md](https://github.com/indexmap-rs/ordermap/blob/main/RELEASES.md). |