kh57
Efficient range reservoir sampling from massive sorted key-value datasets. Deterministic, stable, range-friendly, low read amplification. Cython-accelerated hot inner loop (siphash + encoding) with a small Python surface.
Credits: the algorithm was designed by Karen Hambardzumyan (mahnerak) in 2023.
The problem
You have a huge sorted-by-integer-key dataset, potentially trillions of items on disk. You want n uniformly-sampled (key, value) pairs from a sub-range [begin, end) without pulling the whole thing. Naive every-Nth sampling is biased. Random shuffling on disk kills range access. You want both: uniformity and efficient range reads.
kh57 gives you:
- deterministic - same keys, same salt, same sample.
- stable - appending new keys outside the queried range does not change which keys inside get sampled.
- range-friendly - scan only what the query needs.
- low read amplification - total reads stay within ~2x of
n.
How it works
Each key is hashed with SipHash-2-4 (uniform 64-bit output). The bit_length of that hash becomes the key's "level": level 63 holds roughly half the keys, level 62 a quarter, and so on down. Every item is stored under a compound sort key (level << 57) | key - top 7 bits are the level id, bottom 57 bits are the original key. This preserves original key order within a level, and level order across levels.
To sample from [begin, end): walk levels from sparsest to densest, range_scan each level's slice, take full levels while they fit the quota, reservoir-sample the boundary level for the remainder, stop. Each level is a deterministic uniform subset of the range, so the union is a uniform sample.
Install
pip install kh57
Requires Python 3.12+.
Usage
from kh57 import kh57, sample, MemBackend
backend = MemBackend()
for key in range(1_000_000):
encoded = kh57(key).to_bytes(8, "big")
backend.put(encoded, str(key).encode())
# 500 uniform samples from the [100_000, 200_000) range
result = sample(backend, 500, begin=100_000, end=200_000)
Any sorted-by-bytes key-value store can be a backend - just implement the Backend protocol (get, put, delete, range_scan). MemBackend is the reference in-memory adapter; RocksDB / LMDB adapters can be added out-of-tree.
Public API
kh57(key: int) -> int- encode a 57-bit non-negative key into a 64-bit sort key.recover(h: int) -> tuple[int, int]- inverse, returns(level, key).uniform_hash(key: int) -> int- SipHash-2-4 with the default salt.sample(backend, n, begin=None, end=None, *, rng=None) -> list[tuple[int, bytes]].Backend- Protocol.MemBackend- in-memory reference implementation.
Dev
make install # create venv + install with dev,test extras
make build # build cython extensions in-place
make test # run tests
make lint # ruff check
make format # ruff format + fix
make wheels # build manylinux wheels via docker
License
Apache-2.0
Metadata
Release files for kh57 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| kh57-0.1.3.tar.gz | 20.3 kB | Details |
Built distributions (wheels)
Total release size: 4.7 MB
Release files / kh57-0.1.3.tar.gz
| Download URL | kh57-0.1.3.tar.gz |
|---|---|
| Size | 20.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8048541742ec52ad629d619896986572d30315505750addba6228395dafb8b8b
|
|
BLAKE2b-256 checksum How to use checksums |
e291a4c28e9d1ab0702054837045954d27a11a91edf66fad19c77326994e6f43
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 6, 2026.
Transparency logRelease files / kh57-0.1.3-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
| Download URL | kh57-0.1.3-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 1.2 MB |
| Tags | CPython 3.14 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
0c43ea3b2a01c5019b6c9a9bf3237f9583ecfb30ff755c5646278659253ec0c7
|
|
BLAKE2b-256 checksum How to use checksums |
1df8883306cd747edf223453d075530f0b0ffe9df2b946f83b9741f2b1ed33fc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 6, 2026.
Transparency logRelease files / kh57-0.1.3-cp314-cp314-macosx_11_0_arm64.whl
| Download URL | kh57-0.1.3-cp314-cp314-macosx_11_0_arm64.whl |
|---|---|
| Size | 190.6 kB |
| Tags | CPython 3.14 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
bf6b4650136d442f39d432260e4d45fb8ae9ce71a147c2832e7b0e7513ee62d6
|
|
BLAKE2b-256 checksum How to use checksums |
d7b225cf382b21d6df78f3ab7dcf3dbff9019c6d4715a8628469c7c37a9c1d5f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 6, 2026.
Transparency logRelease files / kh57-0.1.3-cp314-cp314-macosx_10_15_x86_64.whl
| Download URL | kh57-0.1.3-cp314-cp314-macosx_10_15_x86_64.whl |
|---|---|
| Size | 192.9 kB |
| Tags | CPython 3.14 macOS 10.15+ x86-64 |
|
SHA-256 checksum How to use checksums |
67aea540a1410744f81f939a88f4434e5d0b16bc4f629074ebc946ef59e31bff
|
|
BLAKE2b-256 checksum How to use checksums |
c83752ff5c63dd442662711fdbe1d5e15a2a36f3f93d4ff17147a98cf199a913
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 6, 2026.
Transparency logRelease files / kh57-0.1.3-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
| Download URL | kh57-0.1.3-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 1.2 MB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
6287c1b31b61eb9e276776cd6edaacf3cad2030b66212eb1c02180d8b809419e
|
|
BLAKE2b-256 checksum How to use checksums |
7ebe0b32acbd41c7d5601267b2464fca9ccb32c5dcb96919bd7a7f43d849218f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 6, 2026.
Transparency logRelease files / kh57-0.1.3-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | kh57-0.1.3-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 188.9 kB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
f08ae461b464a6bf3d3ba5b11146e8076ca186de2579e6446c4c8392811c7920
|
|
BLAKE2b-256 checksum How to use checksums |
53df8fa8eec738e254cf8c02e66b1e66a67abb42d7cc1188d40fd35114f276b0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 6, 2026.
Transparency logRelease files / kh57-0.1.3-cp313-cp313-macosx_10_14_x86_64.whl
| Download URL | kh57-0.1.3-cp313-cp313-macosx_10_14_x86_64.whl |
|---|---|
| Size | 192.3 kB |
| Tags | CPython 3.13 macOS 10.14+ x86-64 |
|
SHA-256 checksum How to use checksums |
a655e0ae39023ff251dbfccd8ec6da6d92af69cbdf8efd5ba01cc3f4c5c22ddb
|
|
BLAKE2b-256 checksum How to use checksums |
5f9f28ffcfe449e58e6178dd19afab653c734cec16d8c1a26b4767fccfa289bb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 6, 2026.
Transparency logRelease files / kh57-0.1.3-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
| Download URL | kh57-0.1.3-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 1.2 MB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
b880c0e30fed30d72da4e3072141313dd10d9e69014d57101cdaacf9dd28c2f7
|
|
BLAKE2b-256 checksum How to use checksums |
8d982f866105580075c0cadd2a6dff4a57a54f209963a1cdfc96120bec89d175
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 6, 2026.
Transparency logRelease files / kh57-0.1.3-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | kh57-0.1.3-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 190.8 kB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
d1f1b23b1fb6f3dee71604082b052823e54a9d07f6c47e88ea513fbf34bd8ed4
|
|
BLAKE2b-256 checksum How to use checksums |
53aa3be202961f05660782a3da30f7728f03582c870f1217a0782459d9ca4352
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 6, 2026.
Transparency logRelease files / kh57-0.1.3-cp312-cp312-macosx_10_14_x86_64.whl
| Download URL | kh57-0.1.3-cp312-cp312-macosx_10_14_x86_64.whl |
|---|---|
| Size | 194.2 kB |
| Tags | CPython 3.12 macOS 10.14+ x86-64 |
|
SHA-256 checksum How to use checksums |
34f14e07e8a2f6eb9a96c2e6a9b3fe7ab67e8ef90611cf5652f0d21e60f964a7
|
|
BLAKE2b-256 checksum How to use checksums |
eca52fc5ac7513467ec3ae192da7017681b84c1ba7fa1f76e0f9c692ab541a85
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 6, 2026.
Transparency log