Part of Familiars: the small, sharp libraries I carved out of Xapiand. This opens the compile-time chapter with three tiny ones: hashes, char-classify, and term-color.
The fastest code is the code that never runs, and the closest thing to that in C++ is code the compiler runs for you, before main ever starts. constexpr turned that from a party trick into a tool: a value computed at compile time costs nothing at runtime, and a mistake in it becomes a compile error instead of a bug. Three of the smallest familiars live entirely in that world, and each one takes a cost you would normally pay every time the program runs and pays it once, at the build.
A switch over strings
Section titled “A switch over strings”C++ lets you switch on an integer and, pointedly, not on a string. The idiomatic workarounds are an if/else if ladder of == comparisons, which is O(cases) and ugly, or building a std::map at startup, which allocates and is O(log n). hashes offers the third way: a header of constexpr hash functions, FNV-1a, djb2, a constexpr xxHash-64, integer mixers, jump-consistent hashing, plus _fnv1a and _xx user-defined literals. Because the hash of a string literal is a compile-time constant, you can hash the input once and switch on it, with case labels like "commit"_fnv1a that the compiler folds to integers:
switch (fnv1ah32::hash(verb)) { case "commit"_fnv1a: return do_commit(); case "search"_fnv1a: return do_search(); // ...}That is a switch over strings with no allocation and no string compares, the case labels checked at compile time. Hash values are sparse, so the compiler builds a binary search rather than a jump table; the next post makes them dense. It is the trick that turns a request verb or a token into a branch without a map or a ladder, and it is exactly how a search engine dispatches its dozens of operation names.
A character table with no locale
Section titled “A character table with no locale”Underneath a lot of parsing sits the humble question “is this byte a digit? a letter? whitespace?”, and the standard answer, <cctype>’s isdigit and friends, carries baggage most parsers do not want: it is locale-dependent, so the answer can change under you, and it is a function call, not a constant. char-classify replaces it with one 256-entry table of packed flags and a set of constexpr masked lookups, is_digit, is_alpha, is_space, is_hexdigit, tolower, toupper, and a byte-to-hex char_repr, with nothing behind them but the standard library. Every classification is a compile-time-capable array read, locale-free and deterministic, so a lexer built on it is faster, portable, and cannot be quietly changed by a setlocale somewhere else in the process.
Color that stays portable
Section titled “Color that stays portable”term-color has the cleverest trick of the three. A named color like RED or STEEL_BLUE expands, at compile time, into an ANSI escape sequence, but the problem is that terminals disagree about which escapes they understand: some do 16 colors, some 256, some 24-bit truecolor. So term-color emits three escapes back to back, worst first: the 16-color code, then the 256-color code, then the truecolor code. A terminal applies each escape it recognizes in turn and ends on the last one it supports, so the richest tier it can render wins, and a single compile-time constant is portable across every terminal without any runtime detection. When you do need one guaranteed tier, to honor a --color mode, the NO_COLOR convention, or a plain log file that should get no escapes at all, collapse() and apply() resolve the stacked string down at runtime. The default is a constant that just works; the runtime path is there for when “just works” is not precise enough.
The compile-time shelf
Section titled “The compile-time shelf”These three are one corner of a larger idea, and the chapter they open is the rest of it: the Familiars that mainly live at build time, each moving a cost you would otherwise pay at runtime onto the compiler. Sorted by the job they do:
- Hashing. hashes:
constexprFNV-1a, djb2, aconstexprxxHash-64, integer mixers, jump-consistent hashing, and the_fnv1aand_xxliterals. The string switch above stands on it. - Perfect hashing. constexpr-phf: a minimal perfect hash over a fixed set of integer keys, built while the program compiles, so a known set of keys becomes a dense, collision-free, branch-free index into a table. The next post is all about it.
- Character classification. char-classify: the locale-free
constexprbyte tables above,is_digitand its siblings with nothing behind them but a masked array read. - Enum reflection. enum-reflection, with utype: turn an
enumvalue back into the name you wrote it with, and the name back into the value, both resolved at compile time. Its post closes the chapter. - Terminal color. term-color: the worst-first stacked ANSI constant above, portable across terminals with no runtime detection.
Different jobs, one habit: the answer is a constant the compiler already worked out.
Where they fit
Section titled “Where they fit”Reach for these when a cost you keep paying at runtime, a string dispatch, a character check, a color code, could be a constant the compiler folds instead. Skip them where the input is genuinely dynamic and no compile-time form exists. They are small, but they are the honest shape of the idea the rest of this chapter runs on: do it once, at the build.
The next familiar is the machinery under that string switch, made exact: a hash table for a fixed set of keys, built entirely at compile time, that is guaranteed to never collide.