New open-source C++ library generates perfect hash functions for compile-time string lookups
A blog post describes a C++ technique for mapping fixed sets of strings (like HTTP methods or keywords) to values without runtime hash collisions. Following the design of the decades-old gperf tool, the approach builds an association table so each known key maps to a unique slot using only key length and a few selected characters. The author presents working code examples, including mapping HTTP methods such as GET, PUT, POST and DELETE to distinct slots via a simple formula.
GoKawiil's interpretation of the reporting above, not reported fact.
Because the keys are known at compile time, this method can eliminate the overhead of generic hash tables—no collision handling, no pointer chasing—which could make string-matching code in servers, parsers and compilers noticeably faster. The piece positions perfect hashing as a practical, low-level optimization technique that developers writing performance-critical C++ code, such as web servers or compilers, might consider instead of defaulting to std::unordered_map.
- Perfect hash functions avoid collisions entirely when the key set is fixed and known in advance.
- The technique computes a slot using key length plus values from a small per-character association table, similar to gperf from 1989.
- Example shown maps HTTP methods (GET, PUT, POST, DELETE) to unique slots using just the first character of each string.
Source: lemire.me — Https, 2026-10-28
Published there as: “Billion-query-per-second lookup tables in C++”
Read the original report → The summary and analysis above are GoKawiil's own, written from reporting by the source above. Facts and quotes belong to the original publisher.