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
From 028235037b51a58a5d576e82bc97dbda42859fda Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email protected]>
Date: Tue, 17 Nov 2026 15:13:53 +0000
Subject: [PATCH] cat: reduce potential overhead with small I/O

/proc/sys allocates kvzalloc(count+1, GFP_KERNEL),
which results in an expensive 512KiB zeroed allocation:

  $ cd /proc/sys/kernel

  $ time cat $(yes version | head -c 256K) >/dev/null
  real	0m1.786s
  user	0m0.132s
  sys	0m1.650s

Using a small initial buffer drops this significantly:

  $ time src/cat $(yes version | head -c 256K) >/dev/null
  real	0m0.210s
  user	0m0.091s
  sys	0m0.113s

* src/cat.c (simple_cat): Adjust to support processing a single block.
(main): Process a 4096 byte block initially before proceeding
to further bulk transfer methods.
* NEWS: Mention the improvement.
Reported by Domas Mituzas
---
 NEWS      |  4 ++++
 src/cat.c | 34 ++++++++++++++++++++++++++--------
 2 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/NEWS b/NEWS
index 3441f1bf2..9828d6e8c 100644
--- a/NEWS
+++ b/NEWS
@@ -393,6 +393,10 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 ** Improvements
 
+  'cat' initially uses a small aligned buffer for plain copies of regular
+  files reporting a size up to 4 KiB.  This avoids large buffer allocations
+  for small files, including allocation and zeroing in Linux sysctl reads.
+
   'basenc --base58' encoding speed is 20x faster when built without libgmp.
 
   'cksum' when generating checksums to tty, uses shell quoting when required,
diff --git a/src/cat.c b/src/cat.c
index 6203ed064..dfb636cf0 100644
--- a/src/cat.c
+++ b/src/cat.c
@@ -50,6 +50,9 @@
   proper_name_lite ("Torbjorn Granlund", "Torbj\303\266rn Granlund"), \
   proper_name ("Richard M. Stallman")
 
+/* Initial read size for small regular files.  */
+#define INITIAL_READ_SIZE 4096
+
 /* Name of input file.  May be "-".  */
 static char const *infile;
 
@@ -168,10 +171,11 @@ next_line_num (void)
 
 /* Plain cat.  Copy the file behind 'input_desc' to STDOUT_FILENO.
    BUF (of size BUFSIZE) is the I/O buffer, used by reads and writes.
-   Return true if successful.  */
+   If INITIAL, return after a full buffer.
+   Return 1 at EOF, 0 to try bulk copying, -1 on error.  */
 
-static bool
-simple_cat (char *buf, idx_t bufsize)
+static int
+simple_cat (char *buf, idx_t bufsize, bool initial)
 {
   /* Loop until the end of the file.  */
 
@@ -183,18 +187,21 @@ simple_cat (char *buf, idx_t bufsize)
       if (n_read < 0)
         {
           error (0, errno, "%s", quotef (infile));
-          return false;
+          return -1;
         }
 
       /* End of this file?  */
 
       if (n_read == 0)
-        return true;
+        return 1;
 
       /* Write this block out.  */
 
       if (full_write (STDOUT_FILENO, buf, n_read) != n_read)
         write_error ();
+
+      if (initial && n_read == bufsize)
+        return 0;
     }
 }
 
@@ -905,8 +912,19 @@ main (int argc, char **argv)
       if (! (number || show_ends || show_nonprinting
              || show_tabs || squeeze_blank))
         {
-          int copy_cat_status =
-            out_isreg && S_ISREG (istat_buf.st_mode) ? copy_cat () : 0;
+          int copy_cat_status = 0;
+          if (S_ISREG (istat_buf.st_mode)
+              && istat_buf.st_size <= INITIAL_READ_SIZE)
+            {
+              /* /proc/sys files files as of Linux 7.2 at least, have
+                 less kernel memory allocation overhead with smaller reads().
+                 The buffer and read size are aligned in case of direct I/O.  */
+              inbuf = ensure_buf_size (inbuf, &inbuf_alloc,
+                                       page_size, INITIAL_READ_SIZE);
+              copy_cat_status = simple_cat (inbuf, INITIAL_READ_SIZE, true);
+            }
+          if (copy_cat_status == 0 && out_isreg && S_ISREG (istat_buf.st_mode))
+            copy_cat_status = copy_cat ();
           if (copy_cat_status != 0)
             ok &= 0 < copy_cat_status;
           else
@@ -925,7 +943,7 @@ main (int argc, char **argv)
                   insize = MAX (insize, outsize);
                   inbuf = ensure_buf_size (inbuf, &inbuf_alloc,
                                            page_size, insize);
-                  ok &= simple_cat (inbuf, insize);
+                  ok &= 0 < simple_cat (inbuf, insize, false);
                 }
             }
         }
-- 
2.56.0

Reply via email to