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.1.tar.gz (114.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.1-cp38-abi3-win_amd64.whl (225.2 kB view details)

Uploaded CPython 3.8+Windows x86-64

safelz4-0.0.1-cp38-abi3-win32.whl (217.9 kB view details)

Uploaded CPython 3.8+Windows x86

safelz4-0.0.1-cp38-abi3-musllinux_1_2_x86_64.whl (541.2 kB view details)

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

safelz4-0.0.1-cp38-abi3-musllinux_1_2_i686.whl (576.0 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

safelz4-0.0.1-cp38-abi3-musllinux_1_2_armv7l.whl (643.7 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

safelz4-0.0.1-cp38-abi3-musllinux_1_2_aarch64.whl (549.6 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

safelz4-0.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (371.1 kB view details)

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

safelz4-0.0.1-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (411.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

safelz4-0.0.1-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (508.9 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

safelz4-0.0.1-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (381.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

safelz4-0.0.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (371.8 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

safelz4-0.0.1-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (400.4 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.5+ i686

safelz4-0.0.1-cp38-abi3-macosx_11_0_arm64.whl (336.9 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

safelz4-0.0.1-cp38-abi3-macosx_10_12_x86_64.whl (347.4 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for safelz4-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a35933b5ede0f4032657435064a5a28433d54fd78df422b6fdac9d4d2827b7e3
MD5 99f919aeb56acae1cc0eca2beb4265d9
BLAKE2b-256 0e780fbcd5eaea29bcac628cd5185e269487dab15116a02a2c3d8c150af17fce

See more details on using hashes here.

File details

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

File metadata

  • Download URL: safelz4-0.0.1-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 225.2 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.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 58649b7604db58d9f38676d3f52e857ab3cea31d81560dedcf32ebe942e01e58
MD5 dfd2a2345a60cb85351681331e1dbd48
BLAKE2b-256 8574b0a4e6d947ffbe7b8ea3a4af9d92683e1656bf380012bdd353adea6d290b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: safelz4-0.0.1-cp38-abi3-win32.whl
  • Upload date:
  • Size: 217.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.1-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 f7d1da426183d8a37b75a3d59bfdd5db426363920f5738acf5e0d4aa098b3c40
MD5 b04fa9c0329b370def89244308368041
BLAKE2b-256 dac99924b630e3419cab70d75b3d0388ff34bc0d380c0842c0896cf7c1853c3d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.1-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 554cbc8115c82f66d43ced86fbd9c933b6382290d91f7702672ec4544794f083
MD5 9294d7635a97a418ca2679c65673853e
BLAKE2b-256 a689acdc8c64e15d1b9dcbd55f712fbf00ef647d5ac2605bb879a2c2305be7f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.1-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 19bf0368b079f17649b95a0dfbe3ce70d0f181b2894e97d9345200e1fc7a4cc9
MD5 45954b971e1d8512d9367f024e67f606
BLAKE2b-256 49b3569d2cdb84d644c20c1cf2f38e5bdfa21f7d8472d8b4d7cd5b4163ed2a2d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.1-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 6c1d2c1e2141ffdbe44e8b31ef1be73027776b89da4524374efb11a082ae7f9d
MD5 1e3fda8ad955a5a480ee89486dd84e06
BLAKE2b-256 de66a9c6a4790b4eee1a54c57689e8d2e234b85307fecd08888055c680ee2dd6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.1-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 21cfbe90bb75091bdd50a016ad22cfa5bd5db0511ab4458d069f5ff0f9ea028f
MD5 7efd5f8f8f1853ebefc69c8e667b68c2
BLAKE2b-256 05aa8a50d4a599df8049c9cdc874ffd99c19d0c38fe29c08241497487c7cb4c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 83611e9449897801d8fa3a6f6089080ddc14b2146fc922f0c4a83500b87a0e1d
MD5 95666cb89757ebb84211e170a1cd29af
BLAKE2b-256 16d2dca613c1181c3237cdef933d433f5ecbb8389a35417c63b286155b3f5054

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.1-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 e6dc34e80c87be8cb83f8675a47a41afdc77b10c2af639004e26c61bc22aa353
MD5 3d6c2a418fb2e051db3b9eda5d6ab59f
BLAKE2b-256 6e84bfd13627b01e93cddc2943559664b8e188a92a77f50334d3c7027237fcb8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.1-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 6f44ea2e81967a181f2b5ba0bd975ccca0e793ca85e8bde6cb4bcfa02b9c4d0a
MD5 f5234f8dec4308bea6da9d18a660a221
BLAKE2b-256 2068d8ca486e64d5dd6bc67c17912a07c6f990f5dfb61aa43c0101093171b03d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.1-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 de53bfdc87e86904f4aa10157274dcc6af30913d7b8a84d66d2d85fb286a3b8f
MD5 af9772b39dea75b0def55e437f7ab844
BLAKE2b-256 258cd38cb34e7e7cf65eec23b2140f72fe1949ced2d57fdf19776dc5c82b007b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e10fd8586dba050f166fd354004bdac7c00f040bbc12ca73ca126f012014946f
MD5 23262a79b2f84c1b16747a8e3af76c4c
BLAKE2b-256 48ad6691b82c6d6c10999a037d42db21ee82e849b4982c628c0951717760eea6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.1-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 c073cb0f05b1f0f156f0a6c21882766fe6f74197c7bf071e406beebfcc371908
MD5 e5abd48b4794973200263d7d488ba3ea
BLAKE2b-256 2f8f595c190a72063d4cabbe8fef7c7b66aa49a29c9b362aad34d5f7d55924a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2dbb9b001817a78cab19fac5851d04b2e84f3941c465448e443648bd72684fb2
MD5 dbb4a38af9aecf6d1ba67972ecc50c45
BLAKE2b-256 ca261096ac71df40ee26bafa5783781939b2c72b71595f85eb7ee3c36649b0b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safelz4-0.0.1-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0435b3a6d33afdfc6b7621d7136bc791981441e25ed2e9c2c5502a3ef6a4b2a2
MD5 612be5e2fb040fe180aad0c7f471d584
BLAKE2b-256 d4a2c3d6f5e2a50a6bf4ec08d0f2d16913d2e0e2ed317acf9f0fc4d944c7a7f3

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