Skip to main content

safe LZ4 data compress library.

Project description

safelz4

GitHub PyPI Python Version PyPI Downloads

Python bindings for lz4_flex, the fastest pure-Rust implementation of the LZ4 compression algorithm.

Installation

Pip

You can install safelz4 via the pip manager:

pip install safelz4

From source

For the sources, you need Rust

# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Make sure it's up to date and using stable channel
rustup update
git clone https://github.com/LVivona/safelz4.git
cd safelz4
pip install setuptools_rust
pip install maturin
# install
pip install -e .

Getting Started

Block Format

safelz4 block

The block format is suitable only for smaller chunks of data, as each block must be fully compressed or decompressed in memory. For larger data sequences, the frame format should be used instead, as it supports streaming and includes metadata for better handling of large-byte sequences. specs

import os
import sys
from typing import Union, Generator
from safelz4.block import compress_prepend_size, decompress_size_prepended

def chunk_blocks(filename : Union[os.PathLike, str], chunk_size : int = 1048576) -> Generator[bytes, None, None]:
    """compress read bytes into chunks blocks"""
    with open(filename, "rb") as f:
        while content := f.read(chunk_size):
            buffer = compress_prepend_size(content)
            yield buffer

# 1 Mb chunck
blocks = chunk_blocks("dickens.txt")

for block in blocks:
    output = decompress_size_prepended(buffer)
    sys.stdout.write(output.decode("utf-8"))

Frame Format

safelz4 frame

Frames are containers that encapsulate a set of compressed blocks. Information about the blocks is stored both in the frame header and within the blocks themselves. Read more within the specs

import safelz4

buffer = None
with open("dickens.txt", "rb") as file:
    buffer = file.read(-1)
    safelz4.compress_into_file("dickens.lz4", buffer)


with safelz4.open("dickens.lz4", "rb") as f:
   while content := f.read(100):
      print(content.decode("utf-8"))

Bechmarks

Benchmark results are available in the benches folder. We evaluated performance in two key scenarios:

Full byte availability, where the entire buffer is accessible during compression and decompression.

Streamed access, using reader and writer interfaces with chunked input.

Summary

In full buffer scenarios, lz4 generally performs well and occasionally outpaces safelz4, especially on larger files. However, safelz4 still remained competitive, with close times.

In reader/writer scenarios (chunked input, 1024 bytes), safelz4 significantly outperforms lz4, consistently achieving more than 2x speed improvement in both compression and decompression.

Streamed access (chunk 1024 bytes)

open Write Benchmark lz4 safelz4
ctx_compression_writer_compression_1k.txt 22.5 us 8.84 us: 2.54x faster
ctx_compression_writer_compression_34k.txt 22.6 us 9.07 us: 2.49x faster
ctx_compression_writer_compression_65k.txt 23.0 us 9.18 us: 2.50x faster
ctx_compression_writer_compression_66k_JSON.txt 23.1 us 9.18 us: 2.51x faster
ctx_compression_writer_dickens.txt 23.9 us 9.16 us: 2.61x faster
ctx_compression_writer_hdfs.json 22.9 us 9.21 us: 2.49x faster
ctx_compression_writer_reymont.pdf 22.9 us 9.26 us: 2.48x faster
ctx_compression_writer_xml_collection.xml 23.1 us 9.27 us: 2.49x faster
Geometric mean (ref) 2.51x faster
open Read Benchmark lz4 safelz4
ctx_decompression_writer_compression_1k.txt 17.6 us 11.0 us: 1.59x faster
ctx_decompression_writer_compression_34k.txt 46.2 us 23.8 us: 1.94x faster
ctx_decompression_writer_compression_65k.txt 68.6 us 34.6 us: 1.98x faster
ctx_decompression_writer_compression_66k_JSON.txt 61.9 us 27.1 us: 2.28x faster
ctx_decompression_writer_dickens.txt 8.67 ms 4.11 ms: 2.11x faster
ctx_decompression_writer_hdfs.json 4.39 ms 1.77 ms: 2.48x faster
ctx_decompression_writer_reymont.pdf 5.74 ms 2.92 ms: 1.97x faster
ctx_decompression_writer_xml_collection.xml 3.97 ms 1.99 ms: 2.00x faster
Geometric mean (ref) 2.03x faster

Full byte availability Run(s)

frame.compress Benchmark lz4 safelz4
compression_compression_1k.txt 839 ns 829 ns: 1.01x faster
compression_compression_34k.txt 32.5 us 26.3 us: 1.23x faster
compression_compression_65k.txt 60.1 us 49.9 us: 1.20x faster
compression_compression_66k_JSON.txt 24.7 us 26.5 us: 1.07x slower
compression_dickens.txt 15.9 ms 17.0 ms: 1.07x slower
compression_hdfs.json 2.63 ms 3.16 ms: 1.20x slower
compression_reymont.pdf 11.4 ms 12.3 ms: 1.08x slower
compression_xml_collection.xml 4.12 ms 4.58 ms: 1.11x slower
Geometric mean (ref) 1.01x slower
frame.decompress Benchmark lz4 safelz4
decompress_compression_1k.txt 416 ns 612 ns: 1.47x slower
decompress_compression_34k.txt 10.0 us 8.96 us: 1.12x faster
decompress_compression_65k.txt 17.1 us 15.4 us: 1.11x faster
decompress_compression_66k_JSON.txt 8.04 us 9.45 us: 1.18x slower
decompress_dickens.txt 2.13 ms 4.00 ms: 1.88x slower
decompress_hdfs.json 1.03 ms 1.50 ms: 1.45x slower
decompress_reymont.pdf 1.99 ms 2.42 ms: 1.21x slower
decompress_xml_collection.xml 1.19 ms 1.68 ms: 1.41x slower
Geometric mean (ref) 1.26x slower

NOTE: All benchmarks were performed using python package pypref, on a system equipped with an Apple M4 Max processor and 36GB of unified memory.

Acknowledgement

This project acknowledges the outstanding work of Yann Collet.

Special thanks also to the maintainers of the lz4_flex Rust crate for providing a safe, pure-Rust implementation of LZ4 compression and decompression.

Other Implementation

LZ4 implementations, including:

Python Library Build Status Version Licence
python-lz4 Build Status PyPI - License

Licence

MIT 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

safelz4-0.1.0.tar.gz (120.4 kB view details)

Uploaded Source

Built Distributions

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

safelz4-0.1.0-cp38-abi3-win_amd64.whl (234.5 kB view details)

Uploaded CPython 3.8+Windows x86-64

safelz4-0.1.0-cp38-abi3-win32.whl (227.1 kB view details)

Uploaded CPython 3.8+Windows x86

safelz4-0.1.0-cp38-abi3-musllinux_1_2_x86_64.whl (551.3 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ x86-64

safelz4-0.1.0-cp38-abi3-musllinux_1_2_i686.whl (584.8 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

safelz4-0.1.0-cp38-abi3-musllinux_1_2_armv7l.whl (651.5 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

safelz4-0.1.0-cp38-abi3-musllinux_1_2_aarch64.whl (557.8 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

safelz4-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (381.2 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

safelz4-0.1.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (416.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

safelz4-0.1.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (519.7 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

safelz4-0.1.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (387.2 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

safelz4-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (380.1 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

safelz4-0.1.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (409.9 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.5+ i686

safelz4-0.1.0-cp38-abi3-macosx_11_0_arm64.whl (345.2 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

safelz4-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl (357.1 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file safelz4-0.1.0.tar.gz.

File metadata

  • Download URL: safelz4-0.1.0.tar.gz
  • Upload date:
  • Size: 120.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.4

File hashes

Hashes for safelz4-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f1d9a6ff6f5588e4cd8dfbbdb84490fb9d1d0658a99008d604b1ec5ecbac84a2
MD5 21cd458a5c14f8a6b7111886cb3fe367
BLAKE2b-256 149523b6a266a051315f53ceb855db96cbdc505ac7cf1e99732a06af699c3e85

See more details on using hashes here.

File details

Details for the file safelz4-0.1.0-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: safelz4-0.1.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 234.5 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.4

File hashes

Hashes for safelz4-0.1.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 8b8d3a54699820e4dcc05347023f4606d22df6b9467a226d6967a29ee5409bcc
MD5 7809e2a06bdf32ddc04a18f7e8ad2f95
BLAKE2b-256 140d1bf6d556447fc4248888a9dc02cef08cd32a4fa1b3837df77cf63c075601

See more details on using hashes here.

File details

Details for the file safelz4-0.1.0-cp38-abi3-win32.whl.

File metadata

  • Download URL: safelz4-0.1.0-cp38-abi3-win32.whl
  • Upload date:
  • Size: 227.1 kB
  • Tags: CPython 3.8+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.4

File hashes

Hashes for safelz4-0.1.0-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 03a2efd20775d1c32e45bd442d49f958e00af16c3c5e3b7fc5980b2fe81d7108
MD5 b3a30bf975bf3a68d0234d3a115a2e0e
BLAKE2b-256 18cd45c57805c768c5a366d46217c70352138f6a15f49b32270ed797497886bb

See more details on using hashes here.

File details

Details for the file safelz4-0.1.0-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for safelz4-0.1.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8fbf2bd3ea565b1111c191a4085db1c7ce2c680039c64ef28215850854110fb9
MD5 edd3e5078bcc99eb7a184952577938da
BLAKE2b-256 bace46f5831187aa2182043414a2c89b2cb9c5be501e14411afe9b32b31798d3

See more details on using hashes here.

File details

Details for the file safelz4-0.1.0-cp38-abi3-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for safelz4-0.1.0-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 ea2213dd2c0f86377c4ddd6d6f0c3fc6a342a1a0d70c388a1f2af08c3d6e35f1
MD5 f1f13f50a49e60af7a145c43bf7f833d
BLAKE2b-256 b9dcdd76726892f102d8e732325b69b1976dcff7a21706bae7daefd49afe92c7

See more details on using hashes here.

File details

Details for the file safelz4-0.1.0-cp38-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for safelz4-0.1.0-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 a356f4c226141621afe7b16a208e14e2fa765c46b1765bd8fdf15c07caf541aa
MD5 4cd59ed38c984979e5b458ddce5c7b7c
BLAKE2b-256 76b4125621cdf05bd23d22365465432c96b12076499f29bfab32317f9b32bc0b

See more details on using hashes here.

File details

Details for the file safelz4-0.1.0-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for safelz4-0.1.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 6b3b2e539bd94db1d12b7a33493d731075692b658b0ef5d0d560b6761f980fb7
MD5 40375a9d3f2c67c4b68d5ea790076d1b
BLAKE2b-256 065ba80aaac80dab44cec7c6d21b55a7c8c997ddd0cf9f71f2cbb9e9e7f382f3

See more details on using hashes here.

File details

Details for the file safelz4-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for safelz4-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 429a6b4ea25c6ffdd01288e525870f60283ee42912e7a32003e2e51f12333fed
MD5 46460666f4ba610eac59f3d972577952
BLAKE2b-256 a925abd6afee49c1d2761b388e131e282276131e128ed91f842110298dbe2531

See more details on using hashes here.

File details

Details for the file safelz4-0.1.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for safelz4-0.1.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 91407d9e993f0d07c38152003ba6673c4dc500efd5487267e54dd6e02f05be56
MD5 1300f79c6250e9c9f9e0ae990f9b1ff4
BLAKE2b-256 b72b9c9f66c32183a01c7c42de847be8a92a88da646f35c6f87488ff58630fa2

See more details on using hashes here.

File details

Details for the file safelz4-0.1.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for safelz4-0.1.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 7af52a2cfd0cd127fa275090b4ce7a42875a93cda6b03f828bfde55344396a0d
MD5 f133c921eafc4e2fc98ce4ea8250d8fb
BLAKE2b-256 6ccda924bf730e575f26ea6f8a322fda0e263f38750cdb39034bd8e05ea66821

See more details on using hashes here.

File details

Details for the file safelz4-0.1.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for safelz4-0.1.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 db00cd268350a6f265fda88b8cdb54fd492fec39fef2c39609097ffecd7348bf
MD5 1ae52abc77495d053d9c9f944c68981a
BLAKE2b-256 ade7156394716683129a06862ff4ccc348f739de2cf998d955448ab6876935e6

See more details on using hashes here.

File details

Details for the file safelz4-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for safelz4-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 df8b93fdfb8d54e5b5c3b44ea127a6bce01bbc8b6278fe6fb753881415667e9a
MD5 3b1c63955b629267145e9af6d01e0be3
BLAKE2b-256 25e2240171af935b62af83034be09f606d105285942dbd4b2350907b5cf516f4

See more details on using hashes here.

File details

Details for the file safelz4-0.1.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for safelz4-0.1.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 de7987160f4a6bf91ac9683dbffa7f5235c1a0d93f5e70f0cb4309312e034633
MD5 03364406903162ce1c15f74ae1dafda2
BLAKE2b-256 4f370d5c545c12f98c7a236a5295909bff4c06006bdcbfbb980bd7abc3169ebc

See more details on using hashes here.

File details

Details for the file safelz4-0.1.0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for safelz4-0.1.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 11341eeedfc2a506405299478a6d020f2e6b10475cf7471cf28baeed6daf54b4
MD5 c37e22581b3fcbd021ab36dd43d1b7fe
BLAKE2b-256 db4b96f451761f519d20c45ad62af428a5a0b895560859d2b7337ae5e81cc3b2

See more details on using hashes here.

File details

Details for the file safelz4-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for safelz4-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 dd3f942c86c9979d5d075d86c59a970268dead78fb27598cdd6b92ae72f2d36d
MD5 38ace48e05b77aaf6ce974f7a780d731
BLAKE2b-256 8003fd66dd28a44104f5baaa5dcb53e2256794c7768f9c938b83aaff3737cdce

See more details on using hashes here.

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