Pádraig Brady <P@draigBrady.com> writes:
Domas Mituzas pointed out that
cat(1) induces a 512KiB zeroed mem allocation in the kernel
when doing a 256KiB read of /proc/sys files.
The attached therefore tries an initial 4KiB read
resulting in a significant perf difference:
$ cd /proc/sys/kernel
$ time cat $(yes version | head -c 256K) >/dev/null
real 0m1.786s
user 0m0.132s
sys 0m1.650s
$ time src/cat $(yes version | head -c 256K) >/dev/null
real 0m0.210s
user 0m0.091s
sys 0m0.113s
# Domas' hack for single line files is good too :)
$ time tac $(yes version | head -c 256K) >/dev/null
real 0m0.237s
user 0m0.083s
sys 0m0.134s
Note doing a 4KiB-1 read() would be slightly more efficient,
but it would complicate the code as you'd have to have
fallbacks for potential EINVALs induced with odd sizes and direct IO.
I guess 'yes' kind-of does this by writing the buffer with write before
increasing the pipe size and splicing.
Would it be better to place the small buffer on the stack and over
allocate slightly so it could be aligned? Or perhaps that is overkill...