hashify is a Rust procedural macro crate designed to create perfect hashing maps and sets without any runtime dependencies. By combining traditional and modern hashing techniques, hashify ensures exceptional speed and efficiency, making it a great tool for developers seeking optimal performance in their projects.
It provides two distinct approaches for building maps and sets, tailored for different dataset sizes:
- For small maps with fewer than 500 entries, hashify uses a strategy similar to GNU gperf with the
--switch
parameter. This approach is highly efficient and specifically optimized for small datasets. - For larger datasets, hashify employs the PTHash algorithm. This Minimal Perfect Hashing algorithm ensures both compactness and speed, making it ideal for scaling to larger datasets without compromising performance.
hashify was designed with performance as a top priority, and its benchmarks demonstrate this:
- For tiny maps, hashify is over 4 times faster than the Rust phf crate (which uses the CHD algorithm algorithm).
- For large maps, hashify achieves a consistent 40% speed improvement compared to
phf
. These results highlight its suitability for developers who require exceptional speed and efficiency in their hash map and set operations.
- Perfect Hashing: Generates collision-free lookup tables for your data at compile time.
- No Runtime Dependencies: Everything is computed at compile time, making it ideal for environments where performance and binary size matter.
- Multiple hashing strategies: Tiny maps for datasets smaller than 500 entries and large maps using the PTHash Minimal Perfect Hashing algorithm.
- Fast: Tiny maps are over 4x faster than the CHD algorithm, while large maps are approximately 40% faster.
hashify, since it uses the Fowler–Noll–Vo 1a hashing algorithm, is designed to work best with maps consisting of short strings. This makes it highly efficient for such datasets, but it may not perform as well with longer or more complex keys. However, modifying the crate to use other hashing algorithms is trivial, and we invite those interested in alternative algorithms to open a GitHub issue to discuss and potentially add support for other hashing options.
fn tiny_charsets(key: &[u8]) -> Option<u32> {
hashify::tiny_map! {
key,
"koi8_r" => 35,
"windows_1253" => 97,
"windows_1257" => 114,
"iso_8859_10" => 69,
"windows_1251" => 70,
"ks_c_5601_1989" => 64,
}
}
fn large_charsets(key: &[u8]) -> Option<&u32> {
hashify::map! {
key,
u32,
"koi8_r" => 35,
"windows_1253" => 97,
"windows_1257" => 114,
"iso_8859_10" => 69,
"windows_1251" => 70,
"ks_c_5601_1989" => 64,
}
}
fn main() {
assert_eq!(tiny_charsets("koi8_r".as_bytes()), Some(35));
assert_eq!(large_charsets("koi8_r".as_bytes()), Some(35));
}
To run the testsuite:
$ cargo test --all-features
and, to run the benchmarks:
$ cargo bench --all-features
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Copyright (C) 2025, Stalwart Labs LLC