← Thu, Nov 5 digest

We can do better than line-buffered output

read article ↗ hn thread ↗

Chris Wellons proposes a time-deadline flushing mechanism to batch terminal output, replacing inefficient line buffering with fully-buffered speeds.

read it A clever solution to a ubiquitous I/O bottleneck that rewards systems programmers with a cleaner mental model.

summary

Traditional line buffering flushes standard output on every newline, introducing severe bottlenecks for command-line tools that print numerous small lines. Standard implementations split writes unnecessarily, degrading throughput. A hybrid work-or-time buffering strategy resolves this by flushing buffers when full or after a brief inactivity window. This delivers fully-buffered performance while maintaining immediate responsiveness for human users.

key points

  1. Line buffering forces frequent small writes on every newline, creating severe bottlenecks in high-frequency CLI tools like unzip.
  2. A time-deadline approach batches lines efficiently while keeping terminal response latency imperceptible to human users.
  3. Work-or-time flushing balances overhead reduction against worst-case latency in low-throughput environments.

who should read it

Systems programmers and CLI tool developers looking to optimize I/O performance.

discussion warm

consensus

Engineers widely endorse the proposal, noting that work-or-time flushing is a reliable pattern for balancing throughput and latency.

pushback

One critique notes that comparing this to Nagle's algorithm is inaccurate because Nagle lacks time-based flushing and causes TCP delayed-acknowledgment issues.

best comment by 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 …

It clarifies the theoretical basis by mapping the concept to work-or-time flushing and contrasting it with Nagle's algorithm.

Generated by an LLM from the article and thread. Check the sources before relying on it.