phf

Compile-time perfect hash function maps and sets for zero-cost static lookups in Rust.

Library
Cargo
v0.14.0
2,181stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
76/100Good
Development Activity80
Maintenance64
Community60
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
83/100Excellent
Architecture88
Code Quality86
Innovation80
Learning Curve78

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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search