Skip to content

perf(io): File.read_line keeps a buffer per handle and answers a buffered line inline - #2178

Merged
nicolas-abril merged 4 commits into
mainfrom
perf/read-line-buffer
Nov 16, 2026
Merged

nicolas-abril merged 4 commits into
mainfrom
perf/read-line-buffer

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

What

On the C lane, File.read_line made one read(2) per byte. Now each handle that read_line reads keeps a 16 KiB buffer of its own. One read(2) fills it with whatever is available, so a pipe or a terminal still answers each line as soon as it arrives. If a whole line is already in the buffer, read_line builds the String straight from the buffer and answers right away, instead of sending the work to a helper thread and back. The loop still polls its timers and helper results every 64 answers, as it does for any effect that answers at once, and a refill always goes through the helper. A read that does reach the fd calls io_sync() first, as #2155 requires.

The buffered bytes belong to the handle, not the fd:

  • File.read and File.read_bytes answer the buffered bytes first. File.read_at and File.size are unchanged.
  • File.write and File.write_at first seek back to the end of the last line read, so on an r+ handle the write lands right after it. This works on any fd that seeks, including block devices. The buffer is dropped only if the seek lands where it was asked. A pipe, a terminal, or a device whose seek doesn't move keeps its buffer, because its reads and writes don't share a position.
  • File.close does the same seek-back, then frees the buffer. File.open drops any stale buffer for an fd number being reused.

Nothing seeks back during reads, so this has none of the failure modes #2157 had: a seek that fails after the bytes are read, a seek that succeeds without moving, or pipes getting slower. Supersedes #2157, and tests/io/file_read_line_rest.bend is taken from it.

Why

A loop that counts the lines and characters of a file with File.read_line(f, 65536), built with bend rl.bend -o rl. All builds print the same counts on every run.

Linux (Arch, kernel 7.2), AMD Ryzen 7 5800H. The main and #2157 columns are medians of 3 interleaved runs; this PR's column is the median of 5, from a later run on the same machine and inputs:

input main #2157 this PR vs main
file, 200k lines (9.7 MB) 8.37 s 2.45 s 0.073 s 115x
file, 10k lines of ~4 KB (42 MB) 26.66 s 0.51 s 0.311 s 86x
./rl /dev/stdin < file, 200k lines 7.88 s 2.49 s 0.073 s 108x
cat file | ./rl /dev/stdin, 200k lines 7.14 s 7.46 s 0.070 s 102x
cat file | ./rl /dev/stdin, 10k × 4 KB 22.53 s 23.44 s 0.291 s 77x

macOS, Apple M4 Pro. Here main and #2157 took 4.23 s and 1.05 s for the 200k-line file, and 4.62 s and 4.77 s for 200k lines from a pipe; an earlier commit of this PR (5f05082) took 0.059 s and 0.060 s. The current head beat that commit by 1.2–1.8x in every case in an interleaved run, but the Mac was under load then (load average 6–7), so I'm not quoting its absolute times.

Most of the gain comes from answering buffered lines on the loop. With the buffer alone and every line going through the helper, the 200k-line file took 0.94 s on macOS.

The buffer is 16 KiB. On macOS, comparing 4, 8, 16 and 64 KiB, short lines ran within 0.054–0.070 s at every size. Lines of about 4 KB took 0.25–0.32 s at 4 KiB, 0.18 s at 16 KiB and 0.15 s at 64 KiB. On Linux, 16 KiB was 5–9% slower than 64 KiB. 16 KiB holds a quarter of the memory per handle, which is in line with common runtime defaults (4–16 KiB).

How to test

  • tests/io/file_read_line_write.bend (new): on an r+ handle, it reads lines, writes between them and reads the rest, then checks the whole file through a fresh handle. On the C lane it fails if the seek-back before a write is removed.
  • tests/io/file_read_line_stdout_order.bend (new): a read_line on a handle opened before an IO.print must flush the held text, even while another fiber keeps the loop away from its poller. On the C lane it interposes fflush and read(), and the read fails the program if stdout was not flushed: without the io_sync(), it fails every time. The JS twin passes the file through, so every lane prints the same lines.
  • tests/io/file_read_line_rest.bend (from perf(io): File.read_line reads a regular file ahead and seeks back past its line #2157): read_line cut at max, lines longer than one chunk, then File.read for the rest.
  • All tests/io/file_*, read_bytes and stdout_file_order tests pass on the C lane on macOS, and the read_line tests also pass on the interpreted and JS lanes. On Linux they pass too, except stdout_file_order, which fails the same way on main when stdout is redirected to a file: opening /dev/stdout with "w" truncates it there.

Risk

  • Bytes read ahead now belong to the handle, which is the same trade-off as stdio, Rust's BufReader and Python's buffered IO:
    • On a pipe or a terminal, bytes still buffered when the handle is closed are lost. For example, in printf 'a\nb\n' | (./prog /dev/stdin; cat), where prog reads one line, cat no longer sees b.
    • On a regular file, File.close puts the shared position back at the end of the last line, but a program that exits without closing leaves it past the buffer.
    • A handle dropped without File.close leaks its 16 KiB buffer along with its fd.
  • If another handle writes to the same file, this handle's buffer can hold stale bytes, as in any buffered reader.
  • A read_line loop answers buffered lines without parking. As with any effect that answers at once, the loop polls its timers every 64 answers, and a buffer holds at most 16 KiB of lines.
  • Terminals are untested: the session that wrote this had no tty. The JS lane still reads one byte at a time.

🤖 Generated with Claude Code

…ered line inline

On the C lane, File.read_line made one read(2) per byte. A handle that
read_line reads now keeps a 64 KiB buffer of its own, filled by one
read(2) with what is there, so a pipe or a terminal still answers each
line as it comes. A line already whole in the buffer answers without the
helper thread's round trip.

The buffered bytes belong to the handle: File.read and File.read_bytes
answer them first, a write and File.close seek a regular file back to the
end of the last line, and File.open drops a stale buffer for a reused fd.

Supersedes #2157; tests/io/file_read_line_rest.bend comes from it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nicolas-abril and others added 3 commits November 16, 2026 11:06
…elds every 64

The buffer change rewrote file_read.c from a copy that predated #2155, so
file_read_start lost its io_sync(): a read that blocks while another fiber
keeps the loop busy no longer flushed the text IO.print held.
file_read_line_stdout_order checks it with an interposed read on a handle
opened before the print.

A line whole in the buffer still answers on the loop, but only 64 in a
row: the next goes through the helper, so the loop polls its timers and
parked work (guide/EFFECTS.md). It allocates the line, not max + 1 bytes,
and scans for '\n' once.

Before a write or close, the seek back runs on any fd that seeks (a block
device too, not only a regular file) and drops the buffer only when the
seek lands where it was asked; a terminal keeps it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Short lines run the same at 4, 8, 16 and 64 KiB, since the helper round
trip every 64 lines costs more than a refill. Lines of about 4 KB run
about 20% slower than at 64 KiB on macOS and 9% slower on Linux, and
each handle that reads lines holds a quarter of the memory until it
closes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ne drops the 64-line cap

file_read_line_stdout_order imported a .c with no .js twin, so the
interpreted lane, which the gate always runs, stopped at "a foreign def
without a .js import". The C side now interposes fflush as well as read,
and the armed read fails the program if stdout was not flushed, so every
lane prints the same lines; the JS twin passes the file through.

io_step already requeues a fiber after 64 answers and io_loop polls its
timers and helpers every 64 turns, so FILE_LINE_RUNS only added a stdout
flush and a helper round trip every 65th line. A buffered line now builds
its String straight from the buffer, with no copy, and the line cutting
lives in one place (file_line_cut).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant