Skip to content

Commit b17e21d

Browse files
committed
gh-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.
1 parent c2f2fd4 commit b17e21d

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed

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`
@@ -182,7 +182,7 @@ The module defines the following items:
182182
attribute instead.
183183

184184

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

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

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 compression level to 6 (down from 9) in gzip and 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)