61. O(1) slice and reverse views (2026-11-21)
s[a..b],xs[a..b],s.rev, andxs.revreturn O(1) read-only views (Ty::StrView,Ty::ListView<T>), never implicit copies; copying is explicit via.to_str()/.to_list().- Reason: By the rule "anything that looks O(1) is O(1)",
s[lo..hi]copying O(k) (and previously lowering to an O(k^2) push loop) ands.revmaterializing O(n) caused severe hidden blowups (cf2096eat 310x,ucup4s25hat 26.1x,cf2245aat 4.40x). - Representation:
- IR:
Ty::StrViewandTy::ListView(Box<Ty>), both wired intoTy::seq_elem()so they satisfySeqandIndexedwithout conversion. - Interpreter:
Value::StrView(Rc<StrViewVal>)andValue::ListView(Rc<ListViewVal>)carrying(base, lo, len, rev)with O(1) indexing, nested slicing, reverse, comparison, hashing, and iteration. - C++ codegen:
cpl::StrView(std::string_view sv; bool rev;) andcpl::ListView<T>(const T* ptr; size_t n; bool rev;) with index-based iterators (no pointer-before-start UB),CpHashoverloads, and explicitto_str/to_list.
- IR:
- Lifetime & mutability safety: Views are tracked as read-only lends in
cpl-check. Mutating the base while a view is live is diagnosed (E0602), mutating a read-only view is diagnosed (E0401), and escaping a view from its lending scope is diagnosed (E0603, suggesting.to_str()/.to_list()). Direct range-place updates (a[l..r] = 0,a[l..r] min= b[l..r]) remain in-place writes tovar a. - Visibility:
cpl explain <file>prints each lowered slice/reverse view (O(1), read-only) and explicit copy/concat (O(n), owned) with its C++ representation. - Reverse: Remove
Ty::StrView/Ty::ListViewand lower slices/.revback to allocatingstd::string/std::vectorcopies, dropping the read-only lend flags incpl-check/src/check/lends.rs.
- Reason: By the rule "anything that looks O(1) is O(1)",