Skip to main content

rptrhash

Minimal perfect hash functions for Python, based on PtrHash (Groot Koerkamp, SEA 2025) and implemented in Rust via PyO3.

An MPHF maps a fixed set of n distinct keys one-to-one onto range(n), using about 3 bits per key (2.2 with compact=True), however long the keys are. It doesn't store the keys. You use it to put keys into a plain array (a static hash table) instead of a dict, which costs hundreds of bits per entry.

from rptrhash import MPHF

keys = ["apple", "banana", "cherry", 42, b"\x00raw"]
mphf = MPHF(keys)
mphf["cherry"]                 # -> an int in range(5); unique per key

# A static key -> value table in two flat arrays
values = [None] * len(mphf)
for k, v in zip(keys, ["red", "yellow", "dark red", "answer", "bytes"]):
    values[mphf[k]] = v
values[mphf["banana"]]         # 'yellow'

mphf.index_many(keys)          # array('Q', [...]), parallel, no GIL
mphf.save("fruit.mphf")        # or to_bytes() / pickle

Why

For static sets of millions to billions of keys, such as k-mer and n-gram indexes, URL and ID dictionaries, or read-only lookup tables, a minimal perfect hash plus flat arrays replaces a dict at 1–2% of the memory. Before this package, the only Python MPHF was bbhash, a Cython wrapper of the 2017 BBHash algorithm. It accepts only 64-bit integer keys and ships only as an sdist, so you need a C++ compiler to install it. PtrHash (2025) is the current state of the art for query throughput, and is also among the most compact.

What rptrhash adds on top of the Rust crate:

  • Any key type: int (up to 128 bits), str, bytes, and numpy or array integer buffers. Each key is pre-hashed with 128-bit XXH3, so accidental collisions are negligible even at billions of keys.
  • Optional fingerprints (fingerprint_bits=8/16/32) to detect keys that were not in the build set. A plain MPHF maps such keys to an arbitrary index.
  • Batch queries that run in parallel with the GIL released.
  • Persistence: save/load, to_bytes/from_bytes and pickle, in a versioned, checksummed format.
  • Prebuilt abi3 wheels (Linux/macOS/Windows, Python 3.11+).

Install

pip install rptrhash

Benchmarks

Distinct random 64-bit keys, 8 threads. The bbhash comparison uses gamma=1 (its most compact setting). Source: benchmarks/bench.py, raw output in benchmarks/results.txt.

n = 10M build bits/key (saved file) index(k) from Python batch (per key)
rptrhash (default) 2.64s 2.99 235 ns 11 ns
rptrhash compact=True 1.49s 2.17 226 ns 11 ns
bbhash 0.71s 3.15 211 ns 79 ns
dict (hash table only) 3.19s 268 133 ns –
n = 1M build bits/key index(k) batch
rptrhash (default) 0.18s 2.99 147 ns 5.9 ns
rptrhash compact=True 0.21s 2.17 142 ns 7.0 ns
bbhash 0.08s 3.07 150 ns 49 ns
rptrhash, 1M str keys 0.20s 2.99 128 ns 24 ns

Results:

  • Batch queries (index_many on a numpy array) are 7x faster than bbhash.lookup_many.
  • compact=True is 31% smaller than bbhash.
  • Single lookups from Python are about the same speed for every MPHF, because the cost of a Python method call dominates.
  • bbhash builds faster with many threads. The default PtrHash variant builds on a single thread; compact=True builds in parallel.
  • The dict row is a baseline for memory only; it doesn't count the key objects.

API

MPHF(keys, *, compact=False, fingerprint_bits=0)

  • keys is an iterable of distinct int, str, bytes or bytearray, or a 32/64-bit integer buffer (a numpy array or array.array), which is hashed in parallel. Duplicate keys raise ValueError. As with dict keys, True and 1 count as the same key, while "a", b"a" and 97 are three different keys.
  • compact=True gives about 2.2 bits/key and multi-threaded construction. Single queries are slightly slower in Rust, which doesn't matter for calls from Python.
  • fingerprint_bits can be 0, 8, 16 or 32. It stores that many bits per key, so non-members are rejected except with probability 2**-bits.
method returns
m.index(key) an int in range(len(m)). Unique for build keys; arbitrary for others (fingerprints are not checked).
m[key] like index, but raises KeyError if a fingerprint rules the key out
m.get(key, default=None) the index, or default if a fingerprint rules the key out
key in m probabilistic membership (needs fingerprints, otherwise raises TypeError). There are no false negatives.
m.index_many(keys) array('Q') of indices: parallel, GIL released. Use np.frombuffer(..., dtype=np.uint64) for a zero-copy numpy view.
m.get_many(keys, default=-1) array('q'), with default wherever a fingerprint rules a key out
len(m), m.variant, m.fingerprint_bits, m.bits_per_key, m.mphf_bits_per_key
m.to_bytes(), MPHF.from_bytes(b), m.save(path), MPHF.load(path), pickle serialization

The key hashing and the file format are versioned and stable within a format version. The default variant is deterministic: the same keys always give the same indices. A compact=True build is multi-threaded and may assign different (equally valid) indices each time you build it, so save it rather than rebuilding when indices must stay stable.

How it works

  1. Each key is hashed to 128 bits with XXH3. There is a separate seed for each key kind (int, str, bytes), and ints are hashed as i128, so Python ints and integer buffers agree.
  2. PtrHash runs over those hashes:
    • keys are split into buckets of about 3 keys on average;
    • each bucket gets an 8-bit pilot, chosen so that its keys land in free slots of a table about 1% larger than n, evicting other buckets when needed (as in cuckoo hashing);
    • the slots past n are remapped to the free slots below n, using Vec<u32> storage or, with compact=True, Elias-Fano encoding.
  3. A query is one hash, one pilot lookup, and a multiply. Batch queries prefetch 32 keys ahead and run across rayon threads.

Development

cargo test --no-default-features
pip install -e '.[dev]' && maturin develop --release
pytest
pip install bbhash numpy && python benchmarks/bench.py

License

MIT. Wraps the MIT-licensed ptr_hash crate by Ragnar Groot Koerkamp.

Metadata

Release files for rptrhash 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rptrhash 0.1.0
File Size Uploaded Provenance Actions
rptrhash-0.1.0.tar.gz 34.0 kB With provenance Details for rptrhash-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for rptrhash 0.1.0
File Actions
rptrhash-0.1.0-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for rptrhash-0.1.0-cp311-abi3-win_amd64.whl
rptrhash-0.1.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.11 abi3 Linux glibc 2.17+ x86-64 Details for rptrhash-0.1.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
rptrhash-0.1.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details for rptrhash-0.1.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
rptrhash-0.1.0-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for rptrhash-0.1.0-cp311-abi3-macosx_11_0_arm64.whl
rptrhash-0.1.0-cp311-abi3-macosx_10_12_x86_64.whl CPython 3.11 abi3 macOS 10.12+ x86-64 Details for rptrhash-0.1.0-cp311-abi3-macosx_10_12_x86_64.whl

Total release size: 2.5 MB

Release files / rptrhash-0.1.0.tar.gz

Download URL rptrhash-0.1.0.tar.gz
Size 34.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2d83bddc3130883dc5cf33df949c176a97f183723a6562e3a17ad1e0a3558426
BLAKE2b-256 checksum
How to use checksums
fc61b07673d6db7205f9d214187413250d8d9c2ef09224c9c0325d6395f1d768
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / rptrhash-0.1.0-cp311-abi3-win_amd64.whl

Download URL rptrhash-0.1.0-cp311-abi3-win_amd64.whl
Size 430.9 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
d53935db93cb1bd374be72225142d9238630d659d93f313f6429fa65ea6aae5c
BLAKE2b-256 checksum
How to use checksums
da05cee3da71c211b69099c266f20a5341d878a7379f3a7359f862b16f96bb83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / rptrhash-0.1.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL rptrhash-0.1.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 537.7 kB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
2be864fa6fc9eb3261488bbf00121e7261764c74bc250f4b71117345e8a48710
BLAKE2b-256 checksum
How to use checksums
36e3a92b7cd9bfca07e486eb2f79b4c3e0556c260b815dd7ff5a46e06e4168c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / rptrhash-0.1.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL rptrhash-0.1.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 531.0 kB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
6f3d8adad23ba699ffbe59c3e6dff69f9322163f9a4d6b919f72eae213aa0b5f
BLAKE2b-256 checksum
How to use checksums
34b888a42805530fcef7b4354bf099d1f998916aa8ac5f62a3012bed56610e5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / rptrhash-0.1.0-cp311-abi3-macosx_11_0_arm64.whl

Download URL rptrhash-0.1.0-cp311-abi3-macosx_11_0_arm64.whl
Size 492.5 kB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6c89f0a3002b478728a62d84295650a0acca294165b929eca42e796161e12363
BLAKE2b-256 checksum
How to use checksums
3cc06fe039a5159db2c8adf9f0df85e505364950fa8cbbc2d3ab577050d5b953
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / rptrhash-0.1.0-cp311-abi3-macosx_10_12_x86_64.whl

Download URL rptrhash-0.1.0-cp311-abi3-macosx_10_12_x86_64.whl
Size 503.8 kB
Tags CPython 3.11 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
b89a6551a4c16b970bab2da57f06c6dea45b96fa3c50997f9bc110b40bb7d6eb
BLAKE2b-256 checksum
How to use checksums
e7701ca799c9631eda1c29b72bd2ec41addefb8be7dc37ac94010ff176f94f9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Security for rptrhash 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for rptrhash-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

6 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page