Skip to main content

safe LZ4 data compress library.

Project description

safelz4

GitHub PyPI Python Version

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

Full byte availability Run(s)

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

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 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.0.2.tar.gz (114.9 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.0.2-cp38-abi3-win_amd64.whl (226.5 kB view details)

Uploaded CPython 3.8+Windows x86-64

safelz4-0.0.2-cp38-abi3-win32.whl (218.9 kB view details)

Uploaded CPython 3.8+Windows x86

safelz4-0.0.2-cp38-abi3-musllinux_1_2_x86_64.whl (542.6 kB view details)

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

safelz4-0.0.2-cp38-abi3-musllinux_1_2_i686.whl (577.1 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

safelz4-0.0.2-cp38-abi3-musllinux_1_2_armv7l.whl (644.9 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

safelz4-0.0.2-cp38-abi3-musllinux_1_2_aarch64.whl (551.0 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

safelz4-0.0.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (372.3 kB view details)

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

safelz4-0.0.2-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (412.4 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

safelz4-0.0.2-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (510.4 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

safelz4-0.0.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (381.9 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

safelz4-0.0.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (373.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

safelz4-0.0.2-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (401.6 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.5+ i686

safelz4-0.0.2-cp38-abi3-macosx_11_0_arm64.whl (338.3 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

safelz4-0.0.2-cp38-abi3-macosx_10_12_x86_64.whl (348.5 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for safelz4-0.0.2.tar.gz
Algorithm Hash digest
SHA256 6519f8f359a5a13fa579a7351816ebd662f48c2ca1e61782e804ed48f8a7eebd
MD5 2e84fa48cd1b8ee0a4399c578ec0b8ee
BLAKE2b-256 d88106d9f330abd6c7b4b69954f58272ac323c4cd4de7ab9c17f5f232fbe0085

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for safelz4-0.0.2-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 45889cd076b7da4eae0300da99281ffa8116afafabfecd066ed67a410e8b05ad
MD5 443dba573fd1617d7142242d9e4148f9
BLAKE2b-256 dd98b1a44c660567b6504285bfc0f9d90874570a5d5f35242036472813d7de6e

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for safelz4-0.0.2-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 ff4dcf10473d2b2db1d070cfd0d55f2c8098a15339ca0677975136f074b0a62c
MD5 811e5f7f5fa39d5c7f035d82a6362339
BLAKE2b-256 2f9b02e749edcf1120119e857ce33367a629c0c5fa0eb6c61ceeb5266353baf8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.2-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6cd969f516c8fd0b00ec8ca81d282d3eeaf4ff9f61678186529d8d769cbf3fab
MD5 8dae810e576ffbe45698f05a82f6afdc
BLAKE2b-256 3cf3813512ce122054432cb1f05d39b0ae147882d38d53211d51fb4d5928a8b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.2-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 b9e929a60c311d336de3b27da699df364ae9e6c44ea8e86462169d97d2bd74c8
MD5 0d646a44e05ed4981464a2e9a70af526
BLAKE2b-256 ef8b305d0f7e529738e554a99efffe16bbedada59ce7acb5fe4bb60333cef402

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.2-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 92b3c3cbbe33504aa317660a1c2d4ce9fa1ae0cc277906dc24b43e8c09b0adcc
MD5 fc626e6b8bf76e732c7b2e026b65fb52
BLAKE2b-256 d7fde84feee41c34482f672bd2649f4bd06499717cd05df370ac62818b11b51f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.2-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 57d309d9ff9e7ac77e1d0037e8a8f30cec50ad04652a9a9856a70c599fed3ea2
MD5 51b670f6e950b7cd1e74964a1677c75a
BLAKE2b-256 30a58caff0a66930d78de85dc59c7c37d5d0ddaef0142756f019f933454e8e35

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4805066f9e4eb5090331bac25a8a182b061341d3910f4ff6ec6ab26f99dfc797
MD5 d2adbc1d27c6cd54ebaea9d3ace7e23d
BLAKE2b-256 d3f19d6b0214a113f11594af9aeb343d53dd13c5d0b24cda8c4379efcfc35e47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.2-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 d2c6571d389c2c6bf7601244d1fc2082920ab676aa97d14b28f4471bcedb8561
MD5 b65b697a4247b24c528cf532fddf80f0
BLAKE2b-256 6299f45e02bdae85303ca42ed74d1af135d172e87ac446dd25335eec141bad40

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.2-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 f588687c928dfc5bab816fee301bd7cbeb2012a1717b80245ae5e5072dacc4c7
MD5 9f37d8c7ed2453bb9f6d008f54fa7518
BLAKE2b-256 830f1e35c09343f58034e760b9ed4ef5ad4db5bbbc1d1d3ded2bbbf8e8600092

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 405b3ff5ea7a91bf2db65f26b5daafd71c52d33f80f0ed23e7f66a0f9a3a1111
MD5 9d8510013af716980d2eea367051e562
BLAKE2b-256 5fd6b14da1ea60e2ac2dd3a4fd6a2193befb4ac3114f12b3ac90e7a4ef23200d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 12a090d0c0f13f8dc9ce1d471c302e1a7c1c22832e38666bf533bdb4728c4c5c
MD5 2e6aee3476b96cef2d4eefc9d291da7f
BLAKE2b-256 d29cb61fa739a12b1cfc48494db5f4dac34cff7acd22b193d4104f5205ab74b1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.2-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 0db8d259d066fc7fb4b659a3d11081f99542799b67dd91ccb6213de6a91fcdf4
MD5 e23264314e42a93955a3d08e6cd180ea
BLAKE2b-256 ec1e9fd5c2f891903a89cfbcac5c6521c2b4cca95d7114d37f116a04ef0f186d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.2-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a84efafee8cfe16df6156466c72a58426713dbcfd8a1f1aa3881e89eb85d93a5
MD5 34670b4d447bbd5ee3138d83a166c328
BLAKE2b-256 4cbfd2d7fb190b4b40a75686a984ae5cb853067ebf2944a041d16dd9ad7ec1cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.2-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4db27b20323a5c3245afff43e61a18ee576b4abe461d828f954dc99e9f871861
MD5 2b0b4d0f01a223d68890f8f7308a226b
BLAKE2b-256 7756233dfcaa918ea6c509036727976795c914cf12f37a6303f44f3198457142

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