Skip to main content

Python bindings for Sketches implemented in Rust via PyO3

Project description

sketches: Probabilistic Data Structures, in Rust

Rust Python License

Fast, memory-efficient probabilistic data structures for streaming analytics, cardinality estimation, quantile computation, and sampling.

Python bindings for Rust-based implementations of HyperLogLog, T-Digest, Reservoir Sampling, and more via PyO3.

Algorithm Deep Dive

Features

Algorithm Category Implementation Description
Cardinality Estimation HyperLogLog (HLL) Industry-standard distinct counting with ~1% error
HyperLogLog++ Enhanced HLL with bias correction and sparse mode
CPC Sketch Most compact serialisation for network transfer
Linear Counter Optimal for small cardinalities (n < 1000)
Hybrid Counter Auto-transitions from Linear to HLL
Set Operations Theta Sketch Union, intersection, difference with cardinality estimation
Sampling Algorithm R Basic reservoir sampling for uniform random samples
Algorithm A Optimised reservoir sampling (Vitter, skips items probabilistically)
Weighted Sampling Probability-proportional reservoir sampling
Stream Sampling High-throughput sampling with batching
Quantile Estimation T-Digest Superior accuracy for extreme quantiles (p95, p99)
KLL Sketch Provable error bounds (~1.65% at k=200)
Frequency Estimation Count-Min Sketch Conservative frequency estimation with epsilon-delta guarantees
Count Sketch Unbiased frequency estimation using median
Frequent Items Top-K heavy hitters with Space-Saving algorithm
Membership Testing Bloom Filter Fast membership testing with configurable false positive rate
Counting Bloom Bloom filter with deletion support
Multi-dimensional Array of Doubles Tuple sketch for multi-dimensional aggregation

Mergeable means two independently built sketches can be combined into one that represents the union of both input streams, without access to the original data. This is essential for distributed systems where data is partitioned across nodes -- each node builds a local sketch, then all sketches are merged into a single result.

Install

The package is not yet published to PyPI (the reserved name is rusty-sketches); install from source for now.

git clone https://github.com/tallamjr/sketches.git
cd sketches
pip install .

For an editable install with development dependencies and the maturin build:

pip install -e .[dev]
maturin develop

Quickstart

from sketches import HllSketch

sketch = HllSketch(lg_k=12)
for item in ["apple", "banana", "orange", "apple"]:
    sketch.update(item)

print(f"Estimated unique items: {sketch.estimate():.2f}")

See the usage guide for every sketch with runnable examples.

Choosing the right sketch

Different problems call for different sketches. Use this guide to pick the right one for your use case.

Problem "How do I know if..." Small Scale Large Scale Distributed / Mergeable
Membership "Is X in the set?" BloomFilter CountingBloomFilter (if deletions needed) Yes, union via bitwise OR
Cardinality "How many unique items?" LinearCounter (n < 1000) HllSketch / HllPlusPlusSketch Yes, register-wise max
Cardinality + Set Ops "What's the overlap between A and B?" ThetaSketch ThetaSketch Yes, union, intersect, difference
Compact Cardinality "Unique count with minimal serialised size?" CpcSketch CpcSketch Yes, sketch merging
Frequency "What are the top-K items?" FrequentStringsSketch CountMinSketch / CountSketch Yes, entry-wise addition
Quantiles "What's the p99 latency?" KllSketch (provable bounds) TDigest (extreme quantile accuracy) Yes, digest merging
Sampling "Give me a random subset" ReservoirSamplerR ReservoirSamplerA (Vitter, skips items) Partial, merge samplers
Weighted Sampling "Sample proportional to weight" WeightedReservoirSampler VarOptSketch (Horvitz-Thompson) Yes, VarOpt merge
Multi-dimensional "Aggregate multiple metrics per key" AodSketch AodSketch Yes, summary merging

Key trade-offs:

  • HLL vs Theta: HLL is more memory-efficient for pure cardinality. Theta supports set operations (union, intersection, difference).
  • HLL vs CPC: CPC achieves ~40% smaller serialised size but is more complex. Use CPC when network transfer cost matters.
  • Count-Min vs Count Sketch: Count-Min always overestimates (conservative). Count Sketch is unbiased but uses more space.
  • KLL vs T-Digest: KLL has provable error bounds (~1.65% at k=200). T-Digest excels at extreme quantiles (p99, p99.9) but bounds are empirical.
  • Algorithm R vs A: Both produce uniform samples. Algorithm A skips items probabilistically (Vitter), which is much faster than Algorithm R for large streams.

Performance

Measured on a stabilised harness (the median over independent rounds with a 95% bootstrap confidence interval), our xxh3-backed default leads Apache DataSketches across the board. On real TPC-H string columns we beat hand-tuned Apache C++ on every one of the five shared sketches, and beat the Apache Rust crate on all five by wider margins. Accuracy stays at parity: HLL, Theta and CPC match Apache DataSketches within the run-to-run noise band by multi-trial RMSE, all at or below the 1/sqrt(k) floor.

Speedup over Apache on a real TPC-H column (the c_address field of a customer table, about 150k distinct strings, generated with tpchgen-rs), where every bar clears the parity line:

Speedup vs Apache, real TPC-H strings

The same lead in absolute time on that TPC-H column: a single HLL update takes roughly 7.7 ns with this library against 16.7 ns for Apache C++ (lower is better).

Latency per operation, real TPC-H strings

What we benchmark against. The references are the official Apache implementations built and run locally: apache/datasketches-cpp at master 3.2.0-858-g0bab259 (2026-06-19), built with cmake/g++ at C++11, and the official Apache Rust datasketches crate. Each runner emits one shared CSV schema over identical datasets, and the C++ runner has a startup self-check that aborts if its measurement scaffolding is miscompiled. Throughput is the median over independent rounds with a 95% bootstrap confidence interval; accuracy is multi-trial RMSE.

See docs/benchmarks.md for the full methodology, the reproduction steps, and the throughput, memory and accuracy plots.

Documentation

Roadmap

Planned but not yet implemented:

  • Similarity estimation: MinHash, SimHash, an LSH framework.
  • Performance: SIMD-accelerated register updates (the implementation is currently pure scalar Rust).
  • Integration: Polars custom expressions and DataFrame operations.

Current priorities: MinHash first (fills the similarity gap), then Polars expressions, then batch operations for the Python bindings.

License

This project is licensed under the MIT License (see pyproject.toml).

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

rusty_sketches-1.0.0.tar.gz (1.3 MB view details)

Uploaded Source

Built Distributions

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

rusty_sketches-1.0.0-cp313-cp313-win_amd64.whl (464.1 kB view details)

Uploaded CPython 3.13Windows x86-64

rusty_sketches-1.0.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (536.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

rusty_sketches-1.0.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (510.8 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

rusty_sketches-1.0.0-cp313-cp313-macosx_11_0_arm64.whl (488.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

rusty_sketches-1.0.0-cp313-cp313-macosx_10_12_x86_64.whl (508.4 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

rusty_sketches-1.0.0-cp312-cp312-win_amd64.whl (464.7 kB view details)

Uploaded CPython 3.12Windows x86-64

rusty_sketches-1.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (536.5 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

rusty_sketches-1.0.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (511.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

rusty_sketches-1.0.0-cp312-cp312-macosx_11_0_arm64.whl (488.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

rusty_sketches-1.0.0-cp312-cp312-macosx_10_12_x86_64.whl (508.8 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

rusty_sketches-1.0.0-cp311-cp311-win_amd64.whl (464.2 kB view details)

Uploaded CPython 3.11Windows x86-64

rusty_sketches-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (534.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

rusty_sketches-1.0.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (509.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

rusty_sketches-1.0.0-cp311-cp311-macosx_11_0_arm64.whl (492.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

rusty_sketches-1.0.0-cp311-cp311-macosx_10_12_x86_64.whl (511.6 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

File details

Details for the file rusty_sketches-1.0.0.tar.gz.

File metadata

  • Download URL: rusty_sketches-1.0.0.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rusty_sketches-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e4733b6a24669536bc4e7b48d93fbc29c0b4e6f4428fd348cb3f29688eadc5a4
MD5 94abe27befc4caf4baacc51087b566d5
BLAKE2b-256 10c0329a6aeeabc1a4e491389f3d9487ae077289ccda37ee2a297a24ec8f405c

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0.tar.gz:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 20d3d013aa1147b4125c990175b1bad3c870522979f1138c5162cbf340f2a063
MD5 6af2cccc66aa70badd982ba8340c0526
BLAKE2b-256 9dd49f4505b34519f4866abcede143e7c73c9c98f72fd4e4a0e4388f79cbaebe

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp313-cp313-win_amd64.whl:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a1b86ce65a29f1aa26947c5f0b082074a718d016be9664011197c2013e4a0fee
MD5 1de1471448692ecb79620a0dcdcaf8a4
BLAKE2b-256 adf951ab2b28dc9f4841818c3c7f733b8b3e4f16dcd5f284e2b649eaa06ed00f

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 61c4303e6998eed6264fdf7b7f333fd2aa588455d375db36575250df4d76888d
MD5 2d87b368b962c7a1bd899a5e31f4a6ae
BLAKE2b-256 cfec980a73f65ca35b5ad3e5d888282c60d54db71cdf83768c1fab6d0f497f96

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2b96e580ed23bc90948b697621b92e9f1458c2748ad7ae26d572c46ea7d37586
MD5 5bb56ec16ec6f1e50907d23afa4dcceb
BLAKE2b-256 dad9458cc104971e6a5e7349d574b8f7452efc5989baed74c09c07b95c1dd417

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8d210b08a1247e9cce1b7a37f198d794fc22fc6641cfbbc2ba0c03b6b9bb696f
MD5 2eacd64a32cb8dc5538eeaaf89662c06
BLAKE2b-256 8adbfa9b1095f5b09a9b3073057aa41bf2436ac96fa0069d9a38984898cd2dc7

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp313-cp313-macosx_10_12_x86_64.whl:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 b610f5a614030c6682a4a5554a9fbba65cf307fdf0c676d76afc3c24f5d75a8a
MD5 4676b86fa475a0b9a903225b878427d6
BLAKE2b-256 630f8ffe72aad5e142ccefc7753ab96f03e2f72b02e38ec1e4ffe1c7dcbd1cef

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp312-cp312-win_amd64.whl:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 577c2277e2b84758a0b597f3399a4ac3b9b2dfa6f4c142f9dbfe8db1f8d49a0f
MD5 04c7f41dff70f96bb2aceff3ec5a67e4
BLAKE2b-256 3029038b0cb074b9a266718544075a377414166a3d205e96c7503e5ee20db1f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 cc9daf0a18ad07cdcd199bf9e8cf487d639992d16fd9c8b2a16dd22fcd6447da
MD5 d0e9308f87d153435830718f14f3ae33
BLAKE2b-256 a8edfea112562939bd293220bd75ab812645de045666a95eb1a9f45975885459

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a7fa3773e498d179fec291685b03e4ff94da790c775d24d4b4d67dcf786bf7b9
MD5 afa50a8af680722c79bd32b7f3f547dd
BLAKE2b-256 daaf761801900c9c7a7d6e7fd3d839932b968ab21b89d2e06f60c52d5302eca2

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0305624fdcf5d6b0cb34ebe41605cb3730452405ffb342702d207daec2226f5b
MD5 51cfff82000b042720d09e053242572d
BLAKE2b-256 59c7f46b1589753176557cea0c698f733e5bff527eb265934aa1685b42bea02a

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp312-cp312-macosx_10_12_x86_64.whl:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8bc73839b2245555e30b025365d57ba42cc05c77f03eaf5302aac872be50cd28
MD5 175494515a32dab04d399828bd26100c
BLAKE2b-256 c679af2769fe4d5da28be3529257d48ffb674a049d481da16aa577615b5f2f2d

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp311-cp311-win_amd64.whl:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 822853493caeec317f2a5cd8d8ac68dba281d44508d032282c2fac371bb70030
MD5 be1632c4d9fff767ce1f70f043e5196a
BLAKE2b-256 e205812a3fcacd050c7a28466e8276943b27220f59c714c642e5676c10c2095b

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ffc1069c1e1bc9d1954d73f325504089d2cc1793a05ab4a061fdba6502f92980
MD5 196b4385fcab2bfc06f4828d24dbd72a
BLAKE2b-256 e91a427f2bf5224fb948fb4a64e8793f0b977128e372a87bbb437d4ccd75cd54

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3471d4cdda11052ed5bda80d8903843121f56ee4717337e41a7319e0dc859e2d
MD5 8a46e2e4f64070fadd65f4060687fbbf
BLAKE2b-256 e12cb91699cf5a163f14ba35fff0b44f0c4895a0b126aa35262efb6c47efa70e

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: publish.yml on tallamjr/sketches

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

File details

Details for the file rusty_sketches-1.0.0-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rusty_sketches-1.0.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 cecef0221800db41720a54b84cf02d339730de7740d98e869a1471e54a1d3a1f
MD5 f19cef171d864846cf8335c67236bfe9
BLAKE2b-256 42002c7a098b10b9beb9efab5b963c7af63567db7e35e8f1ca4655afb671c646

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusty_sketches-1.0.0-cp311-cp311-macosx_10_12_x86_64.whl:

Publisher: publish.yml on tallamjr/sketches

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