On this page

0040. Vectors and for loops

Context

Review found quadratic set memory, repeated lazy searches and costly gcc -O2 str literals (bench/vec-resources).

Decision

vec<T> is seeded, structurally equal, retaining elements (ADRs 0037/0038). [a, b,] evaluates left to right; [] needs context (E0602). Type arguments participate in E0410 cycle checks. C uses type-erased hrt_vec, interp SeqBuf::Items; slices are same-type views. Constant scalar/str literals use static const arrays copied into writable arena storage by hrt_vec_new. Array names use a per-function counter, not possibly repeated IR source positions.

for is hard, in contextual. Heads accept vec/bytes/half-open i64 ranges, evaluated once, left to right; binders are immutable without shadowing. Advance before the body; break/continue target the innermost loop. Ranges aren't values; str iteration is E0716, malformed heads E0717. Amend ADR-0017: each for head (including its final test) charges fuel at the keyword; generated work charges none. Lazy lines/split compute one record step per iteration on both backends, then project valid/value/next, sharing materialized semantics without a vector.

Literals inline at 100 columns; comments/overflow force trailing-comma lines. Loop heads use for x in v { and for i in a..b {.

Set normally copies O(n). A narrow whole-function IR proof permits O(1) direct self-set: a named local starts with VecNew, every assignment is VecNew, VecConcat (fresh copy), or self-set/push. Every use must read elements/scalars or perform those self-updates/copies. Any container snapshot, slice, aggregate capture, call argument or incoming alias rejects it everywhere, including across branches/loops. Element reads cannot alias their acyclic container. Placement still promotes stored elements. The append tip rule is insufficient: it protects only the appended suffix, not existing slots. A direct return is terminal and allowed: earlier aliases still reject the whole local; no subsequent update in that frame can affect the returned value. Return placement/promotion remains unchanged, including early returns from loops.

Amend ADR-0036: default pages to 8 GiB (20M pushes exceeded the old 256 MiB cap). Read HRT_MEMORY_LIMIT once before first commitment: decimal bytes, optional K/M/G (case-insensitive, ×1024); invalid/overflowing settings use the C default. Env overrides C for run/built binaries. Check before mprotect; reset returns budget. OOM traps/exit 101 at allocation (push/set receiver); resource-dependent OOM is excluded from parity. Interp has no arena budget; OS pressure can still kill.

Alternatives considered

Consequences

Get/first/last return option; set checks i64 bounds; contains is structural; concat copies both; vec.join inserts separators. Unused updates: E0711. Qualified/custom methods evaluate ordinarily. ADR-0041 owns push (owner's override of the original separate vec/for ADRs). Open: last-use reuse beyond this proof; fallback carries retain O(iterations * length) storage. Pathological for-nest C size needs nontrivial loop/region work.