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
This commit is contained in:
Pádraig Brady committed 2026-04-05 13:15:56 +01:00
1 parent a16d56d60c
commit 9817599526
1 file changed
+10
+10
View File
@@ -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 ();
}