0053. Insertion-ordered map
- Status: Accepted; index probing superseded by 0063
- Date: 2026-10-31
- Plan: M6.6
- Review: Memory and representation
Context
Command-line frequency tables need fast lookup with reproducible first-seen output. Immutable snapshots must survive overwrites, unlike a vector's append tip rule (ADRs 0040/0041). The M6 shared plan reserves this ADR number.
Decision
map<K, V> is a seeded generic type;
map() needs inferred/annotated context. Keys are
integers, bool, str, bytes, path or newtypes of those. No indexing
or entry assignment: get, get_or,
contains, set, len,
is_empty, keys, values are
methods. Discarded set is E0711. Maps aren't printable.
set returns a value; overwriting keeps position.
for k, v in m freezes the head once and visits
insertion order. Equality ignores order and compares values
structurally. No remove, record keys or vector enumerate syntax in
v0.
C maps are one-word nullable header pointers. Headers own dense
typed entries
{uint32_t hash; uint64_t/hrt_str key; V value} and a
u32 open-addressed index. Linear probing uses power-of-two
capacity, load <=0.5 (index is 2x capacity) and UINT32_MAX
empties; more than 2^31 entries traps OOM. Length-mixed
word-at-a-time string hashing (FNV-1a for integer keys), fmix64
and HRT_MAP_SEED determines lookup only. Adversarial collision
resistance waits for M8.6. Sequence keys are copied into the
destination region only on insertion, never on hits. Key inputs
are not retaining flows; stored values are. Promotion deep-copies
keys, values, entries and index. Existing key storage is safe to
share on a table copy after base lifetime placement/promotion.
Extend ADR-0040's whole-function no-alias proof with
MapNew/MapSet. Only proven local self-set may mutate in place.
Reads don't alias their acyclic container; snapshots, fields,
helper calls and loop-head freezes reject reuse. Fallback copies
the table O(n), reported as map-set. Nested map
updates can copy the inner table each time. Interpreter uses
ordered entries plus a BTreeMap lookup index (never iterated);
unique updates mutate, ordinary updates clone.
A read-only map loop needs no additional handle alias: lowering freezes the evaluated head implicitly when the body contains no assignments (including captured block writes). This preserves unique accumulation followed by output, as in word-frequency. A body that may assign keeps the explicit freeze, rejecting in-place updates anywhere in that function; mutation during iteration still visits the original snapshot. This is a conservative syntactic proof, not liveness analysis. Keys/values materialize vectors of copied handles; retaining those vectors conservatively rejects map uniqueness too.
Alternatives considered
- Public mutation or dynamic tip overwrite: breaks immutable snapshots.
- Hash-index iteration: order depends on the hash seed.
- Version chains/persistent trie: more representation and lifetime machinery before a measured need; dense entries keep iteration simple.
- Record keys/index syntax: broader hashing/diagnostic surface than v0 needs.
Consequences
Direct local accumulation is amortized O(1); helpers/fields/snapshots have a documented copying limit. Seed tests pin ordering; poison tests pin key ownership; allocation bounds detect accidentally copying keys on every update. No new crate.