Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The LZ4 library bindings provided by this package require the LZ4 library. If
the system already has an LZ4 library and development header files present, and
the library is a recent enough version the package will build against that.
Otherwise, the package will use a bundled version of the library files to link
against. The package currently requires LZ4 version 1.7.5 or later.
against. The package currently requires LZ4 version 1.11.0 or later.

On a system for which there are no pre-built wheels available on PyPi, running
this command will result in the extension modules being compiled from source::
Expand Down
43 changes: 36 additions & 7 deletions lz4/frame/_frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ compress (PyObject * Py_UNUSED (self), PyObject * args,
int content_checksum = 0;
int block_checksum = 0;
int block_linked = 1;
int number_of_threads = 0;
LZ4F_preferences_t preferences;
size_t destination_size;
size_t compressed_size;
Expand All @@ -134,21 +135,23 @@ compress (PyObject * Py_UNUSED (self), PyObject * args,
"block_linked",
"store_size",
"return_bytearray",
"number_of_threads",
NULL
};


memset (&preferences, 0, sizeof preferences);

if (!PyArg_ParseTupleAndKeywords (args, keywds, "y*|iippppp", kwlist,
if (!PyArg_ParseTupleAndKeywords (args, keywds, "y*|iipppppi", kwlist,
&source,
&preferences.compressionLevel,
&preferences.frameInfo.blockSizeID,
&content_checksum,
&block_checksum,
&block_linked,
&store_size,
&return_bytearray))
&return_bytearray,
&number_of_threads))
{
return NULL;
}
Expand Down Expand Up @@ -201,6 +204,16 @@ compress (PyObject * Py_UNUSED (self), PyObject * args,
preferences.frameInfo.contentSize = 0;
}

if (number_of_threads < 0)
{
number_of_threads = 0;
}
else if (number_of_threads > LZ4F_getMaxNbWorkers())
{
number_of_threads = LZ4F_getMaxNbWorkers();
}
preferences.nbWorkers = number_of_threads;

Py_BEGIN_ALLOW_THREADS
destination_size =
LZ4F_compressFrameBound (source_size, &preferences);
Expand Down Expand Up @@ -271,6 +284,7 @@ compress_begin (PyObject * Py_UNUSED (self), PyObject * args,
int content_checksum = 0;
int block_checksum = 0;
int block_linked = 1;
int number_of_threads = 0;
LZ4F_preferences_t preferences;
PyObject *py_destination;
char * destination;
Expand All @@ -289,12 +303,13 @@ compress_begin (PyObject * Py_UNUSED (self), PyObject * args,
"block_linked",
"auto_flush",
"return_bytearray",
"number_of_threads",
NULL
};

memset (&preferences, 0, sizeof preferences);

if (!PyArg_ParseTupleAndKeywords (args, keywds, "O|kiippppp", kwlist,
if (!PyArg_ParseTupleAndKeywords (args, keywds, "O|kiipppppi", kwlist,
&py_context,
&source_size,
&preferences.compressionLevel,
Expand All @@ -303,7 +318,8 @@ compress_begin (PyObject * Py_UNUSED (self), PyObject * args,
&block_checksum,
&block_linked,
&preferences.autoFlush,
&return_bytearray
&return_bytearray,
&number_of_threads
))
{
return NULL;
Expand Down Expand Up @@ -354,9 +370,18 @@ compress_begin (PyObject * Py_UNUSED (self), PyObject * args,
preferences.frameInfo.blockMode = LZ4F_blockIndependent;
}


preferences.frameInfo.contentSize = source_size;

if (number_of_threads < 0)
{
number_of_threads = 0;
}
else if (number_of_threads > LZ4F_getMaxNbWorkers())
{
number_of_threads = LZ4F_getMaxNbWorkers();
}
preferences.nbWorkers = number_of_threads;

context =
(struct compression_context *) PyCapsule_GetPointer (py_context, compression_context_capsule_name);

Expand Down Expand Up @@ -1367,14 +1392,18 @@ PyDoc_STRVAR(
" library has version >= 1.8.0. Attempting to set this value\n" \
" to ``True`` with a version of LZ4 < 1.8.0 will cause a\n" \
" ``RuntimeError`` to be raised.\n" \
" number_of_threads (int): since LZ4 1.11.0 it is possible to set the\n" \
" number of working threads used for compression. With the default\n" \
" value set to 0 the old behavior is kept (single thread compression).\n" \
" return_bytearray (bool): If ``True`` a ``bytearray`` object will be\n" \
" returned. If ``False``, a string of bytes is returned. The default\n" \
" is ``False``.\n" \

PyDoc_STRVAR(
compress__doc,
"compress(data, compression_level=0, block_size=0, content_checksum=0,\n" \
"block_linked=True, store_size=True, return_bytearray=False)\n" \
"block_linked=True, store_size=True, return_bytearray=False,\n" \
"number_of_threads=0)\n" \
"\n" \
"Compresses ``data`` returning the compressed data as a complete frame.\n" \
"\n" \
Expand All @@ -1398,7 +1427,7 @@ PyDoc_STRVAR
compress_begin__doc,
"compress_begin(context, source_size=0, compression_level=0, block_size=0,\n" \
"content_checksum=0, content_size=1, block_linked=0, frame_type=0,\n" \
"auto_flush=1)\n" \
"auto_flush=1, number_of_threads=0)\n" \
"\n" \
"Creates a frame header from a compression context.\n\n" \
"Args:\n" \
Expand Down
Loading