On this page

0316. Zero-allocation view and iterator fusion in Core IR

Context

Tenon's text splitting methods (str/bytes .lines(), .split(sep), .words(), and str.chars()) and vector block methods (.filter, .map, .fold) are semantically eager value-returning operations (vec<T>). Prior to this decision:

  1. lower/stmt.rs (for_text) streamed only direct for x in s.{lines,split,words}() heads using Intrinsic::PartStep / Intrinsic::RawStep; for c in s.chars() still allocated a full vec<str> of every Unicode scalar.
  2. Any chained pipeline such as s.lines().filter { ... }.map { ... }.fold(init) { ... }, for x in s.split(",").filter { ... }, v.filter { ... }.map { ... }, or s.split(",").len() materialized every intermediate vec in the arena before the next stage ran.
  3. Escaping trt_text_parts calls (runtime/trt_seq.h) performed two full passes over the input (a counting pass followed by a filling pass), and single-byte split called the general trt_str_find instead of direct memchr.

Decision

Fuse view-producing and block-transforming pipelines directly during Core IR lowering (crates/tenon-compiler/src/lower/), and upgrade runtime/trt_seq.h to single-pass materialization with a direct single-byte memchr path:

  1. Cursor support for chars(): Add RawPart::Chars to ir::RawPart, emitted as trt_part_at(..., kind = 4) (mode = 0 Unicode scalars in runtime/trt_seq.h) and mirrored in interp/value.rs::raw_step. for c in s.chars() and chains rooted at .chars() stream without allocating a vec<str>.
  2. Producer -> Stage -> Sink lowering fusion:*
    • Producers: str/bytes .lines(), .split(sep), .words(), str.chars() (cursor loop via PartStep/RawStep), and any vec<T> expression (index loop 0..n).
    • Intermediate stages (0 or more): .filter { x -> cond } and .map { x -> expr }.
    • Non-materializing sinks (zero vec allocation):
      • for x in <chain> { body }
      • <chain>.fold(init) { acc, x -> body }
      • <chain>.len() (counts surviving items in an i64 accumulator while still executing all stage blocks in order)
      • <chain>.is_empty() (for pure producer without stage blocks, checks whether the first cursor step is invalid; with stage blocks, executes all stages to preserve traps and outer var writes)
    • Materializing sinks (zero intermediate vecs, single output vec):
      • <chain>.filter { ... } or <chain>.map { ... } when the outer value is used as a vec: emits one StmtKind::Loop pushing surviving/mapped elements into the single destination vec via tip Intrinsic::Push.
  3. Single-pass trt_text_parts and 1-byte memchr in runtime/trt_seq.h:
    • Replace the 2-pass count-then-fill loop in trt_text_parts with a single-pass trt_alloc buffer that grows in place at the arena top (doubling capacity on demand with zero copies and returning (trt_vec){data, count, 0}).
    • Add a direct memchr fast path in trt_parts_next for sep.len == 1.
  4. Semantics and parity invariants:
    • Strict L-to-R evaluation: Producer receiver, separator (for split), and fold initializer are evaluated once and frozen into temporaries before the fused loop starts, so outer var mutations in stage blocks never alter the source or separator.
    • UTF-8, NUL, and empty-separator edge cases: Identical to the unfused intrinsics (str.split("") yields leading "", each scalar, trailing ""; bytes.split(b"") yields the whole slice; lines() splits on LF and strips a single trailing LF; NUL bytes are preserved via bounded memchr).
    • Arena scratch resets: Because fusion emits ordinary structured Core IR (StmtKind::Loop), regions/place.rs automatically attaches a per-iteration scratch ScopeDecl and promotes only escaping values (fold accumulator or pushed elements).
    • Interpreter parity: Both backends execute the identical fused Core IR.

Alternatives considered

Consequences