Skip to main content

Fast probabilistic data structures (Bloom, Cuckoo, HyperLogLog, Count-Min) backed by a native Zig core

Project description

zpds

Fast probabilistic data structures for Python — Bloom filter, Cuckoo filter, HyperLogLog, and Count-Min Sketch — powered by a native Zig core.

It targets high-throughput streaming workloads: deduplication, cardinality counting, and frequency tracking over millions of items. On an ordinary laptop it inserts ~90M items/s into a Bloom filter from a NumPy array — ~100× a pure-Python implementation (benchmarks).

  • Bloom filter — membership, tunable false-positive rate.
  • Cuckoo filter — membership with deletion support.
  • HyperLogLog — cardinality estimation.
  • Count-Min Sketch — frequency estimation.

Install

pip install zpds          # prebuilt wheel, no Zig toolchain needed
pip install zpds[numpy]   # add the zero-copy NumPy batch fast path

Wheels for Linux, macOS, and Windows; any Python ≥ 3.9, no compiler needed.

Quick start

from zpds import BloomFilter, CuckooFilter, HyperLogLog, CountMinSketch

bf = BloomFilter(capacity=1_000_000, error_rate=0.01)
bf.add("alice")
"alice" in bf                       # True

cf = CuckooFilter(capacity=1_000_000)
cf.add("bob")
cf.remove("bob")                    # deletion, unlike a Bloom filter

hll = HyperLogLog(precision=14)
hll.add_many(f"user-{i}" for i in range(1_000_000))
len(hll)                            # ≈ 1_000_000, ~0.8% error

cms = CountMinSketch(epsilon=0.001, delta=0.001)
cms.add("apple", 3)
cms.estimate("apple")               # ≥ 3, never an underestimate

Each object owns a native buffer freed on garbage collection; use it as a context manager (with BloomFilter(...) as bf:) or call .close() for deterministic cleanup. The mergeable sketches copy and combine as values:

merged = hll_a | hll_b              # union (HyperLogLog); hll_a |= hll_b in place
totals = cms_a + cms_b             # counter-wise sum (Count-Min); cms_a += cms_b
snapshot = bf.copy()               # independent copy of any structure

Batch & NumPy API

Every structure has *_many methods that cross the Python↔native boundary once per batch instead of once per item. A contiguous fixed-width NumPy array is passed zero-copy — this is where the ~100× speedups come from.

import numpy as np

bf = BloomFilter(capacity=1_000_000)
bf.add_many(np.arange(1_000_000, dtype=np.uint64))              # zero-copy
mask = bf.contains_many(np.array([1, 2, 3], dtype=np.uint64))   # bool array

bf.add_many(open("keys.txt"))       # any iterable streams in bounded memory
bf.add_many(stream, batch_size=50_000)   # tune the per-crossing chunk size

cf = CuckooFilter(capacity=1_000)
failed = cf.add_many(keys, return_failed=True)   # indices that didn't fit

cms = CountMinSketch(epsilon=1e-3, delta=1e-3)
cms.add_many(words, counts)         # optional per-item counts
freqs = cms.estimate_many(words)    # list[int] or np.uint64 array

For non-numeric NumPy dtypes (S/V), the full fixed-width field is hashed (including NUL padding), so add and query with the same representation.

Performance

Two levers compound: the native core, and the batch APIs that cross the FFI boundary once per workload instead of once per item. A contiguous NumPy array takes the zero-copy path — its buffer goes straight to Zig with no per-item Python work.

Workload pure-Python zpds single-item zpds batch (NumPy)
Bloom — add 0.83 M ops/s 4.5 M ops/s (5×) 89 M ops/s (108×)
Bloom — query 0.87 M ops/s 4.0 M ops/s (5×) 101 M ops/s (117×)
HyperLogLog — add 1.5 M ops/s 4.5 M ops/s (3×) 255 M ops/s (167×)
Count-Min — add 1.0 M ops/s 4.2 M ops/s (4×) 95 M ops/s (92×)

Speedups in parentheses are relative to the pure-Python baseline. Apple Silicon, CPython 3.12, ReleaseSafe, 200k items — see benchmarks for the harness and full tables (including the add_many(list) middle path).

Building from source

Requires Zig 0.16.0 or newer.

zig build          # builds zig-out/lib/libzpds.{dylib,so,a}
zig build test     # runs the Zig unit + statistical test suite

To build a wheel from source (compiles the Zig core and bundles it):

pip install build ziglang==0.16.0
python -m build --wheel

Development

The Zig core lives in zig/ (C ABI in zig/ffi.zig, header in zig/include/zpds.h), the Python package in python/zpds/, tests in tests/, and benchmarks in benchmarks/. The bindings load a prebuilt libzpds shared library via cffi in ABI (dlopen) mode — one py3-none-<platform> wheel per platform serves every supported Python 3.

zig build          # produces zig-out/lib/libzpds.{so,dylib}
pytest             # loads the library from zig-out/lib (dev fallback)

Set ZPDS_LIBRARY_PATH to point the bindings at a specific shared library.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zpds-0.0.1.tar.gz (40.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

zpds-0.0.1-py3-none-win_amd64.whl (296.5 kB view details)

Uploaded Python 3Windows x86-64

zpds-0.0.1-py3-none-musllinux_1_2_x86_64.whl (995.4 kB view details)

Uploaded Python 3musllinux: musl 1.2+ x86-64

zpds-0.0.1-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (995.5 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64manylinux: glibc 2.5+ x86-64

zpds-0.0.1-py3-none-macosx_13_0_x86_64.whl (226.3 kB view details)

Uploaded Python 3macOS 13.0+ x86-64

zpds-0.0.1-py3-none-macosx_13_0_arm64.whl (202.5 kB view details)

Uploaded Python 3macOS 13.0+ ARM64

File details

Details for the file zpds-0.0.1.tar.gz.

File metadata

  • Download URL: zpds-0.0.1.tar.gz
  • Upload date:
  • Size: 40.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for zpds-0.0.1.tar.gz
Algorithm Hash digest
SHA256 b1b9ab9b97040aa6df905680e0ba282533e3cef5ea529e5f6b93d536ed0ac52c
MD5 82611ef028fe6cb3b1e96a6717439a51
BLAKE2b-256 9b9471a11215f10d2e9737aad5644603d7b5fda5d454ffc3609d0752ead52591

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpds-0.0.1.tar.gz:

Publisher: wheels.yml on gustavoschmidt/zpds

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zpds-0.0.1-py3-none-win_amd64.whl.

File metadata

  • Download URL: zpds-0.0.1-py3-none-win_amd64.whl
  • Upload date:
  • Size: 296.5 kB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for zpds-0.0.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 b2764d2365833b6682898898b583b9aa6f80dba87dbe7147d9f148bb16e83dc6
MD5 f609be4077fcf0a77ac35a1fb09513ed
BLAKE2b-256 38646919e94dd725513b5e06c89c897416dc585d03d4aa47c76191712b3a9b62

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpds-0.0.1-py3-none-win_amd64.whl:

Publisher: wheels.yml on gustavoschmidt/zpds

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zpds-0.0.1-py3-none-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: zpds-0.0.1-py3-none-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 995.4 kB
  • Tags: Python 3, musllinux: musl 1.2+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for zpds-0.0.1-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7765764dbe34677b09ab154a75703d86b900002398ff02d2eefcbf459895d65c
MD5 0f514e9cbf591d4d779a4583ee275744
BLAKE2b-256 60ab47e497c0f121db3cba85f3bb6d86d2dfd6d8d6759616f05a5bfa21cce0c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpds-0.0.1-py3-none-musllinux_1_2_x86_64.whl:

Publisher: wheels.yml on gustavoschmidt/zpds

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zpds-0.0.1-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zpds-0.0.1-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 389a36d0f397cb759aaf066993c13e771f31bc7f17817cbad9bf8b909b8091ca
MD5 b070a937eb6a9b19d901bf1c642b4bd5
BLAKE2b-256 bb9672afb691b1b0d5ba9a3e68d644b79e74bf78e470d082d6bc2484ec77ca19

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpds-0.0.1-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: wheels.yml on gustavoschmidt/zpds

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zpds-0.0.1-py3-none-macosx_13_0_x86_64.whl.

File metadata

  • Download URL: zpds-0.0.1-py3-none-macosx_13_0_x86_64.whl
  • Upload date:
  • Size: 226.3 kB
  • Tags: Python 3, macOS 13.0+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for zpds-0.0.1-py3-none-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 ccaf0a0e399355d82bc4c26bc1e414210a919e24a1d9a3b135e8ffe74292073c
MD5 6a8eaa6c1f6cc38997d3e2a1582285e0
BLAKE2b-256 00a9621d518de81f7a08be95f949e734bc973dbde9acb9012cce5a0530377c14

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpds-0.0.1-py3-none-macosx_13_0_x86_64.whl:

Publisher: wheels.yml on gustavoschmidt/zpds

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zpds-0.0.1-py3-none-macosx_13_0_arm64.whl.

File metadata

  • Download URL: zpds-0.0.1-py3-none-macosx_13_0_arm64.whl
  • Upload date:
  • Size: 202.5 kB
  • Tags: Python 3, macOS 13.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for zpds-0.0.1-py3-none-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 8606538d2fbddb5bfaf7c78d4abeb13d0a57342e86cbf4dac4d46464b3f5332b
MD5 9747c744d7ffbf02f7c67d049643f916
BLAKE2b-256 19c6dc0df0e8937a064a0adbf8f46a22205646f4cea4b667f16697910d4ac711

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpds-0.0.1-py3-none-macosx_13_0_arm64.whl:

Publisher: wheels.yml on gustavoschmidt/zpds

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page