0046. Size whole reads and extend private arena buffers
- Status: Accepted
- Date: 2026-10-31
- Plan: M5.8
- Review: Memory and representation
Context
The 107,000,000-byte CP-5.3 whole-input CSV run peaks at
236,668 KiB against the strict <107,421 KiB gate.
read_all retains every geometric buffer in its arena;
earlier filled buffers alone total 134,152,192 bytes. Iteration
resets cannot reclaim them. ADR-0036 already reserves stable
contiguous address space.
Decision
Use fstat and lseek(SEEK_CUR) for a
regular-file remaining-size hint, adding unconsumed reader-buffer
bytes. Allocate one private arena buffer with that capacity
(minimum eight bytes). Unknown sizes start at 64 KiB plus buffered
bytes. All paths read until EOF, retry EINTR and preserve
read-error results.
When full, probe one byte on the stack before growing: an
exact-sized file must not double just to detect EOF. If more input
exists, extend the private buffer at the arena top with
hrt_alloc, without moving or copying its bytes. No
intervening operation allocates in that arena; assert this
invariant. This uses existing budget, poison, stats and reset
handling without a new memory class. Growth/shrinkage after the
hint, zero-size pseudo-files, short reads, nonzero offsets and
buffered tails remain ordinary read-to-EOF cases.
Only the final immutable value is published. Existing
snapshot/append rules are unchanged; this is not permission to
extend arbitrary published buffers. to_str validates
without copying, split/lines retain views, and lazy heads allocate
no vector. Both backend semantics and diagnostics remain
unchanged.
Alternatives considered
- malloc/realloc then adopt an owned block — adds ownership, budget and reset bookkeeping although the arena's stable address range already suffices.
- Size hint alone — fixes regular-file RSS but leaves pipe growth wasteful.
- mmap whole input — changing file contents could mutate immutable values; copying read results keeps ADR-0055's whole-read policy and avoids SIGBUS.
- Rewind/reset to replace the buffer —
unnecessary copies and awkward interaction with poison, stats and
HRT_NO_RESET; extending needs none.
Consequences
Whole reads retain only one buffer, including for pipes and growing files. Unknown input still reserves geometric spare capacity but only read bytes touch pages. Truncation may leave unused capacity; size is not an EOF promise. Tests pin single-buffer allocation, exact-size EOF, growth/shrink after the hint, buffered-view preservation, and differential regular/pipe input. The CP-5.3 evidence records RSS and timing.