Many software systems rely on mapping strings to specific values, such as identifying HTTP methods (GET, POST), recognizing header names, or parsing compiler keywords. In C++, the standard approach is typically to use `std::unordered_map`. However, this method incurs runtime costs because the keys are often known at compile time. Standard hash tables must handle potential collisions by hashing the entire string, selecting a bucket, and potentially walking a linked list, all of which introduce overhead that can be avoided if the key set is static.
To solve this, the library utilizes a perfect hash function—a function designed specifically for a fixed set of keys to ensure no two keys map to the same slot. The mechanism follows a design similar to the `gperf` tool: it computes a slot by adding the key's length to small values from an association table indexed by characters at specific positions. This process is executed at compile time using `constexpr` functions, ensuring the runtime cost is minimal. For example, looking up a key involves a few loads and a bitwise AND operation to determine the slot, followed by a single comparison. This eliminates the need for probing or traversing collision chains.

The visual explains the minimal runtime cost of the lookup process, which involves only a few loads and a bitwise operation.
Beyond hashing, the library optimizes string comparison using SIMD (Single Instruction, Multiple Data) instructions. Instead of looping over bytes with multiple branches, the system loads the key into a 16-byte register and uses a shuffle instruction (like `tbl` on ARM NEON) to zero out bytes past the key's end. This allows a single vector comparison to validate the entire key in just a few instructions. Benchmarks on an Apple M3 Max show the library achieving approximately 1.3 ns per lookup for HTTP headers, making it up to thirteen times faster than `std::unordered_map`. While the approach is highly efficient for known keys, it is less suitable for dynamic sets where keys are added or removed at runtime, as the perfect hash function must be recalculated for any change in the key set.

The chart illustrates the significant speed advantage of the perfect hash library over standard implementations for common HTTP headers.

This figure demonstrates the library's efficiency across different types of static key sets, highlighting its high-speed performance.