Skip to content

Avoid growing read_to_end buffers only to discover EOF #757

Closed

Description

@LunaStev

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::'))
PYSETUP

Save 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/repro

Before 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

  1. added
    help wanted
    performance
    needs testing
    on Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedneeds testingperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions