Page MenuHomeFreeBSD

Improve sh(1) built-in read command performance when using a seekable fd
ClosedPublic

Authored by hrs on Feb 18 2020, 8:50 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sep 28 2024, 2:27 AM
Unknown Object (File)
Sep 26 2024, 5:58 AM
Unknown Object (File)
Sep 21 2024, 6:53 AM
Unknown Object (File)
Sep 17 2024, 2:14 PM
Unknown Object (File)
Sep 9 2024, 1:16 AM
Unknown Object (File)
Sep 8 2024, 8:48 AM
Unknown Object (File)
Sep 5 2024, 4:39 AM
Unknown Object (File)
Sep 1 2024, 11:08 AM
Subscribers

Details

Summary

This change adds a small buffer in sh(1) built-in read command where
it currently uses a 1-byte buffer.

sh(1) built-in read command calls read(2) with a 1-byte buffer because
newline characters need to be detected even on byte-stream 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 block
and then reposition the file pointer to where a newline character
found. It reduces the number of read(2) calls.

Theoretically, when multiple built-in commands are reading the same
seekable stream in a single pipe chain it is possible to share the
buffer. However, this change just makes a single built-in read
command allocate a buffer and deallocate it every time when invoked to
keep it simple. Although this causes read(2) to read the same regions
multiple times, impact to the overall performance should be small.

It seems bash and ksh have similar optimization while zsh does not.

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 29476
Build 27349: arc lint + arc unit