[Toybox] Weird stdout buffering effects

Ray Gardner raygard at gmail.com
Sat Oct 21 18:10:52 PDT 2023


I am working on a little something, and I found that output from my toybuf
command redirected to a file is exceedingly slow. But if piped to cat, it
runs about as fast as expected. So I messed around with the
TOYFLAG_LINEBUF and with setvbuf() modes.

I put a line at the top of my program so it does only this:

for (i = 0; i < 1024 * 512; i++) xprintf("%s\n", "asdfasdf"); return;

Then I tried putting just in front of it one of these:

setvbuf(stdout, 0, _IOLBF, 0);
setvbuf(stdout, 0, _IOFBF, 1024);

Can anyone explain these odd timings?

with TOYFLAG_LINEBUF and:
setvbuf(stdout, 0, _IOLBF, 0);
real    0m13.281s
user    0m0.611s
sys     0m2.486s

with TOYFLAG_LINEBUF and:
setvbuf(stdout, 0, _IOFBF, 1024);
real    0m0.087s
user    0m0.045s
sys     0m0.004s

with TOYFLAG_LINEBUF only:
real    0m12.634s
user    0m0.513s
sys     0m2.372s

without TOYFLAG_LINEBUF and with:
setvbuf(stdout, 0, _IOLBF, 0);
real    0m39.077s
user    0m1.410s
sys     0m7.403s

without TOYFLAG_LINEBUF and with:
setvbuf(stdout, 0, _IOFBF, 1024);
real    0m25.509s
user    0m0.889s
sys     0m4.835s

without TOYFLAG_LINEBUF and no setvbuf() call:
real    0m12.910s
user    0m0.614s
sys     0m2.402s

Seems setvbuf(..., _IOLBF,...) alone is horribly slow, but with
TOYFLAG_LINEBUF it runs about the same as no setvbuf() runs with or
without TOYFLAG_LINEBUF. Using setvbuf(..., _IOFBF,...) without
TOYFLAG_LINEBUF takes twice as long, but with TOYFLAG_LINEBUF it runs way
faster (< 1 sec).

These results were in Linux Mint. I got similar results with WSL Ubuntu,
but more exaggerated (bad times were worse).

Again, this is with output redirected to a file. Redirecting to /dev/null,
or piping through cat or to a crc program that reads from stdin, they all
run quite fast.

What gives?


More information about the Toybox mailing list