Repository navigation
Description
Problem
read_to_end grows a full Buffer before determining whether the next read is EOF. Reading an empty file into a buffer with len=64 and cap=64 reallocates it to cap=4096 despite appending zero bytes.
Starting points: std/fs/file.wave.
Reproduction and measurement
Tested at 6257ba3eb4b9f1ab6c247a920c6e0c4b5d699331 on Linux amd64 with LLVM 21.1.8 and a compiler built from that checkout. Build it with LLVM_SYS_211_PREFIX=/usr/lib64/llvm21 cargo build --locked --jobs 2.
For a self-contained test against this checkout's std, create a temporary alias (leaving installed std unchanged):
python3 - <<'PYSETUP'
from pathlib import Path
import shutil
p = Path('/tmp/wave-issue-std')
shutil.copytree('std', p, dirs_exist_ok=True)
for f in p.rglob('*.wave'):
f.write_text(f.read_text().replace('"std::', '"checkout_std::'))
PYSETUPSave as repro.wave:
import("checkout_std::buffer::alloc")::{Buffer, buffer_new, buffer_resize, buffer_free};
import("checkout_std::fs::file")::{read_to_end};
fun main() -> i32 {
var b: Buffer = buffer_new(64);
buffer_resize(&b, 64, 7);
var n: i64 = read_to_end("/tmp/wave-audit-20260925/empty.dat", &b);
println("read={} len={} cap={}", n, b.len, b.cap);
buffer_free(&b);
return 0;
}
target/debug/wavec build repro.wave --dep checkout_std=/tmp/wave-issue-std -O0 --emit=ir,bin --out-dir /tmp/wave-issue-O0
/tmp/wave-issue-O0/repro
target/debug/wavec build repro.wave --dep checkout_std=/tmp/wave-issue-std -O2 --emit=ir,bin --out-dir /tmp/wave-issue-O2
/tmp/wave-issue-O2/reproBefore running, create the empty input: mkdir -p /tmp/wave-audit-20260925 && touch /tmp/wave-audit-20260925/empty.dat && truncate -s 0 /tmp/wave-audit-20260925/empty.dat. One execution per optimization level was recorded. Compare Buffer fields before and after the call; no wall-clock benchmark was performed.
Results
Both O0 and O2 print read=0 len=64 cap=4096; initial cap was 64. No latency reduction is claimed.
Scope
Avoid allocation and copying solely to discover EOF. Preserve existing contents, length rollback on errors and behavior for real additional data.
Difficulty: medium.
Completion criteria
- Do not grow the tested full buffer when the file is already at EOF.
- Cover empty files, exact-fit reads, one additional byte and multiple chunks.
- Verify allocation/read failures and data/length preservation.
- Measure allocation/growth counts; any timing claims require repeatable measurements.
Activity