Repository navigation
perf(io): File.read_line reads a regular file ahead and seeks back past its line - #2157
sebastianbreguel wants to merge 1 commit into
Conversation
…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
left a comment
There was a problem hiding this comment.
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.
|
Thanks for the careful review, good catch on the lseek path. On it! |
|
@nicolas-abril Could you look at this PR? |
|
Closing in favour of #2178. It keeps a buffer per handle instead of seeking back after every line, so a failed or no-op |
…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]>
What
On the C lane,
File.read_linemade oneread(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 perread(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.File.read,read_bytesandread_atpass step 0 and keep their old path. If the seek-back fails, the call answersFailwith 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 withbend rl.bend -o rlon an Apple M4 Pro (macOS, 12 threads). Each row is 3 interleaved base/branch pairs, warm. The output digest matches in every pair../rl /dev/stdin < 200k linescat 200k lines | ./rl /dev/stdinThe pipe row gets slower: every line now pays one
fstatto 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 atmax, then callsFile.readfor 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_peerandfile_binarypass on the interpreted, C and bun lanes.file_read_line_interruptedstill interposesread()and sees the retry.bend rl.bend -o rl && ./rl big.txtbefore and after, on any file with many lines.Risk
readand thelseek. The byte-at-a-time path never had that window.fstat,S_ISREG,lseek), so I expect the same shape there, but I did not run it.fshas nolseek, andbun:ffiis missing on Node (fix(io): File.read and its twins read through fs on Node, where bun:ffi is missing #1881).bend2/effs/file_read.cand the new test.