{"article":{"slug":"we-can-do-better-than-line-buffered-output","title":"We can do better than line-buffered output","subtitle":null,"summary":"Chris Wellons argues line buffering is a common bottleneck in terminal programs and proposes deadline-based flushing, where output is flushed after a short time limit instead of on every newline, with C examples.","content_type":"blog_post","language":"en","canonical_url":"https://nullprogram.com/blog/2026/11/05/","author":{"name":"Chris Wellons","url":"https://nullprogram.com/","person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"null program","url":"https://nullprogram.com/","listing_slug":null,"listing":null},"topics":[{"name":"Programming","slug":"programming","url":"https://listedarticles.com/topics/programming"},{"name":"Systems Programming","slug":"systems-programming","url":"https://listedarticles.com/topics/systems-programming"},{"name":"C","slug":"c","url":"https://listedarticles.com/topics/c"}],"about_listings":[],"cover_image_url":null,"license":"public-domain","word_count":823,"reading_minutes":4,"published_at":"2026-11-05T00:00:00.000Z","added_at":"2026-11-05T17:12:42.142Z","updated_at":"2026-11-05T17:12:42.142Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/we-can-do-better-than-line-buffered-output","markdown_url":"https://listedarticles.com/articles/we-can-do-better-than-line-buffered-output.md","example":false,"citation":"Chris Wellons, null program. \"We can do better than line-buffered output.\" 5 Nov 2026. https://nullprogram.com/blog/2026/11/05/ (public-domain)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://nullprogram.com/blog/2026/11/05/"},"body_markdown":"[Buffered output](/blog/2023/02/13/) typically comes in two flavors: fully buffered\nand line buffered. If output goes to a terminal then the implementation\nautomatically selects line buffering, flushing its buffer when a line is\ncompleted so that it reaches the user immediately. Otherwise it fully\nbuffers, holding output until the buffer is full or an explicit flush\nevent at some arbitrary point in the future. Line buffering is a common\nbottleneck in non-interactive terminal applications such that it’s worth\nredirecting output to a file or pipe in order to avoid it. *It doesn’t\nhave to be this way*. We can have our cake and eat it, too: promptly\ndelivering lines to a terminal at fully-buffered speeds.\n\nFor example, Info-ZIP `unzip` prints file names as it extract them from an\narchive. This printout dominates extraction time of archives with many\nsmall files. Sometimes I run `unzip` without options, then my terminal\nfloods with file names and I know that my extraction is bottlenecked. So I\ncancel and restart with `-q` (quiet) to avoid the bottleneck, because it’s\nfaster to start over than wait. Conscious of this experience, I avoided\nthe problem in [tugz](/blog/2026/10/13/) `unzip` without changing the user interface. You\nget a listing but you don’t have to pay for it.\n\n```\n$ unzip example.zip\nArchive:  example.zip\n  inflating: example.bmp\n  inflating: example.svg\n  inflating: example.txt\n```\n\nConsider this C program that prints three lines:\n\n```\n#include <stdio.h>\nint main()\n{\n    puts(\"a\\nb\");\n    puts(\"c\");\n}\n```\n\nIf we examine the writes with, say, [strace](/blog/2018/06/23/):\n\n```\n$ strace -o log -e write ./a.out\na\nb\nc\n```\n\nWe might see (here glibc):\n\n```\nwrite(1, \"a\\n\", 2)                      = 2\nwrite(1, \"b\\n\", 2)                      = 2\nwrite(1, \"c\\n\", 2)                      = 2\n```\n\nYes, glibc takes line buffering *quite literally*, even splitting writes\non a string. Some implementations are a little more sophisticated:\n\n```\nwrite(1, \"a\\nb\\n\", 2)                   = 4\nwrite(1, \"c\\n\", 2)                      = 2\n```\n\nCPython makes exactly those writes for this program:\n\n```\nimport sys\nsys.stdout.write(\"a\\nb\\n\")\nsys.stdout.write(\"c\\n\")\n```\n\nIts rule is to flush if its buffer contains a newline after appending the\nnew output, with the consequence of possibly outputting partial lines.\nBuilding on [the previous example](/blog/2023/02/13/):\n\n```\nenum {\n    BUF_ERR  = 1 << 0,\n    BUF_LINE = 1 << 1,\n};\ntypedef struct {\n    uint8_t  *buf;\n    ptrdiff_t cap;\n    ptrdiff_t len;\n    int       fd;\n    int       flags;\n} Buf;\nbool os_write(int fd, void *, ptrdiff_t);\nvoid flush(Buf *b)\n{\n    if (!(b->flags & BUF_ERR) && b->len) {\n        if (!os_write(b->fd, b->buf, b->len)) {\n            b->flags |= BUF_ERR;\n        }\n        b->len = 0;\n    }\n}\n```\n\nSuch line buffering might be achieved with a test-then-flush:\n\n```\nbool has_newline(Buf *b)\n{\n    int r = 0;\n    for (ptrdiff_t i = 0; i < b->len; i++) {\n        r |= b->buf[i] == '\\n';\n    }\n    return r;\n}\nvoid append(Buf *b, uint8_t *src, ptrdiff_t len)\n{\n    uint8_t *end = src + len;\n    while (!(b->flags & BUF_ERR) && src<end) {\n        ptrdiff_t left   = end - src;\n        ptrdiff_t avail  = b->cap - b->len;\n        ptrdiff_t amount = avail<left ? avail : left;\n        memcpy(b->buf+b->len, src, amount);\n        b->len += amount;\n        src += amount;\n        if (amount < left) {\n            flush(b);  // make room\n        }\n    }\n    // test-then-flush if line buffered\n    if ((b->flags & BUF_LINE) && has_newline(b)) {\n        flush(b);\n    }\n}\n```\n\nIf we’ve got a bunch of lines to print and append them one after another\ninto the buffer, that’s a lot of writes which could have been one.\n\n### Eat all the cake\n\nLet’s think about our goal: If the output is a terminal then we don’t want\nto hold back lines indefinitely. That’s not the same as a write on every\nnewline. There’s already a solution for the problem where we’re in a hurry\nto send out data, but the underlying, *buffer-oblivious* caller makes lots\nof small writes: [Nagle’s algorithm](https://en.wikipedia.org/wiki/Nagle%27s_algorithm).\n\nThat is, **flush the buffer if it’s been idle a short time**. We don’t\nactually care about newlines in particular. The catch is that it requires\ntweaking the interface. Add a deadline to the buffer:\n\n```\ntypedef struct {\n    int64_t   deadline;\n    // ...\n} Buf;\nint64_t os_monotonic_clock();\nvoid deadline_flush(Buf *b)\n{\n    if ((b->flags & BUF_LINE) && b->len) {\n        int64_t now = os_monotonic_clock();\n        if (b->deadline && now>b->deadline) {\n            flush(b);\n            b->deadline = 0;\n        } else {\n            b->deadline = now + DEADLINE;\n        }\n    }\n}\n```\n\nWhere `DEADLINE` is something appropriate for human perception, like 100ms\nor so. We could poke this concurrently in a thread, awoken via futex when\ndata is written to the buffer. But better to poke it between short stints\nof work, which is how tugz works. For example (using [my strings](/blog/2025/01/19/#strings)):\n\n```\n    for (/* filename in filenames */) {\n        Str pre = S(\"  inflating: \");\n        Str suf = S(\"\\n\");\n        append(&stdout, pre.data, pre.len);\n        append(&stdout, filename.data, filename.len);\n        append(&stdout, suf.data, suf.len);\n        while (/* not done inflating */) {\n            // 1. inflate block\n            // 2. write block\n            deadline_flush(&stdout);\n        }\n    }\n```\n\nIf it processes multiple files within the deadline, their names write out\ntogether. No threads. The behavior difference compared to traditional\nflush-on-newlines is imperceptible to a humans, aside from the fact their\nprogram runs faster. One more reason not to bother with [crummy C\nstdio](/blog/2023/02/11/).","body_html":"<p><a href=\"/blog/2023/02/13/\">Buffered output</a> typically comes in two flavors: fully buffered\nand line buffered. If output goes to a terminal then the implementation\nautomatically selects line buffering, flushing its buffer when a line is\ncompleted so that it reaches the user immediately. Otherwise it fully\nbuffers, holding output until the buffer is full or an explicit flush\nevent at some arbitrary point in the future. Line buffering is a common\nbottleneck in non-interactive terminal applications such that it’s worth\nredirecting output to a file or pipe in order to avoid it. *It doesn’t\nhave to be this way*. We can have our cake and eat it, too: promptly\ndelivering lines to a terminal at fully-buffered speeds.</p>\n<p>For example, Info-ZIP <code>unzip</code> prints file names as it extract them from an\narchive. This printout dominates extraction time of archives with many\nsmall files. Sometimes I run <code>unzip</code> without options, then my terminal\nfloods with file names and I know that my extraction is bottlenecked. So I\ncancel and restart with <code>-q</code> (quiet) to avoid the bottleneck, because it’s\nfaster to start over than wait. Conscious of this experience, I avoided\nthe problem in <a href=\"/blog/2026/10/13/\">tugz</a> <code>unzip</code> without changing the user interface. You\nget a listing but you don’t have to pay for it.</p>\n<pre><code>$ unzip example.zip\nArchive:  example.zip\n  inflating: example.bmp\n  inflating: example.svg\n  inflating: example.txt</code></pre>\n<p>Consider this C program that prints three lines:</p>\n<pre><code>#include &lt;stdio.h&gt;\nint main()\n{\n    puts(&quot;a\\nb&quot;);\n    puts(&quot;c&quot;);\n}</code></pre>\n<p>If we examine the writes with, say, <a href=\"/blog/2018/06/23/\">strace</a>:</p>\n<pre><code>$ strace -o log -e write ./a.out\na\nb\nc</code></pre>\n<p>We might see (here glibc):</p>\n<pre><code>write(1, &quot;a\\n&quot;, 2)                      = 2\nwrite(1, &quot;b\\n&quot;, 2)                      = 2\nwrite(1, &quot;c\\n&quot;, 2)                      = 2</code></pre>\n<p>Yes, glibc takes line buffering <em>quite literally</em>, even splitting writes\non a string. Some implementations are a little more sophisticated:</p>\n<pre><code>write(1, &quot;a\\nb\\n&quot;, 2)                   = 4\nwrite(1, &quot;c\\n&quot;, 2)                      = 2</code></pre>\n<p>CPython makes exactly those writes for this program:</p>\n<pre><code>import sys\nsys.stdout.write(&quot;a\\nb\\n&quot;)\nsys.stdout.write(&quot;c\\n&quot;)</code></pre>\n<p>Its rule is to flush if its buffer contains a newline after appending the\nnew output, with the consequence of possibly outputting partial lines.\nBuilding on <a href=\"/blog/2023/02/13/\">the previous example</a>:</p>\n<pre><code>enum {\n    BUF_ERR  = 1 &lt;&lt; 0,\n    BUF_LINE = 1 &lt;&lt; 1,\n};\ntypedef struct {\n    uint8_t  *buf;\n    ptrdiff_t cap;\n    ptrdiff_t len;\n    int       fd;\n    int       flags;\n} Buf;\nbool os_write(int fd, void *, ptrdiff_t);\nvoid flush(Buf *b)\n{\n    if (!(b-&gt;flags &amp; BUF_ERR) &amp;&amp; b-&gt;len) {\n        if (!os_write(b-&gt;fd, b-&gt;buf, b-&gt;len)) {\n            b-&gt;flags |= BUF_ERR;\n        }\n        b-&gt;len = 0;\n    }\n}</code></pre>\n<p>Such line buffering might be achieved with a test-then-flush:</p>\n<pre><code>bool has_newline(Buf *b)\n{\n    int r = 0;\n    for (ptrdiff_t i = 0; i &lt; b-&gt;len; i++) {\n        r |= b-&gt;buf[i] == &#39;\\n&#39;;\n    }\n    return r;\n}\nvoid append(Buf *b, uint8_t *src, ptrdiff_t len)\n{\n    uint8_t *end = src + len;\n    while (!(b-&gt;flags &amp; BUF_ERR) &amp;&amp; src&lt;end) {\n        ptrdiff_t left   = end - src;\n        ptrdiff_t avail  = b-&gt;cap - b-&gt;len;\n        ptrdiff_t amount = avail&lt;left ? avail : left;\n        memcpy(b-&gt;buf+b-&gt;len, src, amount);\n        b-&gt;len += amount;\n        src += amount;\n        if (amount &lt; left) {\n            flush(b);  // make room\n        }\n    }\n    // test-then-flush if line buffered\n    if ((b-&gt;flags &amp; BUF_LINE) &amp;&amp; has_newline(b)) {\n        flush(b);\n    }\n}</code></pre>\n<p>If we’ve got a bunch of lines to print and append them one after another\ninto the buffer, that’s a lot of writes which could have been one.</p>\n<h3 id=\"eat-all-the-cake\">Eat all the cake</h3>\n<p>Let’s think about our goal: If the output is a terminal then we don’t want\nto hold back lines indefinitely. That’s not the same as a write on every\nnewline. There’s already a solution for the problem where we’re in a hurry\nto send out data, but the underlying, <em>buffer-oblivious</em> caller makes lots\nof small writes: <a href=\"https://en.wikipedia.org/wiki/Nagle%27s_algorithm\" rel=\"nofollow ugc noopener\">Nagle’s algorithm</a>.</p>\n<p>That is, <strong>flush the buffer if it’s been idle a short time</strong>. We don’t\nactually care about newlines in particular. The catch is that it requires\ntweaking the interface. Add a deadline to the buffer:</p>\n<pre><code>typedef struct {\n    int64_t   deadline;\n    // ...\n} Buf;\nint64_t os_monotonic_clock();\nvoid deadline_flush(Buf *b)\n{\n    if ((b-&gt;flags &amp; BUF_LINE) &amp;&amp; b-&gt;len) {\n        int64_t now = os_monotonic_clock();\n        if (b-&gt;deadline &amp;&amp; now&gt;b-&gt;deadline) {\n            flush(b);\n            b-&gt;deadline = 0;\n        } else {\n            b-&gt;deadline = now + DEADLINE;\n        }\n    }\n}</code></pre>\n<p>Where <code>DEADLINE</code> is something appropriate for human perception, like 100ms\nor so. We could poke this concurrently in a thread, awoken via futex when\ndata is written to the buffer. But better to poke it between short stints\nof work, which is how tugz works. For example (using <a href=\"/blog/2025/01/19/#strings\">my strings</a>):</p>\n<pre><code>    for (/* filename in filenames */) {\n        Str pre = S(&quot;  inflating: &quot;);\n        Str suf = S(&quot;\\n&quot;);\n        append(&amp;stdout, pre.data, pre.len);\n        append(&amp;stdout, filename.data, filename.len);\n        append(&amp;stdout, suf.data, suf.len);\n        while (/* not done inflating */) {\n            // 1. inflate block\n            // 2. write block\n            deadline_flush(&amp;stdout);\n        }\n    }</code></pre>\n<p>If it processes multiple files within the deadline, their names write out\ntogether. No threads. The behavior difference compared to traditional\nflush-on-newlines is imperceptible to a humans, aside from the fact their\nprogram runs faster. One more reason not to bother with <a href=\"/blog/2023/02/11/\">crummy C\nstdio</a>.</p>","headings":[{"level":3,"text":"Eat all the cake","id":"eat-all-the-cake"}]}}