Skip to content

compile-time

4 posts with the tag “compile-time”

A Tree Made of Types

A word lookup the compiler builds letter by letter, and why it lost the race.

A dictionary does not make you read every word to find one; you follow the letters. The thirty-second familiar builds that path out of the compiler's own type system, one letter at a time, so a fixed word list becomes a tree that exists only while the program compiles. It is exact, simple, and slower than a perfect hash, and the numbers say by how much.

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.