0054. Streams, volatile views and raw-byte text methods
- Status: Accepted; Superseded in part by ADR-0061 (buffer retention and slot release)
- Date: 2026-10-31
- Plan: M6.2
- Review: Fast command-line tools first; extends ADRs 0036/0037/0038/0050
Context
A tool must read a file larger than memory without one
allocation per line, yet a line is an ordinary immutable
str. Input is arbitrary bytes, so text methods cannot
assume UTF-8, and C and the interpreter must print the same
bytes.
Decision
Stdin and File share
lines(), byte_lines(),
chunks(n), read_all() and
read_text(). As a direct for head the
first three step lazily; anywhere else they read everything and
materialise a vector. There is no iterator protocol.
Volatile views. A lazy binder points into a
reader buffer, in no arena. It is a new region class: origin is
the enclosing loop's iteration scope, that loop always gets a
scope, and home raising does not apply because there is no storage
to place. Every retaining flow to an older home therefore inserts
Promote; views of views follow through ordinary
aliases. Same-iteration scratch (line.split(","))
stays zero-copy.
Head tokens. A step normally compacts the buffer, which destroys the previous view. That is sound only if nothing else read the reader since, so each head entry mints a token at run time. A step with another token pins the buffer: the next refill moves to a new one and leaves old bytes in place. Tokens are per entry, not per loop, because a recursive call re-enters the same loop. Whole reads consume buffered bytes without touching them. An abandoned buffer stays in the persistent arena until exit, so alternating heads on one reader cost one buffer per refill. A single head reuses its buffer, which doubles only for a line longer than itself.
IR. A step and its view are two operations,
ReadLine/ReadChunk returning bool and
ReadView returning the view, so regions see a plain
view and cgen needs no per-layout record. read_all
reports through ReadStatus the same way; lowering
builds the CliError, so its text is identical on both
backends.
Splitting. lines splits on LF
only for streams and str, keeps CR, and a final
terminator yields no empty line. chunks(n) yields
exactly n bytes until the end, so boundaries never
depend on how the OS split the reads. words() splits
on ASCII whitespace.
Failures. Open and whole-read failures are
result; discarding one is E0902. A failure during a
lazy head traps io error, and invalid UTF-8 in
lines() traps invalid utf-8, because a
result per line would burden every loop for a case
tools handle by choosing byte_lines().
read_text() on invalid UTF-8 is Failed.
Failed stdout writes never return a value: EPIPE gives 141, others
print error: cannot write stdout and exit 1.
Files. A File is a handle into
the same reader table as stdin, so every stream operation is
shared. fs.open fails on a directory itself, so no
reader exists that cannot read. fs.list returns
entries joined to its argument, sorted by bytes: a tool can open
them directly, and file_name() recovers the bare
name. close never frees a buffer, because a view from
the current iteration may still be live, so opening many files
costs one buffer each until exit. fs.read releases
its table slot, as no handle to it escapes. Readers remember their
path to name it in errors raised after open.
Raw-byte methods. bytes gains the
str text methods with bytes needles.
trim and words use ASCII whitespace,
split on an empty needle yields the whole value, and
count of an empty needle is one more than
len.
Alternatives considered
- Iterator protocol — needs generic traits the
kernel lacks, and every tool only needs
for. - A
resultper line — every loop body gains a match for an error it cannot usefully handle. - Lossy decoding — silently rewrites data;
byte_lineskeeps it exact. linesretaining terminators — every caller must strip them.- Per-loop tokens — a recursive head would compact under its caller's view.
- Status record per step — one more layout and a wrapper per element type.
Consequences
A 1 GiB lines() run stays within a small constant
of memory unless the program retains lines. Unconditional carries
(last = line) copy every iteration into the older
region, as any carry does. A miss in the retaining-flow rule would
be a dangling view, so Class has no wildcard and
HRT_POISON erases each dead view at the next step. A
process started with stdin or stdout closed differs: C sees EBADF,
while the interpreter's reads come back empty and its writes
succeed.