AutoHotkey Community

It is currently May 24th, 2012, 2:07 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Disable stdout buffering
PostPosted: January 11th, 2009, 3:05 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7501
Location: Australia
FileAppend,Text,* outputs text to standard output (stdout). However, since C++ buffers stdout like any other FILE* by default, it isn't practical for real-time uses such as debugging or inter-process communication. In v1.0.47.06, the following example outputs to stdout only when the script is terminated (or at least very infrequently) rather than the expected once per second:
Code:
Loop
{
    Sleep, 1000
    FileAppend, %A_Index%`n, *
}
Excluding user-defined functions, I can see two solutions:
  1. Introduce another option to FileAppend, such as ** instead of *, to mean flush stdout after writing.
  2. Disable buffering for stdout.
I'd opt for the second option, since the following C++ will work for the lifetime of the application:
Code:
setvbuf(stdout, NULL, _IONBF, 0);
However, this may impact performance of scripts that write a lot to stdout (and don't need it in real-time.)


Report this post
Top
 Profile  
Reply with quote  
 Post subject: extending fileappend
PostPosted: January 16th, 2009, 5:05 pm 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
I have missed this functionality also.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 4th, 2009, 12:27 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Lexikos,

So you think that executing setvbuf(stdout, NULL, _IONBF, 0) unconditionally sometime during program startup is best? I wish it could be known how much it decreases performance; if it's merely due to the higher write frequency (e.g. drive seek times), that wouldn't seem too bad.

This change might also raise the question of whether some option should be provided to do the same for normal files written by FileAppend; otherwise the behavior might seem inconsistent (though it could be documented).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 4th, 2009, 1:26 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7501
Location: Australia
Quote:
So you think that executing setvbuf(stdout, NULL, _IONBF, 0) unconditionally sometime during program startup is best?
It is the easiest option if buffering is typically not desired for stdout. I mostly used it as a "quick fix" while debugging; any concern of performance didn't seem worth the trouble of implementing another option for FileAppend.

I think it may be difficult to measure the performance of stdout, since it may depend on the application (how stdout is implemented and what happens when the data is read from the other end...)
Quote:
This change might also raise the question of whether some option should be provided to do the same for normal files written by FileAppend;
Since closing the file pointer also flushes its buffers, I think buffering could only be an issue for FileAppend in its one-parameter mode within a file-reading loop. The need for an external application to read from the file while the loop is running would probably be very rare.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 4th, 2009, 4:56 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
That seems reasonable. I've added it to WinMain():
setvbuf(stdout, NULL, _IONBF, 0);

The source code for setvbuf() looks small and fast, so it shouldn't affect size or performance too much.

Thanks.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 36 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group