Line buffering is a bottleneck — and we can do better
We can do better than line-buffered output
Line buffering flushes on every newline, which makes programs like unzip crawl when printing many files to a terminal. The author proposes a deadline-based flush: hold output briefly (around 100ms) and send it in batches, poked between short stints of work rather than by a thread. The result looks instant to humans while running at fully buffered speed, as implemented in his tugz unzip.
We can have our cake and eat it, too: promptly delivering lines to a terminal at fully-buffered speeds.
- Veserv
Yes, basically all buffering should use work-or-time based flushing.
Work-based flushing exists to batch work to amortize overhead. It allows you to tune how much time you spend in overhead in a work period. However, this can result in unbounded delay.
Time-based flushing targets a worst-case latency in low throughput environments. This ensures you do not get unbounded delays. Interestingly, this also allows you to tune how much time you spend in overhead in a work period.
So, with work-or-time based flushing what you are really tuning is how much time you are willing to spend in overhead in a work period and balancing that against latency.
As a nit, the article calls this a derivative of Nagle’s algorithm, but it is basically the opposite. Nagle’s algorithm is about batching until acknowledgement with no mention of time. In fact, the lack of time-based flushing in Nagle’s algorithm is why it interacts so poorly with TCP Delayed Ack.
- chuliomartinez
I have seen versions of this many times in my life. Unfortunately, that progress report can hurt a lot, is something we devs often miss.
From my horror cabinet:
1. Running count * on all sqlite tables 4x per second to show sync progress.
2. Progress bounce animation eating 80% of cpu (windows phone bizarro silverlight)
3. Sending myself 800 emails in 10 minutes…
The rule is quite simple, we are just bad at it. Computer to human communication should run at human speed.
- abecedarius
> flush the buffer if it’s been idle a short time
I've thought this was the right default for a good while. 50 years ago when Unix was designed, maybe not.