phf
Compile-time perfect hash function maps and sets for zero-cost static lookups in Rust.
Repository Health
Technical Analysis
phf (Rust-PHF) generates efficient lookup tables at compile time using perfect hash functions. Instead of building a HashMap at runtime, you declare maps and sets whose contents are known at compile time and get O(1) lookups with no hashing collisions, no runtime construction cost, and no heap allocation.
Maps can be created with the phf_map! and phf_set! procedural macros or generated by build scripts via phf_codegen, and the runtime crate works in no_std environments, making it a go-to for static dispatch tables, keyword sets, and lookup-heavy parsers.
What You Get
- phf::Map and phf::Set types for immutable, compile-time-constructed lookup tables
- The phf_map! and phf_set! procedural macros for declaring tables inline
- phf_codegen for generating tables from build scripts when data comes from files or computation
- Ordered map and set variants that preserve insertion order
- no_std support so tables can be used in embedded and allocation-free environments
Common Use Cases
- Building keyword or reserved-word sets for lexers and parsers
- Static dispatch tables mapping string keys to handlers or constants
- Embedding large read-only lookup data generated at build time
- Fast membership checks in no_std or performance-critical code paths
Under The Hood
Architecture - Rust-PHF is a Cargo workspace split by responsibility: phf is the lightweight runtime crate (map.rs, set.rs, and their ordered variants) that generated tables reference; phf_shared holds shared hashing logic; phf_generator computes the perfect hash; phf_macros provides the procedural macros; and phf_codegen offers a build-script API. The runtime crate only performs the final hash-and-index lookup, keeping it tiny.
Tech Stack - Pure Rust (edition 2024, MSRV 1.85). The runtime depends on phf_shared and optionally serde; the default CHD algorithm and an experimental ptrhash feature govern table layout. A std feature toggles libcore-only builds for no_std targets.
Code Quality - The project is long-lived and actively maintained (900+ commits, 70+ contributors, frequent releases) with dedicated test crates (phf_macros_test, phf_macros_no_macros_test) validating macro expansion and behavior, plus cargo-deny configuration for dependency auditing.
API Design - The common path is a one-line phf_map!/phf_set! declaration that produces a value usable like a HashMap for reads, so adoption is easy. The main learning curve is understanding the macros-vs-codegen split and the const-construction constraints, but the runtime read API (get, contains_key, iteration) is idiomatic and minimal.
Used by 2 apps in this directory
AppFlowy
Productivity · Project Management · Collaboration
The open-source AI workspace that puts your data, your rules — with local LLMs, CRDT collaboration, and full self-hosting built in.
CubeSandbox
Developer Tools · Security · AI Agents
Instant, concurrent, hardware-isolated MicroVM sandboxes for AI agents — E2B-API compatible, sub-60ms cold starts, and a built-in zero-trust egress proxy, all self-hostable at scale.