Skip to content

Commit 392e350

Browse files
morottirmmancomeendebakpt
authored andcommitted
pythongh-91349: Adjust default compression level to 6 (down from 9) in gzip and tarfile (pythonGH-131470)
pythongh-91349: Adjust default compression level to 6 (down from 9) in gzip and tarfile It is the default level used by most compression tools and a better tradeoff between speed and performance. Co-authored-by: rmorotti <romain.morotti@man.com> Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
1 parent ef80843 commit 392e350

File tree

6 files changed

+31
-10
lines changed

6 files changed

+31
-10
lines changed

Doc/library/gzip.rst

Lines changed: 17 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`.
@@ -59,6 +59,11 @@ The module defines the following items:
5959
.. versionchanged:: 3.6
6060
Accepts a :term:`path-like object`.
6161

62+
.. versionchanged:: next
63+
The default compression level was reduced to 6 (down from 9).
64+
It is the default level used by most compression tools and a better
65+
tradeoff between speed and performance.
66+
6267
.. exception:: BadGzipFile
6368

6469
An exception raised for invalid gzip files. It inherits from :exc:`OSError`.
@@ -67,7 +72,7 @@ The module defines the following items:
6772

6873
.. versionadded:: 3.8
6974

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

7277
Constructor for the :class:`GzipFile` class, which simulates most of the
7378
methods of a :term:`file object`, with the exception of the :meth:`~io.IOBase.truncate`
@@ -181,8 +186,13 @@ The module defines the following items:
181186
Remove the ``filename`` attribute, use the :attr:`~GzipFile.name`
182187
attribute instead.
183188

189+
.. versionchanged:: next
190+
The default compression level was reduced to 6 (down from 9).
191+
It is the default level used by most compression tools and a better
192+
tradeoff between speed and performance.
193+
184194

185-
.. function:: compress(data, compresslevel=9, *, mtime=0)
195+
.. function:: compress(data, compresslevel=6, *, mtime=0)
186196

187197
Compress the *data*, returning a :class:`bytes` object containing
188198
the compressed data. *compresslevel* and *mtime* have the same meaning as in
@@ -206,6 +216,10 @@ The module defines the following items:
206216
The *mtime* parameter now defaults to 0 for reproducible output.
207217
For the previous behaviour of using the current time,
208218
pass ``None`` to *mtime*.
219+
.. versionchanged:: next
220+
The default compression level was reduced to 6 (down from 9).
221+
It is the default level used by most compression tools and a better
222+
tradeoff between speed and performance.
209223

210224
.. function:: decompress(data)
211225

Doc/library/tarfile.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Some facts and figures:
123123

124124
For modes ``'w:gz'``, ``'x:gz'``, ``'w|gz'``, ``'w:bz2'``, ``'x:bz2'``,
125125
``'w|bz2'``, :func:`tarfile.open` accepts the keyword argument
126-
*compresslevel* (default ``9``) to specify the compression level of the file.
126+
*compresslevel* (default ``6``) to specify the compression level of the file.
127127

128128
For modes ``'w:xz'``, ``'x:xz'`` and ``'w|xz'``, :func:`tarfile.open` accepts the
129129
keyword argument *preset* to specify the compression level of the file.
@@ -198,6 +198,10 @@ Some facts and figures:
198198
.. versionchanged:: 3.14
199199
The *preset* keyword argument also works for streams.
200200

201+
.. versionchanged:: next
202+
The default compression level was reduced to 6 (down from 9).
203+
It is the default level used by most compression tools and a better
204+
tradeoff between speed and performance.
201205

202206
.. class:: TarFile
203207
:noindex:

Lib/gzip.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
_WRITE_BUFFER_SIZE = 4 * io.DEFAULT_BUFFER_SIZE
3131

3232

33-
def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_BEST,
33+
def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_TRADEOFF,
3434
encoding=None, errors=None, newline=None):
3535
"""Open a gzip-compressed file in binary or text mode.
3636
@@ -158,7 +158,7 @@ class GzipFile(_streams.BaseStream):
158158
myfileobj = None
159159

160160
def __init__(self, filename=None, mode=None,
161-
compresslevel=_COMPRESS_LEVEL_BEST, fileobj=None, mtime=None):
161+
compresslevel=_COMPRESS_LEVEL_TRADEOFF, fileobj=None, mtime=None):
162162
"""Constructor for the GzipFile class.
163163
164164
At least one of fileobj and filename must be given a
@@ -621,7 +621,7 @@ def _rewind(self):
621621
self._new_member = True
622622

623623

624-
def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=0):
624+
def compress(data, compresslevel=_COMPRESS_LEVEL_TRADEOFF, *, mtime=0):
625625
"""Compress data in one shot and return the compressed string.
626626
627627
compresslevel sets the compression level in range of 0-9.

Lib/tarfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,7 +1927,7 @@ def not_compressed(comptype):
19271927
if "preset" in kwargs and comptype not in ("xz",):
19281928
raise ValueError("preset is only valid for w|xz mode")
19291929

1930-
compresslevel = kwargs.pop("compresslevel", 9)
1930+
compresslevel = kwargs.pop("compresslevel", 6)
19311931
preset = kwargs.pop("preset", None)
19321932
stream = _Stream(name, filemode, comptype, fileobj, bufsize,
19331933
compresslevel, preset)
@@ -1953,7 +1953,7 @@ def taropen(cls, name, mode="r", fileobj=None, **kwargs):
19531953
return cls(name, mode, fileobj, **kwargs)
19541954

19551955
@classmethod
1956-
def gzopen(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
1956+
def gzopen(cls, name, mode="r", fileobj=None, compresslevel=6, **kwargs):
19571957
"""Open gzip compressed tar archive name for reading or writing.
19581958
Appending is not allowed.
19591959
"""

Lib/test/test_gzip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def test_mtime(self):
353353
def test_metadata(self):
354354
mtime = 123456789
355355

356-
with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
356+
with gzip.GzipFile(self.filename, 'w', mtime = mtime, compresslevel = 9) as fWrite:
357357
fWrite.write(data1)
358358

359359
with open(self.filename, 'rb') as fRead:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Adjust default ``compressionlevel=`` to 6 (down from 9) in :mod:`gzip` and :mod:`tarfile`.
2+
It is the default level used by most compression tools and a better
3+
tradeoff between speed and performance.

0 commit comments

Comments
 (0)