6.1 KiB
RFC 0030: Safe owning general-key hash map
Status: Proposed Scope: STD-6d.4, ordinary safe library source after RFC 0029
Motivation
Arbitrary keys need exact equality and explicit hash policy. Numeric-key U64Map cannot represent bytes/structs/owners by digest alone. Static functions supply reusable typed operations without container-specific language rules.
Invariant and normative changes
Spec 16 §2.14 defines std.hashmap.HashMap<K,V,H,E>, dense initialized complete entries and Copy probe metadata. Every live entry has one reachable occupied bucket; cached hashes and equality agree. Keys and values may both own resources, with no vacancy key/default allocation. The registry/reference/API changes land together.
Verifier rule
No new transfer function. Complete buffer transfer/pop/exchange, borrowed fields, exact callable contracts, normal effects/capabilities, Copy/Send/Share and ownership verification apply. Representation/hash-law preconditions are explicit, as for existing containers. No manual unsafe implementation is allowed.
Runtime and deterministic behavior
Ordinary AIR. Probes are bounded; replacement/full/removal/free return complete owners. Allocation failure rolls back temporary empty storage, preserving old buffers/metadata/pop order. Explicit peak charges include both layouts and separately owned payloads. Removal swaps the last dense entry into a hole and repairs Copy indices. Rehash uses cached hashes after allocation succeeds; no recoverable failure follows the first old-buffer mutation. Normal profiles agree; fuel/callback traps are abnormal outcomes.
Valid and rejected examples
A struct key containing an owner and a Copy identity field supplies pure ref-key hash/equality functions; insert transfers it with an owned value, removal/pop returns both for explicit consumption. Copy-value lookup does not need Copy keys. Missing moves, leaked full/replacement/pop/nonempty-free results, illegal aliases/loans, Copy lookup of owning V and sharing non-Share contents reject through existing codes. Mismatched static policy identities reject E7005.
Diagnostics / compatibility / AIR
No new diagnostic, opcode, AIR feature or runtime boundary. Existing E7003/E2002/E3002/E2101/E4101/E7005/E4002 and ordinary call/effect checks apply. The new module is additive; std.map.U64Map and every existing API retain meaning. No implicit growth, destructor, retained borrow or authority is introduced. Spec 15 §17 clarifies the existing distinction between a Copy value bound in frame storage and internal storage/scope/guard/shield handles; ordinary Copy loading is unchanged.
Agent complexity impact
No new language concept, syntax or verifier/runtime state beyond already specified static operations. Ten functions match the numeric-map operation set; four explicit arguments name type/hash/equality policies, and linear replacement returns both old resources. Algebraic hash/equality and visible dense/index invariants add documentation obligations, which are kept on demand and measured below. Existing safe primitives suffice without borrowing enum payloads or partial moves. No MCC or throughput benefit is claimed from fixtures.
Alternatives
Digest identity loses collisions. Per-key duplication loses general reuse. A vacant Option table cannot observe owning keys through existing Copy enum switching. Two dense/probe buffers need no default key, borrowed enum feature, private representation privilege or unsafe pointer.
Validation gate
Independent dictionary/model traces, exact collisions, tombstones/wrap, full replacement, moving-hole repair, zero capacity, complete owners/files/senders, allocation rollback/peak/refunds, immutable/exclusive/task loans, nominal policies, invalid representation safety, source/AIR/profile/package consistency and GC/schedule stress are required before completion. HashSet and BinaryHeap remain separate increments.
Documentation budget
Relative to the static-operation prerequisite (RFC 0029), all full-source counts exclude AIR:
| Group | cl100k before → after | o200k before → after | Qwen2.5 before → after |
|---|---|---|---|
| Quick Reference | 8,051 → 8,124 | 8,082 → 8,156 | 8,300 → 8,374 |
| Other source | 51,002 → 53,363 | 51,150 → 53,537 | 52,468 → 54,845 |
| Full source | 59,053 → 61,487 | 59,232 → 61,693 | 60,768 → 63,219 |
cl100k growth is +0.9% / +4.6% / +4.1%. library/std.md is 16,504 cl100k tokens. AIR remains 179,282 bytes / 19 files / 51 sections (46,300 cl100k in the source bundle). No normative AIR material is needed. Full source growth from the pre-stdlib 33,981-token baseline is +80.9%; the justification is a general owning-key map meeting the original HashMap<Key, Own> gate, exact failure/resource contracts and a separate numeric API retained for compatibility. The Quick Reference has one compact paragraph; policies/invariants/accounting stay on demand. Selective reference reading/consolidation remains separate work; no empirical MCC benefit is claimed.
Validation result
The complete conformance corpus passes 947 cases, including 33 general-map cases. Unit tests, Quick Reference checking, formatting, reference-bundle generation, source coverage (104/104 constructs) and 55 CLI/source/AIR/profile/package/provenance checks pass. Independent Python dict/dense-order oracles cover 192 public operations across Copy and owning-value traces, checking every key and complete returned owners. Tests cover owning byte-buffer keys, owning key/value replacement/full returns, files, sender task transfer, shared lookups, exact allocation peaks, both reserve-failure phases, new-allocation rollback, cross-authority refunds and invalid-metadata nonempty free. The focused collection-before-every-step/scheduler-stress process has completed: all 33 general-map cases pass, including both independent owning/Copy model traces; scheduled fixtures run 12 seeds under quanta 1, 2, 7 and their own quantum. The process used no wall-clock termination. Subsequent set/heap regression passes the full 1007-case corpus. General key-policy/storage validation is complete on this branch (Q-037); RFC status remains Proposed for owner review.