From 591e4e85ef18aa510a438be40485c696249c30d1 Mon Sep 17 00:00:00 2001 From: Nate Anderson Date: Wed, 1 Oct 2025 22:45:24 -0600 Subject: [PATCH] added the flush til --- .aspell.en.pws | 4 ++ content/tils/why-we-cant-forget-to-flush.md | 46 +++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 content/tils/why-we-cant-forget-to-flush.md diff --git a/.aspell.en.pws b/.aspell.en.pws index 82613e6..389dc0a 100644 --- a/.aspell.en.pws +++ b/.aspell.en.pws @@ -2,6 +2,10 @@ personal_ws-1.1 en 0 lastmod showTableOfContents Zig +zig Nim tils Spotify +normie +syscall +syscalls diff --git a/content/tils/why-we-cant-forget-to-flush.md b/content/tils/why-we-cant-forget-to-flush.md new file mode 100644 index 0000000..f6ff65a --- /dev/null +++ b/content/tils/why-we-cant-forget-to-flush.md @@ -0,0 +1,46 @@ +--- +date: 2025-10-01T22:32:18-06:00 +description: "" +lastmod: 2025-10-01T22:32:18-06:00 +showTableOfContents: true +type: "tils" +title: "TIL: Why We Cant Forget to Flush" +image: "" +image_credit: "" +image_alt: "" +tags: ["zig", "programming"] +--- + +# Context + +I just learned about this book [Systems Programming with Zig](https://www.manning.com/books/systems-programming-with-zig). +I'm reading through the free introductory chapters to familiarize myself with things I thought I'd already know. But, as +a college drop-out, I think there are a handful of low level concepts I missed, and buffer flushing was one of them! + +# Reflection + +Zig is all about removing implicit behavior. Its number one of the `zig zen` after all: + +```shell +nate in ~/source/zig-systems on main λ zig zen + + * Communicate intent precisely. + * Edge cases matter. + * Favor reading code over writing code. + * Only one obvious way to do things. + * Runtime crashes are better than bugs. + * Compile errors are better than runtime crashes. + * Incremental improvements. + * Avoid local maximums. + * Reduce the amount one must remember. + * Focus on code rather than style. + * Resource allocation may fail; resource deallocation must succeed. + * Memory is a resource. + * Together we serve the users. +``` + +When you want to write some text to the console, the program needs to make a syscall to actually send that data to +the OS, to then print out those characters. By default, most languages flush buffers for you, because when you call +`writeOut("some text")`, you expect it to write it! But, to save on syscalls, zig will buffer those bytes, and any more +bytes, until you tell it to `flush()`, which then tells your program to make the syscall so we can see those beautiful +bytes.