jermp/pthash 是一个托管在 GitHub 上的开源项目,主要使用 C++ 编写,采用 MIT License 许可证,目前获得 270 个 Star、37 次 Fork。
仓库全名
jermp/pthash
开源许可证
MIT License
Star 数
270
Fork 数
37
未关闭 Issue
1
README 摘要
PTHash is a C++ library implementing fast and compact minimal perfect hash functions as described in the following research papers: PTHash: Revisiting FCH Minimal Perfect Hashing (SIGIR 2021), Parallel and External-Memory Construction of Minimal Perfect Hash Functions with PTHash (TKDE 2023), PHOBIC: Perfect Hashing with Optimized Bucket Sizes and Interleaved Coding (ESA 2024). Please, cite these papers if you use…
Rust: if you use the Rust programming language, we recommend the PtrHash library.
Development note
The description of PTHash in the SIGIR and TKDE papers uses the c parameter
to control the number of buckets for the search phase of the algorithm.
You can get a version of the library using the c parameter here (Release v2).
The current library
uses instead a parameter called “lambda”, as described in the ESA paper.
Features
Minimal and Non-Minimal Perfect Hash Functions
Space/Time Efficiency: fast lookup within compressed space
External-Memory Scaling
Multi-Threaded Construction
Configurable: can offer different trade-offs (between construction time, lookup time, and space effectiveness)
Introduction
Given a set S of n distinct keys, a function f that bijectively maps the keys of S into the first n natural numbers
is called a minimal perfect hash function (MPHF) for S.
Algorithms that find such functions when n is large and retain constant evaluation time are of practical interest.
For instance, search engines and databases typically use minimal perfect hash functions to quickly assign identifiers to static sets of variable-length keys such as strings.
The challenge is to design an algorithm which is efficient in three different aspects: time to find f (construction time), time to evaluate f on a key of S (lookup time), and space of representation for f.
PTHash is one such algorithm.
The following guide is meant to provide a brief overview of the library
by illustrating its functionalities through some examples.
To construct a perfect hash function, include pthash.hpp and create an instance of pthash::single_phf<...> (PTHash),
pthash::partitioned_phf<...> (PTHash-HEM), or pthash::dense_partitioned_phf<...> (PHOBIC).
For convenience, we also give pthash::phobic<...> which includes the configuration options for
optimized bucket assignment function and interleaved coding. Refer to src/example.cpp for an example.
Compiling the Benchmark and Example Code
The code is tested on Linux with gcc and on Mac OS with clang (both Intel and ARM processors, like Apple M1).
To build the code, CMake is required.
If you have cloned the repository without--recursive, be sure you pull the dependencies with the following command before
compiling:
git submodule update --init --recursive
To compile the code for a release environment (see file CMakeLists.txt for the used compilation flags), it is sufficient to do the following:
mkdir build
cd build
cmake ..
make -j
For a testing environment, use the following instead:
mkdir debug_build
cd debug_build
cmake .. -D CMAKE_BUILD_TYPE=Debug -D PTHASH_USE_SANITIZERS=On
make
NOTE: Beware that the software will result in a much slower execution when running in debug mode and using sanitizers. Use this only for debug purposes, not to run performance tests.)
Enable Large Bucket-Id Type
By default, PTHash assumes there are less than $2^{32}$ buckets, hence 32-bit integers are used
for bucket ids. To overcome this, you can either lower the value of c or recompile with
cmake .. -D PTHASH_ENABLE_LARGE_BUCKET_ID_TYPE=On
to use 64-bit integers for bucket ids.
Quick Start
For a quick start, see the source file src/example.cpp.
The example shows how to setup a simple build configuration
for PTHash (parameters, base hasher, bucketer, and encoder types).
After compilation, run this example with
./example
Build Examples
After compilation (see the section Compiling the Code),
the driver program called build can be used to run some examples.
Run
./build --help
shows the usage of the driver program. In the following, we illustrate some examples using this ./build program.
builds a MPHF using most of the parameters used in Example 2 but the function is built in external memory (option --external) using 1 GB of RAM (option -m 1), with avg. partition size of 2.5M,
and compressing the data structure with a partitioned compact encoding (-e PC).
Reading Keys from Standard Input
You can make the build tool read the keys from stardard input using bash pipelining (|)
in combination with option -i -. This is very useful when building keys from compressed files.
Note: you may need to write zcat < foo.txt.gz | (...) on Mac OSX.
One caveat of this approach is that it is not possible to use --check nor benchmark query time because these two options need to re-iterate over the keys from the stream.
Benchmarks
The directory benchmarks includes some performance benchmarks.
评论