.. /index

Perfect Hashing

Perfect Hashing

A class of hash table constructions that guarantee zero collisions for a known, static key set — making lookups branch-free and probe-free. For read-only data, perfect hashing is unbeatable; nothing in the swiss-table family can match it because a Swiss Table still has to probe at least once. The price is paid at construction time, which can be slow.

The basic idea

Given a fixed set S of keys known up front, build a hash function h such that h is injective on S — every key maps to a distinct slot, no probing needed. Lookups become: hash, index, single key compare. The construction explores hash function families until it finds one with no collisions on S, sometimes via a two-level scheme (FKS hashing) or via perfect-hash builders that compose multiple hash functions.

Modern variants:

When it wins

When it loses

For dynamic workloads, use boost-unordered-flat-map or abseil-flat-hash-map. For static read-only data, perfect hashing is the right tool — and the speedup over even the best Swiss Table is real because there is no probing whatsoever.

Related ideas

C#/.NET's frozen-dictionary takes an analogous approach without going all the way to perfect hashing: build-time analysis of the key set produces a read-optimized layout. Frozen dictionary is faster than mutable Dictionary<TKey, TValue> but does not guarantee the zero-collision property. See fastest-hash-map-2025 for context.

Linked from

Sources