Repository navigation
perf(io): File.read_line keeps a buffer per handle and answers a buffered line inline - #2178
Merged
Merged
Conversation
…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]>
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
On the C lane,
File.read_linemade oneread(2)per byte. Now each handle thatread_linereads keeps a 16 KiB buffer of its own. Oneread(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_linebuilds 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 callsio_sync()first, as #2155 requires.The buffered bytes belong to the handle, not the fd:
File.readandFile.read_bytesanswer the buffered bytes first.File.read_atandFile.sizeare unchanged.File.writeandFile.write_atfirst seek back to the end of the last line read, so on anr+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.closedoes the same seek-back, then frees the buffer.File.opendrops 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.bendis taken from it.Why
A loop that counts the lines and characters of a file with
File.read_line(f, 65536), built withbend rl.bend -o rl. All builds print the same counts on every run.Linux (Arch, kernel 7.2), AMD Ryzen 7 5800H. The
mainand #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:./rl /dev/stdin < file, 200k linescat file | ./rl /dev/stdin, 200k linescat file | ./rl /dev/stdin, 10k × 4 KBmacOS, Apple M4 Pro. Here
mainand #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 anr+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): aread_lineon a handle opened before anIO.printmust flush the held text, even while another fiber keeps the loop away from its poller. On the C lane it interposesfflushandread(), and the read fails the program if stdout was not flushed: without theio_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_linecut atmax, lines longer than one chunk, thenFile.readfor the rest.tests/io/file_*,read_bytesandstdout_file_ordertests pass on the C lane on macOS, and theread_linetests also pass on the interpreted and JS lanes. On Linux they pass too, exceptstdout_file_order, which fails the same way onmainwhen stdout is redirected to a file: opening/dev/stdoutwith"w"truncates it there.Risk
BufReaderand Python's buffered IO:printf 'a\nb\n' | (./prog /dev/stdin; cat), whereprogreads one line,catno longer seesb.File.closeputs the shared position back at the end of the last line, but a program that exits without closing leaves it past the buffer.File.closeleaks its 16 KiB buffer along with its fd.read_lineloop 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.🤖 Generated with Claude Code