Patchwork [2,of,8,"] compression: accept level management for zlib compression

login
register
mail settings
Submitter Pierre-Yves David
Date March 31, 2019, 3:36 p.m.
Message ID <0779dd6ec612bf7dcb5c.1554046578@nodosa.octopoid.net>
Download mbox | patch
Permalink /patch/39421/
State Accepted
Headers show

Comments

Pierre-Yves David - March 31, 2019, 3:36 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@octobus.net>
# Date 1553711650 -3600
#      Wed Mar 27 19:34:10 2019 +0100
# Node ID 0779dd6ec612bf7dcb5ca4628b42409dad2cde29
# Parent  3e6081d58f0e3650f76515b9ee0547af3524fe4d
# EXP-Topic zstd-revlog
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 0779dd6ec612
compression: accept level management for zlib compression

We update the zlib related class to be support setting the compression level.
This changeset focus on updating the internal only. A way to configure this
level will be introduced in the next changeset.

Patch

diff --git a/mercurial/utils/compression.py b/mercurial/utils/compression.py
--- a/mercurial/utils/compression.py
+++ b/mercurial/utils/compression.py
@@ -456,6 +456,10 @@  class _zlibengine(compressionengine):
         return _GzipCompressedStreamReader(fh)
 
     class zlibrevlogcompressor(object):
+
+        def __init__(self, level=None):
+            self._level = level
+
         def compress(self, data):
             insize = len(data)
             # Caller handles empty input case.
@@ -465,7 +469,10 @@  class _zlibengine(compressionengine):
                 return None
 
             elif insize <= 1000000:
-                compressed = zlib.compress(data)
+                if self._level is None:
+                    compressed = zlib.compress(data)
+                else:
+                    compressed = zlib.compress(data, self._level)
                 if len(compressed) < insize:
                     return compressed
                 return None
@@ -474,7 +481,10 @@  class _zlibengine(compressionengine):
             # memory usage for large inputs. So do streaming compression
             # on large inputs.
             else:
-                z = zlib.compressobj()
+                if self._level is None:
+                    z = zlib.compressobj()
+                else:
+                    z = zlib.compressobj(level=self._level)
                 parts = []
                 pos = 0
                 while pos < insize: