Skip to content

Blog

The Bag of Holding

The small, honest tools too little for a post of their own, and a look back at the whole party.

Not every tool earns its own chapter. This is the last of the campaign: the bag of small, honest libraries too little for a post apiece, tipped out onto the table so none of them is lost, and then a look back at the whole party we assembled from one search engine, and what it meant to take it apart.

The Names Enums Throw Away

Getting an enum's name back at compile time, which the language keeps refusing to give you.

The compiler knows the names of your enum members, you just wrote them, and then it throws them away, leaving you to hand-maintain a switch that maps each value back to its own name for every log line and every parse. The twenty-eighth familiar gives the names back, at compile time, through a perfect hash, so an enum and its strings are one constexpr mapping you can even check in a static_assert.

No Collisions, Ever

A hash table for a fixed key set, built entirely at compile time, that never collides and never branches.

An ordinary hash table has bad days: two keys collide, the lookup degrades to a probe or a chain, and worst-case is worse than average. If you know all your keys at compile time, you can eliminate the bad day entirely. The twenty-seventh familiar builds a minimal perfect hash over a fixed key set while the program compiles, so every lookup is a fixed handful of arithmetic and two array reads, forever, with no collision possible.

Before the Program Runs

Switching on a string, classifying a character, and coloring a terminal, all resolved at compile time.

The cheapest work is the work you do before the program starts. This is the compile-time chapter, and it opens with three small libraries that each move a runtime cost to zero: a way to switch on a string, a way to classify a character without the standard library's locale baggage, and a way to color a terminal with a single portable constant. All of it resolved by the compiler, none of it paid at runtime.

Work, Not Threads

Why a thread pool stops being enough, and what a work pool does instead.

A thread pool is the least surprising thing in a concurrent program, until thousands of owners share it. The twenty-fifth familiar started life as a thread pool and grew into a work pool: the same threads, but shared by owners instead of handed out by arrival, so a query never waits behind somebody else's housekeeping, and an idle pool costs nothing.