3. Where the speed comes from

perennial@0.1.0Updated 4 min readView as markdown

The targets' own public claim: im's OrdMap is "2–3× slower" than std::collections::BTreeMap (docs/spec/performance-claims.md). The ceiling for every workload is std (no sharing at all); the field is imbl 7.0.2, im 15.1.0 and rpds 1.2.1. Predictions below are what the design is built to reach; chapter 6 says how they are measured, and the README publishes what was measured, losses included.

What a black-box probe showed first (2026-12-01)

Before any design, the targets were timed as black boxes (n = 100,000 shuffled u64, median of 5 in one process, this Mac under load 3–6; indicative only, not a published benchmark; ns per operation):

Workload std im-rc 15.1 imbl 7.0.2 rpds 1.2.1
OrdMap insert / lookup / remove 88 / 62 / 80 117 / 40 / 114 118 / 47 / 118 374 / – / –
OrdMap clone + 1 insert (100k map) – 446 250 –
HashMap insert / lookup / iterate 20–41 / 10 / – 94 / 30 / 31.8 51 / 17 / 7.5 102 / – / –
Vector push_back / index loop / iterate 1.6 / – / – 3.3 / 7.3 / 2.1 5.6 / 6.6 / 2.1 31.5 / – / –
Vector append of a 100-element vector (1,000×) – 11,271 9,425 –
Vector split_off near the end (1,000×) – 423 479 –

Consequence for the thesis: the "2–3× slower than BTreeMap" text is out of date for these versions. Both im 15.1 and imbl 7 OrdMaps are already within 1.35× of BTreeMap on insert and faster on lookup, so a 2× OrdMap headline is not credible and is not claimed. The headroom is in Vector (about 9–11 µs per append of a 100-element vector, index access ~7 ns against a cached-leaf floor near 1 ns), HashMap against im-rc (insert, lookup, iteration 2–4× behind imbl), and shared-path writes (clone + mutate).

OrdMap / OrdSet: a wide B+tree with in-place writes

  • Shape. Keys and values live only in leaves; inner nodes hold separator keys and child pointers. Leaves are wide (branching tuned by measurement, starting at 32–64 entries) so a lookup touches few cache lines and a clone shares whole leaves.
  • Copy on write by ownership. Insert/remove descend with make_mut: a uniquely owned path is edited in place (no allocation, as BTreeMap does); a shared path is copied once per node. A loop of inserts into a fresh map therefore costs about what BTreeMap costs.
  • Leaf-linked iteration without parent pointers. Iteration keeps a small fixed-depth stack of (node, index) pairs; the inner loop walks a contiguous leaf slice.
  • Bulk build. FromIterator over sorted input and union of disjoint ranges build leaves bottom-up instead of inserting one by one.
  • Prediction. Parity to 1.3× over im/imbl on insert and remove (they are already near BTreeMap, the ceiling); ≥ 1.5× im-rc on clone + mutate. Not a headline workload.

HashMap / HashSet: a CHAMP-style HAMT with inline buckets

  • Nodes sized by popcount. Each node has a 32-bit bitmap for inline entries and one for child nodes; storage is exactly as long as the populated slots (no fixed 32-slot arrays).
  • Inline entries first. Entries sit in the node until a slot collides, so most lookups end one level earlier than a pointer-per-slot trie.
  • Small collision buckets inline for full-hash collisions, compared by Eq only after the stored hash matches.
  • Hash once. The 64-bit hash is computed once per operation and consumed 5 bits per level.
  • Prediction. ≥ 2× im-rc/im on insert, lookup and iteration; 1.1–1.5× imbl. std HashMap (SwissTable) stays faster; reported as it is.

Vector: RRB tree with a tail and a cached focus

  • Tail buffer. push_back appends to an owned tail chunk; the tree is touched once per chunk. A head buffer gives push_front the same treatment.
  • Relaxed nodes only where needed. Nodes stay strictly balanced (index by shifting) until a concat makes them uneven; then a size table is attached to that node only.
  • Concat and split in O(log n) by the RRB rebalancing rule with a bounded search error.
  • Focus. Sequential index access and iteration cache the current leaf and its range, so consecutive indexes cost one bounds check, not a descent.
  • Prediction (headline). ≥ 5× im-rc and imbl on append/concat chains (O(log n) node merging instead of per-element moves), ≥ 2× on in-order index access, ≥ 1.5× imbl on push_back; ≥ 10× rpds on concat and split (rpds' vector has no O(log n) concat or split).

Clone + mutate (the persistent workload)

clone() is O(1) for every type (one reference-count increment). The realistic cost is "clone, then change a few entries", the pattern of cargo's resolver, which clones its context at every backtracking point. Measured as its own workload (chapter 6).

Honest limits

  • std collections are faster when nothing is shared; the README says so with numbers.
  • Arc flavours pay atomic reference counts; Rc flavours do not. Both are measured.
  • A wide B+tree copies more bytes per shared-path write than a narrow one; the branching is chosen by measuring clone+mutate, not just insert.