0246. Static constant vec literals
- Status: Proposed (pending maintainer)
- Date: 2026-11-15
- Plan: Static vec literal optimization
- Review: Compiler and backend
Context
Large constant vec literals used for binary searches are rebuilt on each call. ADR-0235 rejected static aliases because mutable vecs can escape; a conservative function-local proof can instead optimize a strictly read-only subset.
Decision
In the C backend only, a function IR prepass selects a
destination only when it has exactly one assignment, a
VecNew with nonempty, static constant items. Every
use must be either the direct base of Index or the
sole argument of Intrinsic::Len. Reject all other
uses, including copies, calls, aggregate storage, mutations,
returns, promotion, region carry and par uses. If the
proof is incomplete, retain the existing emission. Emit static
constant data and alias hv_N to a vec with that data,
its length, and capacity zero. Named for x in v
currently copies the handle in IR and therefore falls back;
index/len loops need no alias tracing and qualify directly.
The interpreter and its allocation behavior remain unchanged.
No whole-program alias analysis or cap-zero runtime changes are
needed. In particular cap zero is not an immutability guarantee:
VecSetUnique and BufWrite can write to
the buffer, so the use proof must reject both, not rely on
capacity.
Alternatives considered
- Keep allocating every literal: simple, but repeats work on lookup calls.
- Whole-program escape or alias analysis: admits more cases but adds complexity and must prove safety across calls and ownership boundaries.
- Change cap-zero runtime behavior: changes runtime semantics for every vec and still requires an explicit representation and mutation policy.
Consequences
Qualifying C lookups avoid repeated construction; other code stays unchanged. The proof is deliberately conservative and must cover every IR use, including future forms. Differential tests must check the interpreter, traps and behavior of rejected mutation and escape cases; measurements must justify implementation. This proposal revisits only the static-vec alternative discussed in ADR-0235; it does not alter ADR-0235's match lowering decision.