]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
du: avoid locking and flushing standard output
authorCollin Funk <collin.funk1@gmail.com>
Sat, 28 Feb 2026 06:18:48 +0000 (22:18 -0800)
committerCollin Funk <collin.funk1@gmail.com>
Sat, 28 Feb 2026 06:22:20 +0000 (22:22 -0800)
This results in a noticeable increase in performance:

    $ yes /dev/null | head -n 10000000 | tr '\n' '\0' \
        | time --format=%E ./src/du-prev -l --files0-from=- > /dev/null
    0:20.40
    $ yes /dev/null | head -n 10000000 | tr '\n' '\0' \
        | time --format=%E ./src/du -l --files0-from=- > /dev/null
    0:16.57

* src/du.c (print_size): Prefer putchar and fputs which may be unlocked
unlike printf. Prefer ferror to fflush.

src/du.c

index 8243ef1ac520d8876d2c98756ae9d0ca3353c7c5..a41ece904ab2753e73bb809719b8dad533cdbead 100644 (file)
--- a/src/du.c
+++ b/src/du.c
@@ -464,10 +464,11 @@ print_size (const struct duinfo *pdui, char const *string)
           fputs (timetostr (pdui->tmax.tv_sec, buf), stdout);
         }
     }
-  printf ("\t%s%c", string, opt_nul_terminate_output ? '\0' : '\n');
-  if (fflush (stdout) != 0)
+  putchar ('\t');
+  fputs (string, stdout);
+  putchar (opt_nul_terminate_output ? '\0' : '\n');
+  if (ferror (stdout))
     write_error ();
-
 }
 
 /* Fill the di_mnt set with local mount point dev/ino pairs.  */