On 5 July 2015 at 12:50, Pádraig Brady <[email protected]> wrote:
> On 05/07/15 19:54, Henner Zeller wrote:
>> The 'fold' utility was not very helpful in streaming contexts as
>> outgoing lines where not sent immediately but whenever the buffer was
>> full.
>>
>> Patch attached.
>
> That would have significant performance impact on the normal case.
> We have support for configuring this at runtime though using stdbuf.
> The following shows that and the performance difference:
>
>   $ time yes 12345 | fold -w5 | head -n1M > /dev/null
>   real  0m0.393s
>   user  0m0.458s
>   sys   0m0.017s
>
>   $ time yes 12345 | stdbuf -oL fold -w5 | head -n1M > /dev/null
>   real  0m1.695s
>   user  0m0.797s
>   sys   0m2.280s
>

I could hide it behind a flag (which I wanted to avoid initially for
less user confusion).
Updated patch attached.

> cheers,
> Pádraig.
From 94fa4822a9be1e159b19aab8e862ea3a140e4633 Mon Sep 17 00:00:00 2001
From: Henner Zeller <[email protected]>
Date: Sun, 5 Jul 2015 11:20:33 -0700
Subject: [PATCH] fold: flush after each line switched via a command line
 option

* src/fold.c: add new option -l/--linebuffer that makes fold flush
after each line.
---
 src/fold.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/fold.c b/src/fold.c
index 0955c09..487900d 100644
--- a/src/fold.c
+++ b/src/fold.c
@@ -43,13 +43,17 @@ static bool count_bytes;
 /* If nonzero, at least one of the files we read was standard input. */
 static bool have_read_stdin;
 
-static char const shortopts[] = "bsw:0::1::2::3::4::5::6::7::8::9::";
+/* If nonzero, flush after each line */
+static bool line_buffer;
+
+static char const shortopts[] = "bsw:l0::1::2::3::4::5::6::7::8::9::";
 
 static struct option const longopts[] =
 {
   {"bytes", no_argument, NULL, 'b'},
   {"spaces", no_argument, NULL, 's'},
   {"width", required_argument, NULL, 'w'},
+  {"linebuffer", no_argument, NULL, 'l'},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
@@ -77,6 +81,7 @@ Wrap input lines in each FILE, writing to standard output.\n\
   -b, --bytes         count bytes rather than columns\n\
   -s, --spaces        break at spaces\n\
   -w, --width=WIDTH   use WIDTH columns instead of 80\n\
+  -l, --linebuffer    flush after each line\n\
 "), stdout);
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
@@ -142,6 +147,9 @@ fold_file (char const *filename, size_t width)
 
   fadvise (istream, FADVISE_SEQUENTIAL);
 
+  if (line_buffer)
+    setlinebuf (stdout);
+
   while ((c = getc (istream)) != EOF)
     {
       if (offset_out + 1 >= allocated_out)
@@ -251,7 +259,7 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  break_spaces = count_bytes = have_read_stdin = false;
+  break_spaces = count_bytes = have_read_stdin = line_buffer = false;
 
   while ((optc = getopt_long (argc, argv, shortopts, longopts, NULL)) != -1)
     {
@@ -267,6 +275,10 @@ main (int argc, char **argv)
           break_spaces = true;
           break;
 
+	case 'l':
+	  line_buffer = true;
+	  break;
+
         case '0': case '1': case '2': case '3': case '4':
         case '5': case '6': case '7': case '8': case '9':
           if (optarg)
-- 
2.4.0

Reply via email to