HomeFreeBSD

Improve performance of "read" built-in command when using a seekable

Description

Improve performance of "read" built-in command when using a seekable
fd.

The read built-in command calls read(2) with a 1-byte buffer because
newline characters need to be detected even on a byte stream which
comes from a non-seekable file descriptor. Because of this, the
following script calls >6,000 read(2) to show a 6KiB file:

while read IN; do echo "$IN"; done < /COPYRIGHT

When the input byte stream is seekable, it is possible to read a data
block and then reposition the file pointer to where a newline
character found. This change adds a small buffer to do this and
reduces the number of read(2) calls.

Theoretically, multiple built-in commands reading the same seekable
byte stream in a single pipe chain can share the buffer. However,
this change just makes a single invocation of the read built-in
allocate a buffer and deallocate it every time for simplicity.
Although this causes read(2) to read the same regions multiple times,
the performance penalty should be small compared to the reduction of
read(2) calls.

Reviewed by: jilles
MFC after: 1 week
Differential Revision: https://reviews.freebsd.org/D23747

Details

Provenance
hrsAuthored on
Reviewer
jilles
Differential Revision
D23747: Improve sh(1) built-in read command performance when using a seekable fd
Parents
rS358151: MFC r357871:
Branches
Unknown
Tags
Unknown