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 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.0.5.tar.gz (116.3 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.5-cp38-abi3-win_amd64.whl (232.8 kB view details)

Uploaded CPython 3.8+Windows x86-64

safelz4-0.0.5-cp38-abi3-win32.whl (225.0 kB view details)

Uploaded CPython 3.8+Windows x86

safelz4-0.0.5-cp38-abi3-musllinux_1_2_x86_64.whl (549.5 kB view details)

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

safelz4-0.0.5-cp38-abi3-musllinux_1_2_i686.whl (583.5 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

safelz4-0.0.5-cp38-abi3-musllinux_1_2_armv7l.whl (650.7 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

safelz4-0.0.5-cp38-abi3-musllinux_1_2_aarch64.whl (558.0 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

safelz4-0.0.5-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (379.2 kB view details)

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

safelz4-0.0.5-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (419.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

safelz4-0.0.5-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (517.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

safelz4-0.0.5-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (387.8 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

safelz4-0.0.5-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (380.6 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

safelz4-0.0.5-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (408.2 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.5+ i686

safelz4-0.0.5-cp38-abi3-macosx_11_0_arm64.whl (344.6 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

safelz4-0.0.5-cp38-abi3-macosx_10_12_x86_64.whl (354.5 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for safelz4-0.0.5.tar.gz
Algorithm Hash digest
SHA256 e56ac5a6ee25210bf8a676bfe9fbe15e952ce2e803f778bbe323015a44124200
MD5 dacce9d8a6a4dbece5a3063b4863075f
BLAKE2b-256 2c87faac0c002ed0d9d75c16004579ea596d7a4bcace3ffba1ab1e1e7bcc0648

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for safelz4-0.0.5-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 e80cfafccc93029fc74444b43dd6faff0373dc986b574c030102ac76dd76aae0
MD5 cb90c765e2d2a000c19f8e07dcb7977b
BLAKE2b-256 00fdea7edc250fa18acc519f910adbc4505dec31405ce6b7ab396f745f82fcd4

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for safelz4-0.0.5-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 36cd6dccc26e6138569474dcacd6c2777f73311341097d298e58ada9626fad6e
MD5 1be1ffb8218ded8f407396d316154e17
BLAKE2b-256 6a517054209c5ad9ae62213707a7e89d26e4b653736ded34a5e22fba41e54e28

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.5-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d444044f7feef6a2ccfa39950f1de308097637f1b6bccaa5088a07327c416a18
MD5 8edf5e2a37f5540f90307a4a3b45248a
BLAKE2b-256 b950b92f461012aeafa0b376e7fb2a819536ffc49744cf4182d46027d2a15f89

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.5-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 ee481b3e557b3f0b04503ff2a1d3b1ec28e865c37a01b2fe654e3ad2ca19135b
MD5 f854d3a13c474c8996492b7219f5facf
BLAKE2b-256 06406b6f38e4a5682070128cbf09dbb87b182ac8ebbcc59f853c26747b0907d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.5-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 5919b13d3b02b36996f602ac36179c7d82f668c5c01951833b294feb25b5d693
MD5 0264ec38875c94281039abbc3e0633d9
BLAKE2b-256 59bed21f4ad3f64f8eb6fb10805a747b9f8aa346291d741bd39da77e60dbcc21

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.5-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 36f67a70dccc1c0a91a8b4eb8372791645a66c2482919e58c9c376bc7b686a91
MD5 8d1fe8de5ae5d23da6e45ca9ea4c9b0d
BLAKE2b-256 1f13f0328ae30b47925303a3ee5b270c347c7dbe775682f6aeeefde5f5a075c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.5-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4c9a03a10654861369a72da0fc242d2b4e30308f350266022c950903218ad7e2
MD5 61e736dff6725ffc80be5b71bf94aae1
BLAKE2b-256 45af7e240e16b79eaf10fd17fe4d3f6899d0532331a38c5173792772451142f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.5-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 12f24b0d74ed42ec6ec809a9993c9c3015535ec6ecb18d01391280bcfd4443a8
MD5 d7181e7c563b76e7da2c5b5fd01d2492
BLAKE2b-256 02562dc51c042cea4a5c9d1a945402fb0fce8921ac73b45bbbd9037ae3a5cbc9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.5-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 18616ae002ec078e45568627cb6817d2c55314b96173527f0e8b7b4623a4107b
MD5 86478f48d90b3910c93794d952c5e6bb
BLAKE2b-256 ca730d2afa28e6d4b25533827e82c9e08c66aca25c875d609b2135a7a5f2d2e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.5-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 266799458ff4a14e7fe56c7da16ab43cc0b8afd25fe95d8d83a9da6ba94791df
MD5 af143cb3540c37060424489b9752003c
BLAKE2b-256 05660243d54e39e8bdc56ac19693231ddc937338a1ae541617cecf301d709b50

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.5-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9d99a267e2a47788f1230f0a4c48a36500a2a90b1000c98be317d80b4d758220
MD5 1467944166be5ff3d5a4f9d746a44e70
BLAKE2b-256 b3f66ba94b185792bc4fce9e3929fc11c6d6607105137258d967cdb0dbc126db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.5-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 292344f8cbca34f29fc92907c3b9c75de1996397fde9058d5efaa6d0cd0a0c85
MD5 30d8bb8f10630968a9dc15a21336ed81
BLAKE2b-256 73941788d47e58d59ccd54c18afc23db17b13f21db573a199319d8c7f2776ebe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.5-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 436bf6a92ba8577449ea4e1ae6762ccd2c266c3e04f532f34f8e6ad13292df14
MD5 6806838f23e43855712cb411136e13e0
BLAKE2b-256 db5976b6380d5a0e0fdb6b382dcb1d705ff76596547db9d43dc8078ab5e220ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.5-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3a0e21d3fddab44d07ec7a68a5afa0c316ac61a4f36f1f6ef2a1f37b98da1b70
MD5 d7d3930f2c679e372719968b53948c40
BLAKE2b-256 329912e230485705316b05d1d9b7a09ebabd79b970575df74e27e9737af86807

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