All Projects → jermp → pthash

jermp / pthash

Licence: MIT license
Fast and compact minimal perfect hash functions in C++.

Programming Languages

C++
36643 projects - #6 most used programming language

Projects that are alternatives of or similar to pthash

prvhash
PRVHASH - Pseudo-Random-Value Hash. Hash functions, PRNG with unlimited period, randomness extractor. (Codename Gradilac/Градилак)
Stars: ✭ 194 (+212.9%)
Mutual labels:  hashing, hash, hash-function
komihash
Very fast, high-quality hash function (non-cryptographic, C) + PRNG
Stars: ✭ 68 (+9.68%)
Mutual labels:  hashing, hash, hash-function
bromberg sl2
Cayley hashing as in "Navigating in the Cayley Graph of SL₂(𝔽ₚ)"
Stars: ✭ 32 (-48.39%)
Mutual labels:  hashing, hash
Clhash
C library implementing the ridiculously fast CLHash hashing function
Stars: ✭ 220 (+254.84%)
Mutual labels:  hashing, hash
node-blake2
All four BLAKE2 variants (blake2b, blake2bp, blake2s, blake2sp) with stream support for Node.js
Stars: ✭ 52 (-16.13%)
Mutual labels:  hashing, hash
Data Structures
Data-Structures using C++.
Stars: ✭ 121 (+95.16%)
Mutual labels:  hashing, hash
Password4j
Password4j is a user-friendly cryptographic library that supports Argon2, Bcrypt, Scrypt, PBKDF2 and various cryptographic hash functions.
Stars: ✭ 124 (+100%)
Mutual labels:  hashing, hash
Util
A collection of useful utility functions
Stars: ✭ 201 (+224.19%)
Mutual labels:  hashing, hash
noble-hashes
Audited & minimal JS implementation of SHA2, SHA3, RIPEMD, BLAKE2/3, HMAC, HKDF, PBKDF2 & Scrypt
Stars: ✭ 213 (+243.55%)
Mutual labels:  hashing, hash
agent
hashtopolis.org
Stars: ✭ 19 (-69.35%)
Mutual labels:  hashing, hash
hash-checker
Fast and simple application that allows you to generate and compare hashes from files and text
Stars: ✭ 72 (+16.13%)
Mutual labels:  hashing, hash
Minperf
A Minimal Perfect Hash Function Library
Stars: ✭ 107 (+72.58%)
Mutual labels:  hashing, hash
Xxhash cpp
Port of the xxhash library to C++17.
Stars: ✭ 106 (+70.97%)
Mutual labels:  hashing, hash
Dagon
Advanced Hash Manipulation
Stars: ✭ 155 (+150%)
Mutual labels:  hashing, hash
Eternal
A C++14 compile-time/constexpr map and hash map with minimal binary footprint
Stars: ✭ 93 (+50%)
Mutual labels:  hashing, hash
Wyhash Rs
wyhash fast portable non-cryptographic hashing algorithm and random number generator in Rust
Stars: ✭ 44 (-29.03%)
Mutual labels:  hashing, hash
metrohash-rs
Rust MetroHash
Stars: ✭ 45 (-27.42%)
Mutual labels:  hashing, hash
Ahash
aHash is a non-cryptographic hashing algorithm that uses the AES hardware instruction
Stars: ✭ 251 (+304.84%)
Mutual labels:  hashing, hash
Name That Hash
🔗 Don't know what type of hash it is? Name That Hash will name that hash type! 🤖 Identify MD5, SHA256 and 3000+ other hashes ☄ Comes with a neat web app 🔥
Stars: ✭ 540 (+770.97%)
Mutual labels:  hashing, hash
sshash
A compressed, associative, exact, and weighted dictionary for k-mers.
Stars: ✭ 62 (+0%)
Mutual labels:  hashing, minimal-perfect-hash

Language grade: C/C++

PTHash

PTHash is a C++ library implementing fast and compact minimal perfect hash functions as described in the papers

Please, cite these papers if you use PTHash.

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.

Table of contents

Integration

Integrating PTHash in your own project is very simple: just get the source code and include the header include/pthash.hpp in your code. No other configurations are needed.

If you use git, the easiest way to add PTHash is via git add submodule as follows.

git submodule add https://github.com/jermp/pthash.git

Compiling the Code

The code is tested on Linux with gcc and on Mac with clang. To build the code, CMake is required.

Clone the repository with

git clone --recursive https://github.com/jermp/pthash.git

If you have cloned the repository without --recursive, you will need to perform the following commands before compiling:

git submodule init
git submodule update

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 All Encoders

By default, you can choose between three encoders to compress the PTHash data structure: partitioned_compact, dictionary_dictionary, and elias_fano, respectively indicated with PC, D-D, and EF in our papers.

If you want to test all the encoders we tested in the SIGIR paper [1], you can compile with

cmake .. -D PTHASH_ENABLE_ALL_ENCODERS=On

Quick Start

For a quick start, see the source file src/example.cpp (reported below). The example shows how to setup a simple build configuration for PTHash (parameters, bash hasher, and encoder).

After compilation, run this example with

./example

which will build a PTHash MPHF on 10M random 64-bit keys using c = 6.0 and alpha = 0.94. It also shows how to serialize the data structure on disk and re-load it for evaluation.

#include <iostream>
#include "../include/pthash.hpp"
#include "util.hpp"  // for functions distinct_keys and check

int main() {
    using namespace pthash;

    /* Generate 10M random 64-bit keys as input data. */
    static const uint64_t num_keys = 10000000;
    static const uint64_t seed = 1234567890;
    std::cout << "generating input data..." << std::endl;
    std::vector<uint64_t> keys = distinct_keys<uint64_t>(num_keys, seed);
    assert(keys.size() == num_keys);

    /* Set up a build configuration. */
    build_configuration config;
    config.c = 6.0;
    config.alpha = 0.94;
    config.minimal_output = true;  // mphf
    config.verbose_output = true;

    /* Declare the PTHash function. */
    typedef single_phf<murmurhash2_64,         // base hasher
                       dictionary_dictionary,  // encoder type
                       true                    // minimal
                       >
        pthash_type;
    pthash_type f;

    /* Build the function in internal memory. */
    std::cout << "building the function..." << std::endl;
    f.build_in_internal_memory(keys.begin(), keys.size(), config);

    /* Compute and print the number of bits spent per key. */
    double bits_per_key = static_cast<double>(f.num_bits()) / f.num_keys();
    std::cout << "function uses " << bits_per_key << " [bits/key]" << std::endl;

    /* Sanity check! */
    if (check(keys.begin(), keys.size(), f)) std::cout << "EVERYTHING OK!" << std::endl;

    /* Now evaluate f on some keys. */
    for (uint64_t i = 0; i != 10; ++i) {
        std::cout << "f(" << keys[i] << ") = " << f(keys[i]) << '\n';
    }

    /* Serialize the data structure to a file. */
    std::cout << "serializing the function to disk..." << std::endl;
    std::string output_filename("pthash.bin");
    essentials::save(f, output_filename.c_str());

    /* Now reload from disk and query. */
    pthash_type other;
    essentials::load(other, output_filename.c_str());
    for (uint64_t i = 0; i != 10; ++i) {
        std::cout << "f(" << keys[i] << ") = " << other(keys[i]) << '\n';
        assert(f(keys[i]) == other(keys[i]));
    }

	std::remove(output_filename.c_str());
    return 0;
}

Build Examples

All the examples below must be run from within the directory where the code was compiled (see the section Compiling the Code), using the driver program called build.

Running the command

./build --help

shows the usage of the driver program, as reported below.

Usage: ./build [-h,--help] num_keys c alpha encoder_type [-p num_partitions] [-s seed] [-t num_threads] [-i input_filename] [-o output_filename] [-d tmp_dir] [-m ram] [--minimal] [--external] [--verbose] [--check] [--lookup]

 num_keys
	The size of the input.

 c
	A constant that trades construction speed for space effectiveness. A reasonable value lies between 3.0 and 10.0.

 alpha
	The table load factor. It must be a quantity > 0 and <= 1.

 encoder_type
	The encoder type. See include/encoders/encoders.hpp for a list of available types.

 [-p num_partitions]
	Number of partitions.

 [-s seed]
	Seed to use for construction.

 [-t num_threads]
	Number of threads to use for construction.

 [-i input_filename]
	A string input file name. If this is not provided, then num_keys 64-bit random keys will be used as input instead.

 [-o output_filename]
	Output file name where the function will be serialized.

 [-d tmp_dir]
	Temporary directory used for building in external memory. Default is directory '.'.

 [-m ram]
	Number of Giga bytes of RAM to use for construction in external memory.

 [--minimal]
	Build a minimal PHF.

 [--external]
	Build the function in external memory.

 [--verbose]
	Verbose output during construction.

 [--check]
	Check correctness after construction.

 [--lookup]
	Measure average lookup time after construction.

 [-h,--help]
	Print this help text and silently exits.

Example 1

./build 1000000 4.5 0.99 dictionary_dictionary -s 727369 --minimal --verbose --check --lookup -o mphf.bin

This example will build a MPHF over 1M random 64-bit keys (generated with seed 727369), using c = 4.5, alpha = 0.99, and compressing the MPHF data structure with the encoder dictionary_dictionary.

The data structure will be serialized on a binary file named mphf.bin.

It will also check the correctness of the data structure (flag --check) and measure average lookup time (flag --lookup).

Construction will happen in internal memory, using a single processing thread. (Experimental setting of the SIGIR paper [1].)

Example 2

For the following example, we are going to use the strings from the UK-2005 URLs collection, which can be downloaded by clicking here. (This is also one of the datasets used in the paper.)

The file is ~300 MB compressed using gzip (2.86 GB uncompressed).

After download, place the dataset in the build directory and run

gunzip uk-2005.urls.gz

to uncompress it. The file contains one string per line, for a total of 39,459,925 strings.

NOTE: Input files are read line by line (i.e., individual strings are assumed to be separated by the character \n). Be sure there are no blank lines.

The following command will build a MPHF using the strings of the file as input keys, with c = 7.0, alpha = 0.94.

./build 39459925 7.0 0.94 dictionary_dictionary -s 1234567890 --minimal -i uk-2005.urls --verbose --check --lookup

Example 3

./build 39459925 7.0 0.94 dictionary_dictionary -s 1234567890 --minimal -i uk-2005.urls --verbose --check --lookup -p 128

This example will run the construction over the same input and parameters used in Example 2, but with 128 partitions. The resulting data structure will consume essentially the same space as that built in Example 2 and only slightly slower at lookup.

Example 4

./build 39459925 7.0 0.94 dictionary_dictionary -s 1234567890 -i uk-2005.urls --verbose --check --lookup --external

This example will run the construction over the same input and parameters used in Example 2, but using external memory. The resulting data structure will be exactly the same as that built in Example 2.

Enable Multi-Threading

You can always specify to use multiple threads for construction with -t. For example, just append -t 4 to any of the previous build commands to use 4 parallel threads. (Also consult our second paper [2] for more information about parallelism.)

Building Perfect Hash Functions (not Minimal)

Just do not specify the --minimal flag when using the build utility.

An Example Benchmark

The script script/run_benchmark.sh runs the 4 trade-off configurations (encoder, alpha, c) described in Section 5.2 of the paper [1] on 100M and 1000M keys.

C-C stands for "compact-compact" encoder; D-D for "dictionary-dictionary"; and EF for "Elias-Fano".

Be sure you run the benchmark after compiling with

cmake .. -D PTHASH_ENABLE_ALL_ENCODERS=On

From within the directory where the code has been compiled, just run

bash ../script/run_benchmark.sh 2> results.json

to reproduce the bottom part of Table 5 of the SIGIR 2021 paper [1]. (All constructions run in internal memory on a single core of the processor).

Below, the result of the benchmark across different processors and compilers. The code is compiled with -O3 and -march=native in all cases.

Intel i9-9900K @ 3.60 GHz, gcc 9.2.1, GNU/Linux 5.4.0-70-generic x86_64

Configuration 100M keys 1000M keys
constr. (sec) space (bits/key) lookup (ns/key) constr. (sec) space (bits/key) lookup (ns/key)
(1) C-C, alpha = 0.99, c = 7.0 42 3.36 28 1042 3.23 37
(2) D-D, alpha = 0.88, c = 11.0 19 4.05 46 308 3.94 64
(3) EF, alpha = 0.99, c = 6.0 45 2.26 49 1799 2.17 101
(4) D-D, alpha = 0.94, c = 7.0 26 3.23 37 689 2.99 55

Intel i7-7700 @ 3.60 GHz, gcc 9.3.0, GNU/Linux 5.4.0-66-generic x86_64

Configuration 100M keys 1000M keys
constr. (sec) space (bits/key) lookup (ns/key) constr. (sec) space (bits/key) lookup (ns/key)
(1) C-C, alpha = 0.99, c = 7.0 59 3.36 35 1145 3.23 40
(2) D-D, alpha = 0.88, c = 11.0 27 4.05 57 357 3.94 69
(3) EF, alpha = 0.99, c = 6.0 86 2.26 66 1918 2.17 110
(4) D-D, alpha = 0.94, c = 7.0 45 3.23 48 796 2.99 61

Intel i7-4790K @ 4.00GHz, gcc 8.3.0, GNU/Linux 5.0.0-27-generic x86_64

Configuration 100M keys 1000M keys
constr. (sec) space (bits/key) lookup (ns/key) constr. (sec) space (bits/key) lookup (ns/key)
(1) C-C, alpha = 0.99, c = 7.0 55 3.36 41 1156 3.23 51
(2) D-D, alpha = 0.88, c = 11.0 26 4.05 55 422 3.94 69
(3) EF, alpha = 0.99, c = 6.0 81 2.26 69 1921 2.17 147
(4) D-D, alpha = 0.94, c = 7.0 42 3.23 47 812 2.99 60

Other Resources

Authors

References

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].