#hash-table #hash-map #key-value #key-hash #hash-set #order #hash-values

no-std indexmap-amortized

A hash table with consistent order and fast iteration. The indexmap is a hash table where the iteration order of the key-value pairs is independent of the hash values of the keys. It has the usual hash table functionality, it preserves insertion order except after removals, and it allows lookup of its elements by either hash table key or numerical index. A corresponding hash set type is also provided. This crate is an ongoing fork of bluss/indexmap that provides amortized resizes.

3 stable releases

1.6.1 Dec 21, 2020
1.0.1 Jul 27, 2020

#2154 in Data structures

Apache-2.0/MIT

190KB
4K SLoC

indexmap-amortized

build_status crates 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.

This crate is an ongoing fork of indexmap_ that amortizes the cost of resizes. If you're unsure if you need this, take a look at the documentation of griddle and atone, which provide the underlying amortization.

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().
  • 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

  • 1.6.1
    • Crate is no longer nightly-only!
    • Synchronize with upstream version 1.6.1
  • 1.0.1
    • Note that crate is nightly-only.
    • Make benchmarks compile again.
  • 1.0.0
    • Initial release.

Dependencies

~1.2–1.6MB
~27K SLoC