Skip to content

Commit

Permalink
Fork indexmap into ringmap, based on VecDeque
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Jan 21, 2025
1 parent e87a15e commit 0ef5dc8
Show file tree
Hide file tree
Showing 37 changed files with 2,984 additions and 2,372 deletions.
26 changes: 5 additions & 21 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]
merge_group:

name: CI
Expand All @@ -17,16 +17,14 @@ jobs:
strategy:
matrix:
include:
- rust: 1.63.0 # MSRV
- rust: 1.68.0 # MSRV
features:
- rust: stable
features: arbitrary
- rust: stable
features: quickcheck
- rust: stable
features: rayon
- rust: stable
features: rustc-rayon
- rust: stable
features: serde
- rust: stable
Expand All @@ -40,11 +38,6 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
if: matrix.rust == '1.63.0'
with:
path: ~/.cargo/registry/index
key: cargo-git-index
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
Expand Down Expand Up @@ -72,18 +65,13 @@ jobs:
strategy:
matrix:
include:
- rust: 1.63.0
- rust: 1.68.0
target: thumbv6m-none-eabi
- rust: stable
target: thumbv6m-none-eabi

steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
if: matrix.rust == '1.63.0'
with:
path: ~/.cargo/registry/index
key: cargo-git-index
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
Expand Down Expand Up @@ -121,12 +109,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ~/.cargo/registry/index
key: cargo-git-index
- uses: dtolnay/rust-toolchain@nightly
- uses: dtolnay/rust-toolchain@1.63.0 # MSRV
- uses: dtolnay/rust-toolchain@1.68.0 # MSRV
- uses: taiki-e/install-action@v2
with:
tool: cargo-hack
Expand Down
18 changes: 7 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "indexmap"
name = "ringmap"
edition = "2021"
version = "2.7.1"
documentation = "https://docs.rs/indexmap/"
repository = "https://github.com/indexmap-rs/indexmap"
version = "0.1.0"
documentation = "https://docs.rs/ringmap/"
repository = "https://github.com/indexmap-rs/ringmap"
license = "Apache-2.0 OR MIT"
description = "A hash table with consistent order and fast iteration."
description = "A hash table with consistent deque-like order and fast iteration."
keywords = ["hashmap", "no_std"]
categories = ["data-structures", "no-std"]
rust-version = "1.63"
rust-version = "1.68"

[lib]
bench = false
Expand All @@ -22,10 +22,6 @@ serde = { version = "1.0", optional = true, default-features = false }
borsh = { version = "1.2", optional = true, default-features = false }
rayon = { version = "1.9", optional = true }

# Internal feature, only used when building as part of rustc,
# not part of the stable interface of this crate.
rustc-rayon = { package = "rustc-rayon", version = "0.5", optional = true }

[dependencies.hashbrown]
version = "0.15.0"
default-features = false
Expand All @@ -49,7 +45,7 @@ test_debug = []
debug = true

[package.metadata.release]
allow-branch = ["master"]
allow-branch = ["main"]
sign-tag = true
tag-name = "{{version}}"

Expand Down
64 changes: 23 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,41 @@
# indexmap
# ringmap

[![build status](https://github.com/indexmap-rs/indexmap/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/indexmap-rs/indexmap/actions)
[![crates.io](https://img.shields.io/crates/v/indexmap.svg)](https://crates.io/crates/indexmap)
[![docs](https://docs.rs/indexmap/badge.svg)](https://docs.rs/indexmap)
[![rustc](https://img.shields.io/badge/rust-1.63%2B-orange.svg)](https://img.shields.io/badge/rust-1.63%2B-orange.svg)
[![build status](https://github.com/indexmap-rs/ringmap/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/indexmap-rs/ringmap/actions)
[![crates.io](https://img.shields.io/crates/v/ringmap.svg)](https://crates.io/crates/ringmap)
[![docs](https://docs.rs/ringmap/badge.svg)](https://docs.rs/ringmap)
[![rustc](https://img.shields.io/badge/rust-1.68%2B-orange.svg)](https://img.shields.io/badge/rust-1.68%2B-orange.svg)

A pure-Rust hash table which preserves (in a limited sense) insertion order.
A pure-Rust hash table which preserves (in a limited sense) insertion order,
with efficient deque-like manipulation of both the front and back ends.

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
value. It preserves insertion order in most mutating operations, 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`](https://crates.io/crates/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:
This crate was forked from [`indexmap`](https://crates.io/crates/indexmap),
with the primary difference being a change from `Vec` to `VecDeque` for the
primary item storage. As a result, it has many of the same properties, as
well as a few new ones:

- 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.
- Efficient pushing and popping from both the front and back.
- Preserves insertion order **as long** as you don't call `.swap_remove_back()`
or other methods that explicitly change order.
- In `ringmap`, the regular `.remove()` **does** preserve insertion order,
equivalent to what `indexmap` calls `.shift_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.

[PR45282]: https://github.com/rust-lang/rust/pull/45282
`ringmap` also follows [`ordermap`](https://crates.io/crates/ordermap) in using
its entry order for `PartialEq` and `Eq`, whereas `indexmap` considers the same
entries in *any* order to be equal for drop-in compatibility with `HashMap`
semantics. Using the order is faster, and also allows `ringmap` to implement
`PartialOrd`, `Ord`, and `Hash`.

# Recent Changes

See [RELEASES.md](https://github.com/indexmap-rs/indexmap/blob/master/RELEASES.md).
See [RELEASES.md](https://github.com/indexmap-rs/ringmap/blob/main/RELEASES.md).
Loading

0 comments on commit 0ef5dc8

Please sign in to comment.