Design decisions

115. Extend element narrowing to 2D grids, nested sequences, and all-ranged tuple elements (2026-11-21)

  • Decision. Replace Program::narrow_elems's flat IntWidth with recursive NarrowLayout (Int(IntWidth), Tuple(Vec<IntWidth>), Seq(Box<NarrowLayout>)), narrowing multi-dimensional arrays ([R, C]lo..=hi -> cpl::Grid<uint32_t|int32_t, 2>), nested arrays/lists ([D][E]lo..=hi, [D]List<lo..=hi> -> std::vector<std::vector<uint32_t|int32_t>>), and sequences of all-ranged tuples ([V](r1, r2) -> std::vector<std::tuple<uint32_t|int32_t, ...>>). Flow analysis tracks (ElemLoc, depth) and widens the root container conservatively on any mismatch (plain [R, C]Int / [E]Int / [V](Int, Int) params, incompatible assignments/push/extend/for-binding, mut element escapes, or non-natural sort). Leaf Int and Tuple reads widen to ll / std::tuple<ll, ...> before arithmetic.
  • Why. Multi-dimensional DP tables, nested adjacency/bucket tables, and ranged pair lists are among the largest memory consumers in contest solutions; generalizing NarrowLayout handles all three with one recursive rule instead of separate passes, while preserving 64-bit arithmetic and interpreter/C++ parity.
  • Reverse. Restrict RangeInfo::narrow_elem_layout in crates/cpl-ir/src/lower/range.rs back to 1D Ty::Int sequences only.