From: Pádraig Brady
Date: Mon, 23 Mar 2026 17:42:27 +0000 (+0000) Subject: cut: avoid fwrite calls for smaller amounts of data X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9817599526684ecf393f8ca5d1ed2b2f2d7e5358;p=thirdparty%2Fcoreutils.git cut: avoid fwrite calls for smaller amounts of data This is quite significant: yes abcdfeg | head -n1MB > big-file $ time src/cut-before -b1,3 big-file >/dev/null real 0m0.050s $ time src/cut-after -b1,3 big-file >/dev/null real 0m0.029s --- diff --git a/src/cut.c b/src/cut.c index 9f73eefd5c..0dc66c4ec1 100644 --- a/src/cut.c +++ b/src/cut.c @@ -405,6 +405,16 @@ skip_whitespace_run (mbbuf_t *mbuf, struct mbfield_parser *parser, static void write_bytes (char const *buf, size_t n_bytes) { + /* Avoid a function call for smaller amounts, + using instead the macro to directly interact with the stdio buffer. */ + if (n_bytes <= 4) + { + for (size_t i = 0; i < n_bytes; i++) + if (putchar (buf[i]) < 0) + write_error (); + return; + } + if (fwrite (buf, sizeof (char), n_bytes, stdout) != n_bytes) write_error (); }