coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: RFC: using a small initial buffer with cat


From: Pádraig Brady
Subject: Re: RFC: using a small initial buffer with cat
Date: Tue, 17 Nov 2026 17:09:40 +0000
User-agent: Mozilla Thunderbird Beta

On 17/11/2026 16:55, Collin Funk wrote:
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...
Oh right. I was playing around with retries and fallback,
but without that we could indeed allocate an 8KiB stack buffer
and ptr_align() to a 4KiB aligned region within that.
I'll have a look.

cheers,
Padraig



reply via email to

[Prev in Thread] Current Thread [Next in Thread]