2.1 KiB
← Back to Twitter thread index
title: "So I can liter error checks around stuff with little actual cost," author: "NOTimothyLottes" handle: "@NOTimothyLottes" post_url: "https://x.com/NOTimothyLottes/status/2076893128515112995" post_id: "2076893128515112995" timestamp: "2026-07-14 04:54:43" post_count: 6 reply_count: 0 repost_count: 0 like_count: 3 view_count: 419
@NOTimothyLottes — So I can liter error checks around stuff with little actual cost,
Post 1 (2026-07-14 04:36:51)
Another round of getting optimized error checking. Disassembly [after syscall] shows that it works. (1.) Volatile store LINE (2.) Volatile store "error code" (3.) TEST if error (4.) Conditional forward branch on error [static prediction untaken] AS_MINIMAL_AS_ONE_CAN_GET
Post 2 (2026-07-14 04:43:38) — reply to Post 1
A macro cheat sheet to try to explain how it works ...
Post 3 (2026-07-14 04:46:54) — reply to Post 2
I have a collection of force inline wrappers that test returns from syscalls/functions for error and return the return. These all leverage builtin_expect so the compiler knows to make them fall through in the common case.
Post 4 (2026-07-14 04:50:23) — reply to Post 3
Those force inlines call Err() on terminal error. The Err() function ensures the {LINE, error} stores are visible, then triggers the console drawing code to kill the app with printed error. Then it sleeps until the termination.
Post 5 (2026-07-14 04:52:35) — reply to Post 4
Compiler eventually screws up, but at least it gets the fast path correct, and the slow error path gets an extra call. Effectively it conditionally forward branches to a distant call to Err() instead of just branching to the Err().
Post 6 (2026-07-14 04:54:43) — reply to Post 5
So I can liter error checks around stuff with little actual cost, (1.) Two stores (2.) A CMP or TEST (3.) An ignored on no-error branch on the first execution! [this code almost never runs 2 times].



