original-mawk icon indicating copy to clipboard operation
original-mawk copied to clipboard

improve flushing of output

Open GoogleCodeExporter opened this issue 10 years ago • 8 comments

http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=593504


Original issue reported on code.google.com by [email protected] on 28 Jun 2012 at 10:44

GoogleCodeExporter avatar Aug 04 '15 08:08 GoogleCodeExporter

This can probably be fixed by using setlinebuf() when the output is
either a tty or a pipe.

Original comment by [email protected] on 1 Jul 2012 at 6:00

GoogleCodeExporter avatar Aug 04 '15 08:08 GoogleCodeExporter

However, see my comment in
https://bugs.launchpad.net/ubuntu/+source/mawk/+bug/382602

Original comment by [email protected] on 29 Nov 2012 at 1:36

GoogleCodeExporter avatar Aug 04 '15 08:08 GoogleCodeExporter

Original comment by [email protected] on 29 Nov 2012 at 1:45

  • Added labels: Priority-Low
  • Removed labels: Priority-Medium

GoogleCodeExporter avatar Aug 04 '15 08:08 GoogleCodeExporter

Since the (long-existing and documented) -W interactive option does
what is needed here, the functionality is already supplied.  Making
mawk use unbuffered I/O for stdout when it is a tty would detract from
its performance.  One could make this more/less transparent by reading
options from an environment variable (and I'll leave this issue open
to consider that approach).

Original comment by [email protected] on 1 Dec 2012 at 12:16

GoogleCodeExporter avatar Aug 04 '15 08:08 GoogleCodeExporter

Hello,

This is a buffered input problem and not an output buffering problem.  The "-W 
interactive" option tells mawk to return from each read rather than trying to 
fill up its input buffer and solves the problem.  One could argue that mawk 
should use unbuffered input when reading from standard in.

Also note that the call to "fflush" has no effect since standard out to a 
console is always flushed automatically.

Thanks.  Daniel Pettet

Original comment by [email protected] on 12 Feb 2014 at 1:10

GoogleCodeExporter avatar Aug 04 '15 08:08 GoogleCodeExporter

That last part of the comment was inaccurate (stdout by default is 
line-buffered).

Original comment by [email protected] on 1 Aug 2014 at 8:13

GoogleCodeExporter avatar Aug 04 '15 08:08 GoogleCodeExporter

I'm not inclined to change the default (by turning on -Wi all the time),
because it would make mawk slower.  For instance, running the example
script with/without fflush with/without -Wi, and just looking at mawk's
timing (ignoring the sleeps), I see a 15% slowdown.

The fflush adds a little (oddly, fflush in mawk adds less than in gawk
or original-awk, according to my testing).

Turning on -Wi if the output is a tty might make sense.  However, the
example given does not output to a tty - it is to a pipe.

Original comment by [email protected] on 5 Aug 2014 at 11:24

GoogleCodeExporter avatar Aug 04 '15 08:08 GoogleCodeExporter

Original comment by [email protected] on 5 Aug 2014 at 11:24
I'm not inclined to change the default (by turning on -Wi all the time), because it would make mawk slower. For instance, running the example script with/without fflush with/without -Wi, and just looking at mawk's timing (ignoring the sleeps), I see a 15% slowdown.

You could however stop that input buffering (while leaving output buffering as is or line based if tty like most other utilities). mawk is the only utility that I know that does that. I suspect it's even counter productive. It's also very confusing.

See another case at https://unix.stackexchange.com/q/655796 today of yet another person scratching their head for hours trying to understand what's going on.

stephane-chazelas avatar Jun 25 '21 13:06 stephane-chazelas