Buffered output typically comes in two flavors: fully buffered and line buffered. If output goes to a terminal, the implementation automatically selects line buffering, flushing when a line completes so it reaches the user immediately. Otherwise it fully buffers, holding output until the buffer is full or an explicit flush occurs. Line buffering is a common bottleneck in non-interactive terminal applications, often worth avoiding by redirecting output to a file or pipe.
It doesn’t have to be this way. Info-ZIP unzip prints file names as it extracts them, and that printout dominates extraction time for archives with many small files. The author avoided this problem in tugz unzip without changing the user interface: you get a listing but don’t pay for it. glibc takes line buffering literally, even splitting writes on a string, while CPython flushes if its buffer contains a newline after appending new output.
A better approach applies Nagle’s algorithm: flush the buffer if it’s been idle a short time, rather than writing on every newline. Adding a deadline to the buffer struct enables prompt terminal delivery at fully buffered speeds. The goal is not a write on every newline, but avoiding holding lines indefinitely when output goes to a terminal.
Source: Hacker News · Summarized by HeadlinesBriefing