Skip to content

Commit e9a4c3c

Browse files
committed
pythongh-91349: Adjust default compression level to 6 (down from 9) in gzip tarfile and
bzip. It is the default level used by most compression tools and a reasonable tradeoff between speed and performance. level 9 is very slow, in the order of 1 MB/s, vary with the machine, and was likely a major bottleneck for any software that forgot to specify the compression level.
1 parent 8ad4646 commit e9a4c3c

File tree

6 files changed

+18
-13
lines changed

6 files changed

+18
-13
lines changed

Doc/library/bz2.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The :mod:`bz2` module contains:
2929
(De)compression of files
3030
------------------------
3131

32-
.. function:: open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)
32+
.. function:: open(filename, mode='rb', compresslevel=6, encoding=None, errors=None, newline=None)
3333

3434
Open a bzip2-compressed file in binary or text mode, returning a :term:`file
3535
object`.
@@ -63,7 +63,7 @@ The :mod:`bz2` module contains:
6363
Accepts a :term:`path-like object`.
6464

6565

66-
.. class:: BZ2File(filename, mode='r', *, compresslevel=9)
66+
.. class:: BZ2File(filename, mode='r', *, compresslevel=6)
6767

6868
Open a bzip2-compressed file in binary mode.
6969

@@ -198,7 +198,7 @@ The :mod:`bz2` module contains:
198198
Incremental (de)compression
199199
---------------------------
200200

201-
.. class:: BZ2Compressor(compresslevel=9)
201+
.. class:: BZ2Compressor(compresslevel=6)
202202

203203
Create a new compressor object. This object may be used to compress data
204204
incrementally. For one-shot compression, use the :func:`compress` function
@@ -288,7 +288,7 @@ Incremental (de)compression
288288
One-shot (de)compression
289289
------------------------
290290

291-
.. function:: compress(data, compresslevel=9)
291+
.. function:: compress(data, compresslevel=6)
292292

293293
Compress *data*, a :term:`bytes-like object <bytes-like object>`.
294294

Doc/library/gzip.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Note that additional file formats which can be decompressed by the
2626
The module defines the following items:
2727

2828

29-
.. function:: open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)
29+
.. function:: open(filename, mode='rb', compresslevel=6, encoding=None, errors=None, newline=None)
3030

3131
Open a gzip-compressed file in binary or text mode, returning a :term:`file
3232
object`.
@@ -67,7 +67,7 @@ The module defines the following items:
6767

6868
.. versionadded:: 3.8
6969

70-
.. class:: GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)
70+
.. class:: GzipFile(filename=None, mode=None, compresslevel=6, fileobj=None, mtime=None)
7171

7272
Constructor for the :class:`GzipFile` class, which simulates most of the
7373
methods of a :term:`file object`, with the exception of the :meth:`~io.IOBase.truncate`
@@ -184,7 +184,7 @@ The module defines the following items:
184184
attribute instead.
185185

186186

187-
.. function:: compress(data, compresslevel=9, *, mtime=0)
187+
.. function:: compress(data, compresslevel=6, *, mtime=0)
188188

189189
Compress the *data*, returning a :class:`bytes` object containing
190190
the compressed data. *compresslevel* and *mtime* have the same meaning as in

Lib/bz2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class BZ2File(_compression.BaseStream):
3434
returned as bytes, and data to be written should be given as bytes.
3535
"""
3636

37-
def __init__(self, filename, mode="r", *, compresslevel=9):
37+
def __init__(self, filename, mode="r", *, compresslevel=6):
3838
"""Open a bzip2-compressed file.
3939
4040
If filename is a str, bytes, or PathLike object, it gives the
@@ -276,7 +276,7 @@ def tell(self):
276276
return self._pos
277277

278278

279-
def open(filename, mode="rb", compresslevel=9,
279+
def open(filename, mode="rb", compresslevel=6,
280280
encoding=None, errors=None, newline=None):
281281
"""Open a bzip2-compressed file in binary or text mode.
282282
@@ -318,7 +318,7 @@ def open(filename, mode="rb", compresslevel=9,
318318
return binary_file
319319

320320

321-
def compress(data, compresslevel=9):
321+
def compress(data, compresslevel=6):
322322
"""Compress a block of data.
323323
324324
compresslevel, if given, must be a number between 1 and 9.

Lib/tarfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,7 +1911,7 @@ def taropen(cls, name, mode="r", fileobj=None, **kwargs):
19111911
return cls(name, mode, fileobj, **kwargs)
19121912

19131913
@classmethod
1914-
def gzopen(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
1914+
def gzopen(cls, name, mode="r", fileobj=None, compresslevel=6, **kwargs):
19151915
"""Open gzip compressed tar archive name for reading or writing.
19161916
Appending is not allowed.
19171917
"""
@@ -1944,7 +1944,7 @@ def gzopen(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
19441944
return t
19451945

19461946
@classmethod
1947-
def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
1947+
def bz2open(cls, name, mode="r", fileobj=None, compresslevel=6, **kwargs):
19481948
"""Open bzip2 compressed tar archive name for reading or writing.
19491949
Appending is not allowed.
19501950
"""
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Adjust default compression level to 6 (down from 9) in gzip tarfile and
2+
bzip. It is the default level used by most compression tools and a
3+
reasonable tradeoff between speed and performance. level 9 is very slow, in
4+
the order of 1 MB/s, vary with the machine, and was likely a major
5+
bottleneck for any software that forgot to specify the compression level.

Modules/clinic/_bz2module.c.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)