Open and answer this issue on GitHub.
compiler, collect, views, diagnostics, performance, decision · opened 2026-11-16 · updated 2026-11-17 · 1 comment
Goal. A collection built only to be read once in order, or by position where the source already offers that, is reported by the compiler, with the view to use instead.
Why. On the yaml benchmark (callgrind, large input, checks off, 6.52 G instructions), chars().collect() into a Vec[scalar] costs 5.6 % (2.3 M allocations): the package’s fold, is_int and is_float each copy a whole scalar’s text into a fresh vector before reading it. Nothing told the author. nikaia fix and the copy diagnostics see clones of a value, not a collect that copies a view into a new vector.
Found (develop, target/fast/nikaia):
fn count_digits(s: String) -> i64 {
let cs: Vec[scalar] = s.chars().collect()
let mut n = 0
let mut i = 0
while i < cs.len() {
if cs[i] >= '0' && cs[i] <= '9' { n += 1 }
i += 1
}
return n
}
It compiles and prints 3 for "a1b22", with no word about the vector.
What is meant. A let v: Vec[T] = <seq>.collect() whose v is afterwards only (a) iterated once in order, or (b) read by len() and once each, front to back, by index, is a copy that the sequence itself makes unnecessary. The warning names the place of the collect and the uses that show it, and the help says what to write (for c in s.chars(), or the view with position once the decision on a text view with position and lookahead is built, see Related). A vector that is changed, kept past the function, handed on, or read out of order (lookahead v[i+1], going back) is not reported: the copy then buys something. Until a text view with lookahead exists, the lookahead case stays silent unless (c) applies, rather than recommending a form the language does not have.
(c) ASCII-only reads (rule B). When every element read from v is only compared with ASCII literals (==, !=, <, <=, >, >= against a literal below 0x80, or a range of such literals), the byte view s.bytes (ADR-320 D11, borrowed, no copy) gives the same result, peeks and rereads at the same position included: every byte of a non-ASCII scalar is >= 0x80 and fails each ASCII comparison exactly as the scalar does, so the loop stops at the same place, and i == v.len() holds at the end exactly when every scalar read was ASCII. The index must start at 0 or after a prefix known to be ASCII: in the function itself (i = 1 after s.starts_with("-")), or, for a start taken as a parameter, at every caller (starts_with("0x") then from = 2). The help suggests s.bytes[i] and s.bytes.len(). A vector whose elements are also pushed elsewhere, returned, compared with a non-ASCII value or used in arithmetic is not covered by (c).
To build.
crates/nikaia-std/src/compiler/, in Nikaia), find a local initialised from .collect() over a Seq and classify its uses as above.docs/specification/30-nikaia-tooling.md), with its text and help in the table.nikaia fix rewrites case (a), a single in-order loop over the vector, into a loop over the sequence.starts_with with an ASCII literal) in the same function, or a parameter whose every caller passes such a value (an interprocedural summary over the callers in the package).tests/compiler/tests/: case (a), case (b), case (c) (an ASCII-only loop that peeks v[i] twice and one starting after starts_with("-") are reported, with s.bytes in the help), and the silent cases (changed, kept, handed on, read v[i+1] with a non-ASCII comparison, an element copied out with push, a start after a non-ASCII prefix).Done when. The probe above gets the warning; the silent cases stay silent; nikaia fix turns case (a) into a loop over s.chars(); in the yaml package, is_float and digits_in (via its callers’ starts_with("0x")) are reported under (c) with s.bytes in the help, while fold (copies non-ASCII scalars out, hands the vector to skip_blanks) stays silent; the yaml package builds with no new warning other than the ones its own follow-up removes.
Related. A text view with position and lookahead (filed with this); the yaml copies in fold and the number checks (filed with this); #453; ADR-282 D, ADR-293 (chars() is a forward Seq).
Generated by Claude Code
Driver: #1932 is not queued, its form misses what a builder needs (nikaia run scripts/issue-check.nika -- 1932, docs/issues.md, #2003):
Needs, Part of or Found while line names it (docs/issues.md I4)Label swapped from ready to decision: add what is missing (a Needs #M line for an issue it waits for, or Part of #M), then set ready again.
Generated by Claude Code