106. Narrow 1D range-typed array and list elements to 32-bit storage (2026-11-21)
- Decision. Narrow 1D range-typed arrays and lists (
[V]lo..=hi,List<lo..=hi>, and domain-relative bounds[V]0..=V.count/[V]1..=V.count) tostd::vector<uint32_t>(when0 <= loandhi <= u32::MAX, or domain-relative0..=V.count/1..=V.count) orstd::vector<int32_t>(wheni32::MIN <= loandhi <= i32::MAX) across globals, locals/params, returns, and struct fields (Program::narrow_elems+scope::narrow_elems). A candidate stays narrow only if every flow (assignments, calls, returns,mutargs, struct constructors, closure captures, slices/views, non-natural.sort) lands in a place with the same narrowed element width; any flow into plain[V]Int/List<Int>or mismatched width widens it toll. Narrowed element reads widen viall(...)before arithmetic soa[i] * a[j]cannot overflow 32-bit.stdlib/tree.cp'sRooteduses[V]0..=V.countfordep/k0/k1and[V]1..=V.countforsz(farstays[V]Int), andexpect_declaredpropagates declared field element ranges into[D] _ => ...comprehensions soMarks<D>.stamp([D]0..=2_000_000_000) also narrows tostd::vector<uint32_t>. - Why. Extends ranged scalar struct-field narrowing (Decision 55) to 1D arrays and lists, makes the stdlib docs' 4-byte-per-element storage claim (
Marks<D>) true, and cuts memory on large trees (bench/trees/root_benchatn = 10^6:Rooteddropsdep/sz/k0/k1from 8 B to 4 B each, peak RSS ~123.7 MB / 0.35 s vs hand-written C++ 69.8 MB / 0.23 s). Stores are already validated byExprKind::RangeCheck(Decision 100), so checked builds trap on out-of-range writes while release stays zero-cost. - Reverse. Return an empty map from
scope::narrow_elemsincrates/cpl-codegen/src/scope.rs(or clearProgram::narrow_elemsincrates/cpl-ir/src/lower/mod.rs) to emit all ranged arrays/lists asstd::vector<ll>; optionally revertRooted'sdep/sz/k0/k1fields instdlib/tree.cpto[V]Int.