Context
I just learned about this book 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:
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.