[plt-scheme] interleaving the I and O in I/O

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Feb 1 12:23:37 EST 2008

At Fri, 01 Feb 2008 10:51:32 -0500, Prabhakar Ragde wrote:
> In response to my question on why alternating reads and printfs (instead 
> of doing all the printfs later) slowed down a program, Matthew Flatt 
> wrote on January 27:
> > When you read from the original input port, the original output port is
> > automatically flushed. So, the first version writes line-by-line, while
> > the second writes the list in blocks. I think that accounts for the
> > time difference.
> > 
> > I note that there's another layer of automatic behavior here, which is
> > that block buffering for output is enabled because output is directed
> > to a file, as opposed to a tty.
> > 
> > For 4.0, we should reconsider auto-flush of the current output port
> > when reading from the current input port. Maybe it should only happen
> > when the output is a tty. It's also awkward that the behavior is tied
> > to the original ports, but that's another part of the trade-off in
> > performance and expected behavior, and it will probably stay.
> So, a followup I should have asked at the time: is there any way for the 
> user to modify this behaviour? Specifically, I want to avoid having the 
> current output port flushed every time there is a read.

There's currently no way to control that.

To avoid the flush, you could write a different port that has its own
buffer and that eventually flushes to the original output port --- but
the wrapper creates enough new work that it might defeat the purpose.

FWIW, I did change v4 so that the port has to be a tty to trigger


Posted on the users mailing list.