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.2.tar.gz (22.8 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.2-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (296.2 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

zlib_rs-0.1.2-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (294.7 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

zlib_rs-0.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (294.8 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

zlib_rs-0.1.2-cp312-cp312-win_amd64.whl (202.4 kB view details)

Uploaded CPython 3.12Windows x86-64

zlib_rs-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (295.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

zlib_rs-0.1.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (280.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

zlib_rs-0.1.2-cp312-cp312-macosx_11_0_arm64.whl (254.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

zlib_rs-0.1.2-cp312-cp312-macosx_10_12_x86_64.whl (282.9 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

zlib_rs-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (295.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

zlib_rs-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (295.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

zlib_rs-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (297.9 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

zlib_rs-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (298.1 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

  • Download URL: zlib_rs-0.1.2.tar.gz
  • Upload date:
  • Size: 22.8 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.2.tar.gz
Algorithm Hash digest
SHA256 1e6214c5f2a83a2751c9716d9da8672745825f62a3d89669ac73a1e7ea3efba0
MD5 0ca1c6996af8f95c9ea80e92cca79a47
BLAKE2b-256 10d537a2db5c2bae8b900dc52527a3a1bb740731741cfe680ec816d10ce282de

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.2.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.2-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.2-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f9bda5812249662fec9d6f44a4c2e121feb4262a5287bd73e0cd2666ad594661
MD5 ce4ea0291c730e87edd685a16b5daaf7
BLAKE2b-256 5875c032dd0b38a5cc5ccfa9878b87c9a70a6e016ebdee9d5cb7faa217a96c24

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.2-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.2-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.2-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b6fb5ccda44794cdf3c4ad23fa4da820c6535a5822c3f3f8e92f115d2805fa68
MD5 625738f41973df1386584f60c2e90954
BLAKE2b-256 f42f1a1009843ce207a40bb3e12df2c17b1909e9e2bda414da0ba1c3080682ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.2-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.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0d18eabc33c978bb7c21bb4369e1dc854a44185b494fb081b4a7d2c617a3524e
MD5 2bdb4104cc38b6e74696228cb67ad636
BLAKE2b-256 30fec97bc4688167af0be463601da828c5fc90203883dd5149e347637bfe22f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.2-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.2-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: zlib_rs-0.1.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 202.4 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.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4e0a3f54001f077fb3014078ab0fe190569ecf7baa14f3bddc4b8995dc146a43
MD5 70980f0b68dc50c5037e9dbef385c077
BLAKE2b-256 6deeb410d039532c059e9d10216cd5a6a18dd9cd4e482101a38f731959f04b42

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.2-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.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a0d4e33a380b08cd375ac933b5a56dcfa149e1fbdc8934717a51742d58b0348c
MD5 786b9fbf5958a143583f26068ea78b4a
BLAKE2b-256 ad7f2117730fa6c172c457fa07add5bf9f1fdaeb7bf69ec8efda51289e20e382

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.2-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.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 154760c38600ed1ebf2aeee5794ad8d3805a08db47d0cb4d7f4c69053ac8d015
MD5 e232d33cfdc1800930bac5b4c902093c
BLAKE2b-256 43d44fea55ed3cb18637246d8c7433c3283af8a12da17758cace3f7e46e3c269

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.2-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.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 de1203affe28dcb3bd714570fcd022cf67b7d6ab6b90095999311813062aca56
MD5 db8ae963927ecb0b12d270d28ac39d8b
BLAKE2b-256 b8975849217e4865ff05af5d6d7f1a275797cc3d7043577be17a24e7512e9db3

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.2-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.2-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.2-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a002a69e6820aa2550240a0f21859f55fa771123082f093423abea74fe520822
MD5 88e9d7d4e99a109599ddf9ea7362e301
BLAKE2b-256 1877e452a3856b67c3a8768a43c2ba7476ec3e9ee131391b092703d2d2a0b18a

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.2-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.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8405369a4b1482ca1adeb51d8f7be922ac75e81c016eb85625278c93226d477d
MD5 a2214b92e9f601629090b77ed44bd50f
BLAKE2b-256 a03daefc5a7b330d91e1308b5d0b59bc90b658d2f089fd79c94ec4e230794422

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.2-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.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8189fa4c32be03e08dc9a8407aee79df91e8f29ff2cb5bc826cd4e6a1d6dedda
MD5 7d06287de407a917307fcd88a0b00091
BLAKE2b-256 87d23068ab0abb9102b672a490009f09fbeb4a0f945ec6c5df347d7ef73ad865

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.2-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.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0587e35653692ffa1d696526ccaf41a337960c62fc2286c0a4bfc2b0af2b2bcc
MD5 72ce4d6bdc69091d230c519a67b7d6f1
BLAKE2b-256 6cb8ba12fd96b42f72f4dc0002cc4f2014f0e40e2181e9eaf2513f873233c489

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.2-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.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zlib_rs-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0daf266f1f2744c0ece24b7ae9f4e699da58fcf9e9dfd0970c7bc99284277a86
MD5 5dd6c7106cfe0b72fae6e05b0e9e4525
BLAKE2b-256 f15e6f72892d63fb805bcda7501fd7a85c811965c404fc099f494105b53b7378

See more details on using hashes here.

Provenance

The following attestation bundles were made for zlib_rs-0.1.2-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