Quantcast

print stdout and flush

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

print stdout and flush

Cuero Bugot-2
I may be re-spawning an ancient topic but I was not able to retrieve clear explanation/reasons on the history.

I am wondering why static int luaB_print (lua_State *L) function does not have a fflush(stdout) after fputs("\n", stdout);
1) For the specific use case of print (mainly debug) I would not consider armful in term of performances.
2) It would also be more user friendly (who did not try to redirect stdout and miss a couple of lines because of the buffering)
3) Finally it would also 'comply' or at least mimic the C ISO/Posix printf API that recommends to flush the buffer when '\n' is printed.

Is there a rationale that I am missing here ?

-Cuero


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: print stdout and flush

steve donovan
On Thu, May 26, 2011 at 3:28 PM, Cuero Bugot <[hidden email]> wrote:
> 1) For the specific use case of print (mainly debug) I would not consider armful in term of performances.

Well, it is easy enough to change the buffering (see
http://www.lua.org/manual/5.1/manual.html#pdf-file:setvbuf)

io.stdout:setvbuf("no")

It's indeed an issue that people need to know about - e.g. in SciTE we
set the run Lua command to be

lua -e "io.stdout:setvbuf 'no'" $(FileName)

precisely because of this issue - at least on Windows.

steve d.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: print stdout and flush

Cuero Bugot-2

> Well, it is easy enough to change the buffering (see
> http://www.lua.org/manual/5.1/manual.html#pdf-file:setvbuf)
> io.stdout:setvbuf("no")

I missed this one thanks. Well io.stdout:setvbuf("line") would be even more appropriate in that case.

Thx

 -C


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: print stdout and flush

Cuero Bugot-2
In reply to this post by Cuero Bugot-2

> 3) Finally it would also 'comply' or at least mimic the C ISO/Posix printf API that recommends to flush the buffer when '\n' is printed.

This seems actually not true (at least on my Ubuntu Linux)



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: print stdout and flush

Luiz Henrique de Figueiredo
In reply to this post by Cuero Bugot-2
> I am wondering why static int luaB_print (lua_State *L) function does not have a fflush(stdout) after fputs("\n", stdout);

Because ANSI C says that stdout is line buffered? At least I thought so but
some people disagree: http://stackoverflow.com/questions/3723795/is-stdout-line-buffered-unbuffered-or-indeterminate-by-default

So, yes, perhaps it's best to fflush(stdout) after all, just in case.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: print stdout and flush

Cuero Bugot-2
> Because ANSI C says that stdout is line buffered? At least I thought so but
> some people disagree: http://stackoverflow.com/questions/3723795/is-stdout-line-buffered-unbuffered-or-indeterminate-by-default
> So, yes, perhaps it's best to fflush(stdout) after all, just in case.

Well I was also miss leaded by some posts, but I found another one that have actual references on the C99 specification:
http://stackoverflow.com/questions/5229096/does-printf-always-flush-the-buffer-on-encountering-a-newline
C99, 7.19.3 Files, paragraph 7 and 5.1.2.3/6
Basically it says that at program startup stdout is opened as a full buffered file unless it is connected to an interactive device...

I actually verified this behavior doing a simple&stupid C program:

#include <stdio.h>
int main()
{
    //setvbuf(stdout, (char *) NULL, _IOLBF, 0);
    while (1)
    {
        printf("toto\n");
        sleep(1);
    }
    return 0;
}


When running 'test' in a shell shows that the new line appears every seconds

However running 'test > somefile' will not feed the file, unless I uncomment the setvbuff line :)

 -C

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: print stdout and flush

Roberto Ierusalimschy
> C99, 7.19.3 Files, paragraph 7 and 5.1.2.3/6
> Basically it says that at program startup stdout is opened as a full
> buffered file unless it is connected to an interactive device...

  7.19.3/7:
  As initially opened, the standard error stream is not fully buffered;
  the standard input and standard output streams are fully buffered if
  and only if the stream can be determined not to refer to an interactive
  device.

  5.1.2.3/6:
  What constitutes an interactive device is implementation-defined.

:)

-- Roberto

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: print stdout and flush

Axel Kittenberger
The C++ approach to this issue might be worth a read:

http://stackoverflow.com/questions/213907/c-stdendl-vs-n

On Thu, May 26, 2011 at 6:00 PM, Roberto Ierusalimschy
<[hidden email]> wrote:

>> C99, 7.19.3 Files, paragraph 7 and 5.1.2.3/6
>> Basically it says that at program startup stdout is opened as a full
>> buffered file unless it is connected to an interactive device...
>
>  7.19.3/7:
>  As initially opened, the standard error stream is not fully buffered;
>  the standard input and standard output streams are fully buffered if
>  and only if the stream can be determined not to refer to an interactive
>  device.
>
>  5.1.2.3/6:
>  What constitutes an interactive device is implementation-defined.
>
> :)
>
> -- Roberto
>
>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: print stdout and flush

Jay Carlson
In reply to this post by Cuero Bugot-2
On Thu, May 26, 2011 at 9:28 AM, Cuero Bugot <[hidden email]> wrote:
> I may be re-spawning an ancient topic but I was not able to retrieve clear explanation/reasons on the history.
>
> I am wondering why static int luaB_print (lua_State *L) function does not have a fflush(stdout) after fputs("\n", stdout);
> 1) For the specific use case of print (mainly debug) I would not consider armful in term of performances.

I would bet a lot of people use print() in ways that are not about
debug; print() shows up a lot in my ad-hoc Unix-y pipes/scripts. It
would be a significant performance hit.

If there is a desire for a real debug function, it would be more
natural for output to go to io.stderr. Might not be a bad idea anyway;
log()?

> 2) It would also be more user friendly (who did not try to redirect stdout and miss a couple of lines because of the buffering)

Me? I guess I don't understand the situation in which this would happen.

Performance difference between buffered and unbuffered access for file
redirection is significant. Calling print() on a 40 character string a
million times going to a ramdisk currently takes:

ubuntu@ubuntu:~$ /usr/bin/time lua foo.lua >/tmp/aa
0.73user 0.15system 0:01.10elapsed

Turning off buffering with setvbuf takes

ubuntu@ubuntu:~$ /usr/bin/time lua foo.lua >/tmp/aa
1.80user 2.32system 0:05.43elapsed

> 3) Finally it would also 'comply' or at least mimic the C ISO/Posix printf API that recommends to flush the buffer when '\n' is printed.

Cite?

> Is there a rationale that I am missing here ?

print() currently acts like C's printf(). This seems natural for a
language that generally takes C's position on IO (much to the dismay
of the i18n crowd, for instance.)

Jay

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: print stdout and flush

Axel Kittenberger
> print() currently acts like C's printf(). This seems natural for a
> language that generally takes C's position on IO (much to the dismay
> of the i18n crowd, for instance.)

print() is currently an adhoc function to make something look remotely
useful without worrying much (autospacing at the such).

For those who need more control use io.stdout.write() anyway.
So there is a good point for a flush after every print(). For those
for which this is too much performance hit, should use write()
instead.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: print stdout and flush

Roberto Ierusalimschy
> For those who need more control use io.stdout.write() anyway.
> So there is a good point for a flush after every print(). For those
> for which this is too much performance hit, should use write()
> instead.

That is true. BTW, io.write is also more efficient when printing numbers.

-- Roberto

Loading...