Files
aji/docs/rfcs/0028-owning-numeric-map.md
T
dev 57603860df cli: add safe owning numeric-key maps
Register std.map with bounded U64Map<V>, exact key equality and explicit
allocation/growth. Preserve returned ownership, old slots on reserve failure
and original-authority refunds; support Copy values, owners and resources.

Cover collision/tombstone/wrap behavior, independent dictionary traces,
quota/cleanup/borrow/task rules, source/AIR parity and hosted/package CLI.
Publish the API, roadmap and measured documentation cost. General key
hashing/equality remains Q-037; numeric maps do not satisfy that gate.
2026-10-07 08:36:23 +03:00

6.7 KiB

RFC 0028: Safe owning numeric-key map

Status: Proposed
Scope: STD-6d.2, safe standard source; general-key gate remains Q-037
Language version: 0.1

Motivation

Numeric identifiers need bounded lookup and storage without rebuilding ownership, quota and collision handling. std.hash supplies pure hashing; initialized buffers and whole-value exchange supply safe storage. Pure error construction (RFC 0027) permits size validation before allocation. No map-specific language feature or trusted runtime is needed.

Precise invariant

U64Map has 2N initialized slots for logical capacity N. Copy key/state fields permit observation with owning V. Each occupied slot holds exactly one some(V); vacancies hold none. Keys are unique and reachable through the probe chain, occupied count equals length≤N. Linear owners/resources are always transferred or explicitly consumed. Both state and option tags are charged; the owning-handle slot is 18 bytes under air64.

Normative rule changes

Spec 16 §2.13 registers std.map and its ten functions. It defines exact u64 equality, FNV-1a over eight little-endian key bytes, bounded linear probing, tombstones, ownership of inserted/replaced/full/removed/popped/not_empty results, complete failure preservation and explicit growth/cleanup. Reference/std.md publishes the API. General keys remain Q-037; this specialization does not satisfy HashMap<Key, Own>.

Verifier implications

No new verifier state or privileges. Ordinary generic constraints, field borrows, whole Option/Slot exchange, move/unpack, task loans and Copy/Send/Share apply. Only get requires V: Copy; contains needs no value copy. Library functions maintain the invariant; direct representation mutation is governed by spec 16. Existing diagnostic codes cover leaks, invalid copies/moves/aliases, task loans and missing effects.

Interpreter / runtime implications

Existing buffer/allocation semantics and deterministic fuel apply. New/reserve explicitly require mem.alloc authority/effect; insert never grows. Growth allocates and initializes the whole new table before mutation. Returned failure preserves all old slots, metadata, key/value contents and pop order. Peak quota includes both tables and user-owned allocations. Refund uses the original table's authority, even when another funds growth. Successful transfer uses old physical-index order and clears tombstones. Pop uses first occupied physical slot. No hidden resource close or destructor exists; corrupted empty metadata cannot cause normal destruction of an owner.

AIR changes

None. Lowered safe library source uses existing instructions, including error.const from the separately registered error_values prerequisite. Source integer-pattern canonicalization was repaired separately; no existing instruction semantics change.

Valid example

examples/std/map.aji creates a map of own, inserts a moved owner under key 7, removes and consumes it, and frees the drained map, returning 42. Tests also transfer files, sender endpoints and exclusive task loans.

Rejected example

tests/conformance/source/map/get_owner.aji attempts copying lookup for own (E7005). Other tests reject missing moves, discarded replaced/full/pop/not_empty owners, mutation during borrows/task loans, shared file-valued maps and growth without its allocation effect.

Diagnostics impact

No new codes. E7003/E7005, E2002/E3002, E2101, E4101, E4002 and E3101 retain their existing meaning. Broken caller representation may trap; valid library operations do not trap except execution limits. Recoverable failures are explicit returned data.

Compatibility and migration

New compiler-pinned source module only, no map-specific AIR feature. Existing std and AIR API meanings remain unchanged. Maps do not implicitly allocate, compact, iterate or expose escaping borrows. General HashMap/HashSet, BinaryHeap and the remaining Std 0.1 gates remain open.

Agent complexity impact

Library concepts: bounded exact-key map and returned-value ownership, reusing Vec/Deque result names and cleanup. Ten canonical operations, no aliases, overloads, traits, reflection, callbacks or new source syntax. The slot's metadata stays Copy while values may be linear; there is no new verifier state or repair diagnostic. Existing primitives suffice after the separately measured error_values prerequisite. Full contracts remain in the on-demand library page; no long Quick Reference tutorial is added. Measurements and final checks follow below; no MCC improvement is inferred from fixtures or documentation size.

Alternatives considered

A linear entry enum prevents read-only metadata observation with owning V. Digest equality would conflate colliding original keys. Automatic growth hides allocation/authority. Compacting on deletion changes pop order and increases mutation. Generic key hashing/equality needs a separate source contract, rather than map-specific compiler magic.

Unresolved questions

Q-037: general key hashing and equality. Numeric U64Map is explicitly a specialization.

Validation and measured documentation budget

zig build, zig build test, formatting and source coverage pass (98/98). All 30 map conformance cases pass with collection before every instruction; concurrent fixtures additionally use 12 schedule seeds under quanta 1, 2, 7 and their declared quantum. The independent Python dictionary oracle verifies 128 mixed operations across Copy and owning traces without modelling the hash/probe implementation. Source/package/hosted CLI checks pass (76), including exact source/AIR records, canonical lowering, import order and absence of trusted operations. Full conformance passes: 863 cases, 0 failed, 0 pending.

Relative to the error_values prerequisite (the source-doc difference also includes the separate integer-pattern clarification):

Group cl100k o200k Qwen2.5
Quick Reference 7,863 → 7,863 7,894 → 7,894 8,102 → 8,102
Other source docs 48,061 → 50,079 48,204 → 50,223 49,419 → 51,509
Full source excluding AIR 55,924 → 57,942 56,098 → 58,117 57,521 → 59,611
Library page 12,190 → 14,167 12,239 → 14,216 12,559 → 14,604

AIR remains 179,282 bytes / 19 files / 51 sections (source-bundle copy 46,300 / 46,451 / 47,800 tokens). Full source grows 3.6% incrementally and 70.5% cumulatively from the pre-stdlib cl100k baseline 33,981. The cumulative cost is above the existing review line: explicit ownership, failure preservation and peak accounting require a reviewable contract. No Quick Reference catalog, new source form or map-specific language semantics is added; the API remains on demand. This cost is documented for owner review and is not evidence of MCC improvement.