Perfect hashing hits 1.3ns lookups, ~12x faster than std::unordered_map

arpit_bhayani · x · 2026-10-29

A write-up on a C++ header-only perfect hashing library for compile-time-known key sets (HTTP methods, header names, compiler keywords, ticker symbols). At compile time it builds a hash function tailored to the exact keys, so each known key maps to a unique slot: it uses string length plus a few selected characters via a small association table (4 instructions), then verifies with a single 16-byte SIMD comparison instead of a byte loop.

Result: 1.3ns per lookup (about 5 cycles) vs 16.6ns for std::unorderedmap on the same machine. Caveats: fixed key sets only, and trivial comparisons may win for tiny key sets — but for extremely hot lookup paths, trading generality for speed can pay off.

Original post →

More from Research

Research channel →