coreutils
[Top][All Lists]
Advanced

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

RFC: using a small initial buffer with cat


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

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.

cheers,
Padraig

Attachment: cat-small.patch
Description: Text Data


reply via email to

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