Skip to content

Blog

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.

Sometimes a Mutex Is Right

A blocking channel that is not trying to be fast, only correct, with timeouts and a shutdown that actually drains.

After a chapter of lock-free structures that avoid mutexes at all costs, here is the honest counterweight: a queue that reaches straight for one. The twenty-fourth familiar is not chasing throughput. It is chasing the two things fast queues are bad at, waiting with a timeout and shutting down without losing or hanging on the items still in flight, and for those, a mutex and two condition variables are simply the right tool.

Built to Disappear

An atomic shared_ptr that becomes a one-line alias the moment the standard library grows its own.

You want to atomically swap a shared_ptr, publish a new version of some shared state without a lock of your own, and read it safely from other threads. C++20 finally standardized that, but not every compiler shipped it. The twenty-third familiar fills the gap where it is missing and, more interestingly, is written to delete itself the moment the standard library grows the feature, so the dependency has a built-in expiry date.