Skip to main content

High-performance, memory-safe Python bindings for zlib-rs (Rust). Up to 22x faster than CPython zlib.

Project description

Notice: This project was built entirely with AI assistance and is an experimental proof of concept. It is not intended for production use. Use at your own risk.

zlib-rs-python

CI PyPI Python Versions License: MIT

A high-performance, memory-safe Python binding for the zlib-rs Rust crate. Up to 22x faster than CPython's built-in zlib module.

Features

  • High Performance -- Up to 22x faster compression and 17x faster checksums compared to CPython's built-in zlib (see benchmarks).
  • Memory Safe -- Built on top of zlib-rs, a memory-safe, pure-Rust implementation of the zlib compression algorithm.
  • Drop-in Compatible -- Follows the standard Python zlib API. Swap imports and go.
  • Cross-Platform -- Builds and runs on Linux, macOS, and Windows with no C dependencies.
  • Zero C Dependencies -- No system zlib headers or C compiler required. Everything is compiled from Rust source.

Installation

From PyPI

pip install zlib-rs

From Source

pip install maturin
git clone https://github.com/farhaanaliii/zlib-rs-python.git
cd zlib-rs-python
maturin develop --release

Quick Start

import zlib_rs as zlib

# One-shot compression
data = b"Hello, zlib-rs!" * 1000
compressed = zlib.compress(data)
original = zlib.decompress(compressed)
assert data == original

# Streaming compression
compressor = zlib.compressobj(level=6)
compressed = compressor.compress(data)
compressed += compressor.flush()

decompressor = zlib.decompressobj()
result = decompressor.decompress(compressed)
assert data == result

# Checksums
checksum_adler = zlib.adler32(data)
checksum_crc = zlib.crc32(data)

Drop-in Replacement

To use zlib-rs globally in an existing project without changing every import:

import sys
import zlib_rs

sys.modules["zlib"] = zlib_rs

# Now any library importing 'zlib' will use 'zlib-rs' instead
import zlib
compressed = zlib.compress(b"transparent replacement")

API Reference

Function / Class Description
compress(data, level=-1) One-shot compression. Returns compressed bytes.
decompress(data, wbits=15, bufsize=16384) One-shot decompression. Returns original bytes.
compressobj(level, method, wbits, ...) Create a streaming compression object.
decompressobj(wbits, zdict) Create a streaming decompression object.
adler32(data, value=1) Compute Adler-32 checksum.
crc32(data, value=0) Compute CRC-32 checksum.

All standard zlib constants are available: Z_BEST_COMPRESSION, Z_BEST_SPEED, Z_DEFAULT_COMPRESSION, Z_DEFAULT_STRATEGY, Z_DEFLATED, Z_FINISH, Z_NO_FLUSH, Z_SYNC_FLUSH, MAX_WBITS, DEF_MEM_LEVEL, etc.

Benchmarks

All benchmarks measured on Windows, Python 3.12, with release optimizations (lto = "fat", codegen-units = 1, target-cpu=native).

One-Shot Compression

Data Size Level CPython zlib zlib-rs Speedup
1 KB 1 57.2 us 46.3 us 1.2x faster
1 KB 6 62.6 us 20.0 us 3.1x faster
64 KB 1 155.6 us 63.2 us 2.5x faster
64 KB 6 398.1 us 86.3 us 4.6x faster
1 MB 6 6.45 ms 401.4 us 16.1x faster
10 MB 6 94.60 ms 4.22 ms 22.4x faster

One-Shot Decompression

Data Size Level CPython zlib zlib-rs Speedup
1 KB 1 4.4 us 2.3 us 1.9x faster
64 KB 9 50.5 us 15.0 us 3.4x faster
1 MB 6 1.18 ms 664.0 us 1.8x faster
10 MB 1 11.99 ms 5.86 ms 2.0x faster

Checksums

Operation Data Size CPython zlib zlib-rs Speedup
adler32 64 KB 21.0 us 2.0 us 10.5x faster
crc32 64 KB 41.9 us 3.2 us 13.1x faster
adler32 1 MB 364.6 us 33.1 us 11.0x faster
crc32 1 MB 814.8 us 48.3 us 16.9x faster

Running Benchmarks

maturin develop --release
python benchmarks/bench_zlib.py

Development

Prerequisites

Setup

git clone https://github.com/farhaanaliii/zlib-rs-python.git
cd zlib-rs-python
python -m venv .venv

# Linux / macOS
source .venv/bin/activate

# Windows (PowerShell)
.\.venv\Scripts\Activate.ps1

pip install maturin pytest

Build

Debug build (fast compile, slow runtime):

maturin develop

Release build (slow compile, fast runtime -- recommended for benchmarks):

maturin develop --release

Test

pytest tests/ -v

Dev Scripts

Platform-specific dev scripts are provided in the scripts/ directory:

Script Description
scripts/dev.ps1 Windows: build, test, and benchmark
scripts/dev.sh Linux/macOS: build, test, and benchmark
scripts/release.ps1 Windows: bump version, tag, and push release
scripts/release.sh Linux/macOS: bump version, tag, and push release

Usage:

# Windows (PowerShell)
.\scripts\dev.ps1 build     # Build release
.\scripts\dev.ps1 test      # Run tests
.\scripts\dev.ps1 bench     # Run benchmarks
.\scripts\dev.ps1 all       # Build + test + bench

# Linux / macOS
./scripts/dev.sh build
./scripts/dev.sh test
./scripts/dev.sh bench
./scripts/dev.sh all

Project Structure

zlib-rs-python/
  src/
    lib.rs             # Rust source -- PyO3 bindings to zlib-rs
  python/
    zlib_rs/
      __init__.py      # Python package entry point
  tests/
    test_compatibility.py  # Basic cross-compat tests
    test_compress.py       # One-shot compress/decompress
    test_streaming.py      # Streaming operations
    test_checksums.py      # adler32/crc32
    test_edge_cases.py     # Edge cases, constants, errors
  benchmarks/
    bench_zlib.py      # Performance comparison vs CPython zlib
  scripts/
    dev.ps1            # Windows dev script
    dev.sh             # Linux/macOS dev script
    release.ps1        # Windows release script
    release.sh         # Linux/macOS release script
  .github/
    workflows/
      CI.yml           # Test + lint on push/PR
      release.yml      # Cross-platform build + PyPI publish on tag
  Cargo.toml           # Rust dependencies and release profile
  pyproject.toml       # Python package metadata (PyPI)
  LICENSE
  README.md

Releasing

Releases are automated via GitHub Actions. To create a new release:

# Windows
.\scripts\release.ps1 0.2.0

# Linux / macOS
./scripts/release.sh 0.2.0

This will:

  1. Update version in Cargo.toml and __init__.py
  2. Build and test locally
  3. Commit, create a v0.2.0 tag, and push to GitHub
  4. GitHub Actions then builds cross-platform wheels (Linux x86_64/aarch64, Windows x86_64, macOS x86_64/arm64)
  5. Tests wheels on all platforms
  6. Publishes to PyPI
  7. Creates a GitHub Release with all wheel assets

Contributing

Contributions are welcome. Please see CONTRIBUTING.md for guidelines.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgements

  • zlib-rs -- The underlying Rust zlib implementation by the Trifecta Tech Foundation.
  • PyO3 -- Rust bindings for Python.
  • maturin -- Build system for Rust-based Python packages.

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

zlib_rs-0.1.1.tar.gz (22.1 kB view details)

Uploaded Source

Built Distributions

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

zlib_rs-0.1.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (293.8 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

zlib_rs-0.1.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (292.6 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

zlib_rs-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (292.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

zlib_rs-0.1.1-cp312-cp312-win_amd64.whl (199.2 kB view details)

Uploaded CPython 3.12Windows x86-64

zlib_rs-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (292.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

zlib_rs-0.1.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (278.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

zlib_rs-0.1.1-cp312-cp312-macosx_11_0_arm64.whl (252.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

zlib_rs-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl (280.2 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

zlib_rs-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (293.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

zlib_rs-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (293.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

zlib_rs-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (295.4 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

zlib_rs-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (295.5 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file zlib_rs-0.1.1.tar.gz.

File metadata

  • Download URL: zlib_rs-0.1.1.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for zlib_rs-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8ed0a4ab9fb349ec081273cabdbe28086d71e015c12ded8bbd3096affc1cd952
MD5 4bb92c373bd68d30ae5a6cf521efbe88
BLAKE2b-256 a8c57ffd3e282e70bd64505280d57eae265155ee314d477e657b74e4b0c8c966

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.1.tar.gz:

Publisher: release.yml on farhaanaliii/zlib-rs-python

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

File details

Details for the file zlib_rs-0.1.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 06eeb162c7bbb59f80a49a55bdf74999b2c19c7b45e277b117671f55fc891c62
MD5 e3095494733bfc0605461689afef460a
BLAKE2b-256 ab5d45b3bf4b1d3fd3f6c699feb37ae16f6d25c9f1fe76b598e874d565319b84

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on farhaanaliii/zlib-rs-python

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

File details

Details for the file zlib_rs-0.1.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ea81053af44870acd0f6c5db5c6e53556fb610f77c77f4ad68df0a682f53f16e
MD5 30b3fb7b8e549f283ef3a6890bb60718
BLAKE2b-256 50025acc12168deb309bb131adea096f428aeeed22a9d166da6cadbec88231f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on farhaanaliii/zlib-rs-python

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

File details

Details for the file zlib_rs-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ac2b3f9266aec4b4a43413d6a231a508190537c366d967a8a76515322539e8b2
MD5 4c108ef2f43774b9c3d64dab56701b35
BLAKE2b-256 284c228a15cecff34f4921b27d203e94388ede8be3d0700a4e7175ec5871efd7

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on farhaanaliii/zlib-rs-python

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

File details

Details for the file zlib_rs-0.1.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: zlib_rs-0.1.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 199.2 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for zlib_rs-0.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ac5d75b44895590f41d89607f36c997aed06dfbac8806e944512e06fa8c1bc13
MD5 e4d86d49f7f371a836d83747f196e2d5
BLAKE2b-256 3dc36dc44fee54fba6811722564ea20451e0d0b207199d6c10e346d9d7019fcd

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.1-cp312-cp312-win_amd64.whl:

Publisher: release.yml on farhaanaliii/zlib-rs-python

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

File details

Details for the file zlib_rs-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ec156ce0e7ff12fd08744f2dc3ac3819df78129cc1f930cdb7ed9021b225ac32
MD5 1fecfba9fa237fdb46d7c87080df60cf
BLAKE2b-256 7549a78be8c21b6498b4fa9f04041cdd45025584d29e6f023540f3f506b4ee01

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on farhaanaliii/zlib-rs-python

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

File details

Details for the file zlib_rs-0.1.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 496004a93cb1c3516a4a5fd5f5f1e770dfe8e527c8840fecc8118ab815b64ddd
MD5 3a1f2a0b4b8afd4f5a943d250bf12b8e
BLAKE2b-256 ab76dc1cf91dfc9a918b4cfe4c7157efc23858888d370c6f06f132e01120b404

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on farhaanaliii/zlib-rs-python

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

File details

Details for the file zlib_rs-0.1.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e14e1635ef5f140378fc2abe8971d7561b19b5d8ecfa61365a89266a368c5b8c
MD5 624315f81eb01458070d63416a6294da
BLAKE2b-256 edc0443b394cc85d475269a1cbc78afe3c56687433f660af509b2e4fec81f708

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.1-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: release.yml on farhaanaliii/zlib-rs-python

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

File details

Details for the file zlib_rs-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f08d0ffd990385ba95500b4cf97a47fe26242dd062035858b800ae350ca2a8bc
MD5 e1e2b0d062a617f1a8d868e9174dd88b
BLAKE2b-256 f71de9e98ba0a8276c5cfebdc5aac76a7b44f51de2a07c6820f0d90b4d35fba1

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl:

Publisher: release.yml on farhaanaliii/zlib-rs-python

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

File details

Details for the file zlib_rs-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0d05f63a574dc5a220a0b87b8a515497c688196b4fcbf0a175126f5ad85401c4
MD5 617b0c5e0568e63091273f22f9749664
BLAKE2b-256 21ea86df7962dc88c6782433b61300a12ebe1a86d77d95c2a12f7b9b6b7ebc00

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on farhaanaliii/zlib-rs-python

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

File details

Details for the file zlib_rs-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 736b06443ccf070e0922456008b95ffba0cf78a901b5e0246dec96e7d53b92d0
MD5 23160966a8fef6373796da7d0c2c1c95
BLAKE2b-256 7bcf36c23a72a3dfd06d29b5f2f48d4c81328268779b3a58faf613bd8096c1a0

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on farhaanaliii/zlib-rs-python

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

File details

Details for the file zlib_rs-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7eb7e6a3b6171d3d08a29278fb40e798dd8b853b4c1d90d6a18db21127c6e5e6
MD5 4375783b5997b48caaed1fc587cbfee9
BLAKE2b-256 94693adf961c543845597b11062786bc4bf5814841b40626bd26e37545dbb11c

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on farhaanaliii/zlib-rs-python

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

File details

Details for the file zlib_rs-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4ff77987eed1c85118e0c63c2878b20445bf5b92ea735301cfbbe2a8e41fa168
MD5 8912ff2b0f3c527572f2e49839e59638
BLAKE2b-256 44a9ccc797d05738b4dfbbccc8106cb6e17c8be0d8220852dc49070e73874420

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on farhaanaliii/zlib-rs-python

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