6 releases (3 breaking)

0.4.0 Aug 27, 2024
0.3.3 Aug 22, 2024
0.2.0 Jun 18, 2024
0.1.0 May 3, 2024

#100 in Compression

Download history 211/week @ 2024-07-29 49/week @ 2024-08-05 190/week @ 2024-08-12 203/week @ 2024-08-19 208/week @ 2024-08-26 40/week @ 2024-09-02 56/week @ 2024-09-09 94/week @ 2024-09-16 77/week @ 2024-09-23 53/week @ 2024-09-30 48/week @ 2024-10-07 56/week @ 2024-10-14 39/week @ 2024-10-21 53/week @ 2024-10-28 49/week @ 2024-11-04 54/week @ 2024-11-11

200 downloads per month
Used in 2 crates (via swh-graph)

GPL-3.0-or-later

105KB
2K SLoC

Rust 1K SLoC // 0.0% comments C++ 1K SLoC // 0.1% comments Shell 14 SLoC // 0.2% comments

pthash-rs

Rust bindings for PTHash, a C++ library implementing perfect-hash functions using:

Building

apt install build-essential libclang-dev
git clone https://gitlab.softwareheritage.org/swh/devel/pthash-rs.git
cd pthash-rs
git submodule update --init --recursive
cargo build

Internal code structure

Due to C++ templates being closer to macros than to Rust generics, every possible instantiation of type parameters of the Rust struct needs to be mapped to a concrete C++ class.

This is invisible when using the crate, but means only hash algorithms and encoders explicitly allowed by this crate can be used. Additionally, the allow list can be adjusted through features to cut down on the combinatorial explosion of template instantiations and linking with Rust types; see Cargo.toml for details.

Examples

Minimal PHF

use pthash::{
    BuildConfiguration, DictionaryDictionary, Hashable, Minimal, MurmurHash2_64, Phf, SinglePhf
};

let temp_dir = tempfile::tempdir().unwrap();
let mut config = BuildConfiguration::new(temp_dir.path().to_owned());
// config.minimal_output = true; // unlike the C++ API, this is implicit from f's type

let keys: Vec<&[u8]> = vec!["abc".as_bytes(), "def".as_bytes(), "ghikl".as_bytes()];

let mut f = SinglePhf::<Minimal, MurmurHash2_64, DictionaryDictionary>::new();
f.build_in_internal_memory_from_bytes(|| &keys, &config).expect("Failed to build");

// Hashes are unique and in the [0; 3) segment
let mut hashes: Vec<u64> = keys.iter().map(|key| f.hash(key)).collect();
hashes.sort();
assert_eq!(hashes, vec![0, 1, 2]);

// Hashing an object that wasn't provided when building the function collides
assert!(f.hash(b"not_a_key".as_bytes()) < 3);

Non-minimal PHF

use pthash::{
    BuildConfiguration, DictionaryDictionary, Hashable, Nonminimal, MurmurHash2_64, Phf, SinglePhf
};

let temp_dir = tempfile::tempdir().unwrap();
let mut config = BuildConfiguration::new(temp_dir.path().to_owned());

let keys: Vec<&[u8]> = vec!["abc".as_bytes(), "def".as_bytes(), "ghikl".as_bytes()];

let mut f = SinglePhf::<Nonminimal, MurmurHash2_64, DictionaryDictionary>::new();
f.build_in_internal_memory_from_bytes(|| &keys, &config).expect("Failed to build");

// Hashes are unique
let mut hashes: Vec<u64> = keys.iter().map(|key| f.hash(key)).collect();
hashes.sort();

// But not necessarily in the [0; 3) segment (not minimal)
// assert_eq!(hashes, vec![0, 1, 2]);

// Hashing an object that wasn't provided when building the function may collide
// assert!(!hashes.contains(f.hash(b"not_a_key".as_bytes())));

Dependencies

~8–43MB
~714K SLoC