summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorRuben Vorderman <r.h.p.vorderman@lumc.nl>2021-09-02 15:02:59 (GMT)
committerGitHub <noreply@github.com>2021-09-02 15:02:59 (GMT)
commitea23e7820f02840368569db8082bd0ca4d59b62a (patch)
tree44dcdd66cf7335a31a837d7e84a857e5c677e2b3 /Modules
parenta7ef15aae8608560bffeeaba412c10e52cab07dd (diff)
downloadcpython-ea23e7820f02840368569db8082bd0ca4d59b62a.zip
cpython-ea23e7820f02840368569db8082bd0ca4d59b62a.tar.gz
cpython-ea23e7820f02840368569db8082bd0ca4d59b62a.tar.bz2
bpo-43613: Faster implementation of gzip.compress and gzip.decompress (GH-27941)
Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/clinic/zlibmodule.c.h32
-rw-r--r--Modules/zlibmodule.c9
2 files changed, 28 insertions, 13 deletions
diff --git a/Modules/clinic/zlibmodule.c.h b/Modules/clinic/zlibmodule.c.h
index 14e955d..e2a5fcc 100644
--- a/Modules/clinic/zlibmodule.c.h
+++ b/Modules/clinic/zlibmodule.c.h
@@ -3,7 +3,7 @@ preserve
[clinic start generated code]*/
PyDoc_STRVAR(zlib_compress__doc__,
-"compress($module, data, /, level=Z_DEFAULT_COMPRESSION)\n"
+"compress($module, data, /, level=Z_DEFAULT_COMPRESSION, wbits=MAX_WBITS)\n"
"--\n"
"\n"
"Returns a bytes object containing compressed data.\n"
@@ -11,26 +11,29 @@ PyDoc_STRVAR(zlib_compress__doc__,
" data\n"
" Binary data to be compressed.\n"
" level\n"
-" Compression level, in 0-9 or -1.");
+" Compression level, in 0-9 or -1.\n"
+" wbits\n"
+" The window buffer size and container format.");
#define ZLIB_COMPRESS_METHODDEF \
{"compress", (PyCFunction)(void(*)(void))zlib_compress, METH_FASTCALL|METH_KEYWORDS, zlib_compress__doc__},
static PyObject *
-zlib_compress_impl(PyObject *module, Py_buffer *data, int level);
+zlib_compress_impl(PyObject *module, Py_buffer *data, int level, int wbits);
static PyObject *
zlib_compress(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
- static const char * const _keywords[] = {"", "level", NULL};
+ static const char * const _keywords[] = {"", "level", "wbits", NULL};
static _PyArg_Parser _parser = {NULL, _keywords, "compress", 0};
- PyObject *argsbuf[2];
+ PyObject *argsbuf[3];
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Py_buffer data = {NULL, NULL};
int level = Z_DEFAULT_COMPRESSION;
+ int wbits = MAX_WBITS;
- args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
if (!args) {
goto exit;
}
@@ -44,12 +47,21 @@ zlib_compress(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec
if (!noptargs) {
goto skip_optional_pos;
}
- level = _PyLong_AsInt(args[1]);
- if (level == -1 && PyErr_Occurred()) {
+ if (args[1]) {
+ level = _PyLong_AsInt(args[1]);
+ if (level == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ wbits = _PyLong_AsInt(args[2]);
+ if (wbits == -1 && PyErr_Occurred()) {
goto exit;
}
skip_optional_pos:
- return_value = zlib_compress_impl(module, &data, level);
+ return_value = zlib_compress_impl(module, &data, level, wbits);
exit:
/* Cleanup for data */
@@ -803,4 +815,4 @@ exit:
#ifndef ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
#define ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
#endif /* !defined(ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF) */
-/*[clinic end generated code: output=6736bae59fab268b input=a9049054013a1b77]*/
+/*[clinic end generated code: output=e3e8a6142ea045a7 input=a9049054013a1b77]*/
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index 3efb24a..27a6d9a 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -310,13 +310,15 @@ zlib.compress
/
level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Compression level, in 0-9 or -1.
+ wbits: int(c_default="MAX_WBITS") = MAX_WBITS
+ The window buffer size and container format.
Returns a bytes object containing compressed data.
[clinic start generated code]*/
static PyObject *
-zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
-/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
+zlib_compress_impl(PyObject *module, Py_buffer *data, int level, int wbits)
+/*[clinic end generated code: output=46bd152fadd66df2 input=c4d06ee5782a7e3f]*/
{
PyObject *RetVal;
int flush;
@@ -336,7 +338,8 @@ zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
zst.zalloc = PyZlib_Malloc;
zst.zfree = PyZlib_Free;
zst.next_in = ibuf;
- int err = deflateInit(&zst, level);
+ int err = deflateInit2(&zst, level, DEFLATED, wbits, DEF_MEM_LEVEL,
+ Z_DEFAULT_STRATEGY);
switch (err) {
case Z_OK: