Skip to content

perf(io): File.read_line reads a regular file ahead and seeks back past its line - #2157

Closed
sebastianbreguel wants to merge 1 commit into
bendlang:mainfrom
sebastianbreguel:perf/read-line-ahead
Closed

sebastianbreguel wants to merge 1 commit into
bendlang:mainfrom
sebastianbreguel:perf/read-line-ahead

Conversation

@sebastianbreguel

Copy link
Copy Markdown
Contributor

What

On the C lane, File.read_line made one read(2) per byte, so a program that reads a file line by line spent most of its time in syscalls. On a regular file it now reads up to 256 bytes per read(2), cuts at the first '\n', and seeks back over the bytes past it. The file position after each call is the same as before. A pipe, a terminal or a size-0 file such as /proc/... still reads one byte at a time, because only a regular file can seek back.

regular file:  read(256) -> "ab\ncd..."  -> keep "ab\n", lseek(-k, SEEK_CUR)
pipe / tty:    read(1) x n               -> unchanged

File.read, read_bytes and read_at pass step 0 and keep their old path. If the seek-back fails, the call answers Fail with that errno instead of dropping bytes without a trace.

Why

A loop that counts the lines and bytes of a file with File.read_line(f, 65536), built with bend rl.bend -o rl on an Apple M4 Pro (macOS, 12 threads). Each row is 3 interleaved base/branch pairs, warm. The output digest matches in every pair.

input base p50 branch p50 speedup
200k lines, 9.5 MB 4.22 s 1.67 s 2.5x
1M lines, 47.6 MB 21.16 s 7.94 s 2.7x
10k lines of ~4 KB, 42 MB 12.18 s 0.27 s 45x
./rl /dev/stdin < 200k lines 4.28 s 1.65 s 2.6x
cat 200k lines | ./rl /dev/stdin 4.47 s 4.91 s 0.91x

The pipe row gets slower: every line now pays one fstat to learn it is not a regular file. That works out to about 2 µs per line, against roughly 22 µs per line that a pipe already costs. On the regular-file rows, sys time drops from 3.4 s to 1.3 s at 200k lines; I did not profile what remains.

How to test

  • tests/io/file_read_line_rest.bend (new): it reads a short line, a 300-byte line cut at 280 and then finished by the next call, a whole 300-byte line (two 256-byte chunks), and a line cut at max, then calls File.read for the rest of the file. The test passes on base and on the branch. On the C lane it goes red if the seek-back is off by one byte, or if the scan for '\n' starts at the start of the buffer instead of at the new chunk.
  • tests/io/file_read_line*, file_read_short, file_read_after_error, file_read_cap, file_read_stdout_order, read_bytes, file_open_fifo_peer and file_binary pass on the interpreted, C and bun lanes. file_read_line_interrupted still interposes read() and sees the retry.
  • Benchmark: bend rl.bend -o rl && ./rl big.txt before and after, on any file with many lines.

Risk

  • Two readers that share one open file description (a dup'd fd, a forked child) could move the offset between the read and the lseek. The byte-at-a-time path never had that window.
  • Only macOS was measured. On Linux the same calls exist (fstat, S_ISREG, lseek), so I expect the same shape there, but I did not run it.
  • The JS lane still reads one byte at a time: Node's fs has no lseek, and bun:ffi is missing on Node (fix(io): File.read and its twins read through fs on Node, where bun:ffi is missing #1881).
  • To revert, revert the commit. The change is limited to bend2/effs/file_read.c and the new test.

…st its line

On the C lane, File.read_line made one read(2) per byte. On a regular
file with a size, it now reads up to 256 bytes per read(2), cuts at the
first newline and seeks back over the rest, so the file position is the
same as before. Pipes, terminals and size-0 files such as /proc still
read a byte at a time. A failed seek-back answers Fail.

@YidaWeng YidaWeng left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file_read_line_call enables read-ahead when fstat says S_ISREG and st_size > 0. That is a good heuristic, but it does not prove the fd is seekable: a regular-looking pseudo-file can pass it and still fail lseek.

If the backward lseek in file_read_fill fails, w->code is set after the read-ahead bytes have already been consumed. file_read_pack then calls io_res, which returns a Fail, so the line is not returned and the fd is already past the line. A caller that retries loses those bytes.

Would it be worth requiring lseek(fd, 0, SEEK_CUR) >= 0 before choosing the read-ahead path, or using pread and only advancing the fd after the newline is found? The byte-at-a-time path remains correct for anything that fails the check.

I built the PR head and ran tests/io/file_read_line_rest.bend, file_read_line.bend, file_read_line_interrupted.bend, and file_open_fifo_peer.bend on the C lane; all matched their #| lines. bun gates/repo.ts is 56/56.

The new test covers the 300-byte line and the 280-byte cut; a newline at byte 256 and a line exactly 256 bytes would also cover the chunk boundary. Since this is a perf change, a short-line benchmark would be useful too, because fstat runs once per line.

@sebastianbreguel

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review, good catch on the lseek path. On it!

@sebastianbreguel

Copy link
Copy Markdown
Contributor Author

@nicolas-abril Could you look at this PR?

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Closing in favour of #2178. It keeps a buffer per handle instead of seeking back after every line, so a failed or no-op lseek can no longer lose data, and pipes and stdin get faster too (about 46x vs main, where this PR was 0.91x). Your file_read_line_rest.bend test is included there. Thanks for finding this one.

nicolas-abril added a commit that referenced this pull request Nov 16, 2026
…ered line inline (#2178)

On the C lane, File.read_line made one read(2) per byte. A handle that read_line reads now keeps a 16 KiB buffer of its own, filled by one read(2) with what is there, and a line already whole in the buffer answers on the loop straight from it. A read that reaches the fd flushes stdout first (#2155). File.read and File.read_bytes answer buffered bytes first; a write and File.close seek back to the end of the last line on an fd that seeks. Supersedes #2157.

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.

3 participants