# We can do better than line-buffered output

DevFeed: [We can do better than line-buffered output](<https://devfeed.tech/articles/we-can-do-better-than-line-buffered-output-119307.md>)

Original publisher: [Read original article](<https://nullprogram.com/blog/2026/11/05/>)

Published: 2026-11-05T00:25:25Z

Content type: article

Language: en

Sources: [Chris Wellons](<https://devfeed.tech/sources/chris-wellons.md>)

Topics: [IO](<https://devfeed.tech/topics/io.md>), [Command-line interface](<https://devfeed.tech/topics/cli.md>), [Terminal](<https://devfeed.tech/topics/terminal.md>)

Tags: [2](<https://devfeed.tech/tags/2.md>), [applications](<https://devfeed.tech/tags/applications.md>), [archive](<https://devfeed.tech/tags/archive.md>), [automatically](<https://devfeed.tech/tags/automatically.md>), [avoid](<https://devfeed.tech/tags/avoid.md>), [better](<https://devfeed.tech/tags/better.md>), [buffer](<https://devfeed.tech/tags/buffer.md>), [but](<https://devfeed.tech/tags/but.md>), [c](<https://devfeed.tech/tags/c.md>), [can](<https://devfeed.tech/tags/can.md>), [comes](<https://devfeed.tech/tags/comes.md>), [consider](<https://devfeed.tech/tags/consider.md>), [cpython](<https://devfeed.tech/tags/cpython.md>), [doesn-t](<https://devfeed.tech/tags/doesn-t.md>), [don-t](<https://devfeed.tech/tags/don-t.md>), [event](<https://devfeed.tech/tags/event.md>), [example](<https://devfeed.tech/tags/example.md>), [experience](<https://devfeed.tech/tags/experience.md>), [faster](<https://devfeed.tech/tags/faster.md>), [file](<https://devfeed.tech/tags/file.md>)

## AI overview

Terminal line buffering can slow noninteractive programs by flushing on each newline. In the author's `tugz` unzip, output stays visible without that per-line cost: the buffer is checked during short stints of work and flushed only after it has been idle for a brief deadline (suggested around 100 ms), allowing several filenames to be written together. This avoids extra writes without a thread and leaves the user interface unchanged. The excerpt contrasts this with glibc's newline-by-newline writes and CPython's flush when buffered output contains a newline, which can emit partial lines.

## Source excerpt

Buffered output typically comes in two flavors: fully buffered and line buffered. If output goes to a terminal then the implementation automatically selects line buffering, flushing its buffer when a line is completed so that it reaches the user immediately. Otherwise it fully buffers, holding output until the buffer is full or an explicit flush event at some arbitrary point in the future. Line buffering is a common bottleneck in non-interactive terminal applications such that it's worth redirecting output to a file or pipe in order to avoid it. It doesn't have to be this way. We can have our cake and eat it, too: promptly delivering lines to a terminal at fully-buffered speeds. For example, Info-ZIP unzip prints file names as it extract them from an archive. This printout dominates extraction time of archives with many small files. Sometimes I run unzip without options, then my terminal floods with file names and I know that my extraction is bottlenecked. So I cancel and restart with -q (quiet) to avoid the bottleneck, because it's faster to start over than wait. Conscious of this experience, I avoided the problem in tugz unzip without changing the user interface. You get a listing but you don't have to pay for it. $ unzip example.zip Archive: example.zip inflating: example.bmp inflating: example.svg inflating: example.txt Consider this C program that prints three lines: #include <stdio.h> int main() { puts("a\nb"); puts("c"); } If we examine the writes with, say, strace: $ strace -o log -e write ./a.out a b c We might see (here glibc): write(1, "a\n", 2) = 2 write(1, "b\n", 2) = 2 write(1, "c\n", 2) = 2 Yes, glibc takes line buffering quite literally, even splitting writes on a string. Some implementations are a little more sophisticated: write(1, "a\nb\n", 2) = 4 write(1, "c\n", 2) = 2 CPython makes exactly those writes for this program: import sys sys.stdout.write("a\nb\n") sys.stdout.write("c\n") Its rule is to flush if its buffer contains a newline after appen