Bug 1317041 - Calculate crc32 incrementally; r=gps
authorMarco Castelluccio <mcastelluccio@mozilla.com>
Tue, 07 Nov 2017 15:34:20 -0800
changeset 443906 f47cb574c96e39ecda7bdcdac10ed97afb651f02
parent 443905 0754603ee7b055980f52bf1b26978df222ec96b4
child 443907 dd834d38eaabe22f056d3eb37304f43f37edeb35
push id1618
push userCallek@gmail.com
push dateThu, 11 Jan 2018 17:45:48 +0000
treeherdermozilla-release@882ca853e05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgps
bugs1317041
milestone58.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1317041 - Calculate crc32 incrementally; r=gps
python/mozbuild/mozpack/mozjar.py
python/mozbuild/mozpack/test/test_mozjar.py
--- a/python/mozbuild/mozpack/mozjar.py
+++ b/python/mozbuild/mozpack/mozjar.py
@@ -696,30 +696,35 @@ class Deflater(object):
                 self._deflater = zlib.compressobj(
                     compress_level, zlib.DEFLATED, -MAX_WBITS)
             else:
                 self._deflater = BrotliCompress()
             self._deflated = BytesIO()
         else:
             assert compress == JAR_STORED
             self._deflater = None
+        self.crc32 = 0
 
     def write(self, data):
         '''
         Append a buffer to the Deflater.
         '''
         self._data.write(data)
+
+        if isinstance(data, memoryview):
+            data = data.tobytes()
+
         if self.compress:
             if self._deflater:
-                if isinstance(data, memoryview):
-                    data = data.tobytes()
                 self._deflated.write(self._deflater.compress(data))
             else:
                 raise JarWriterError("Can't write after flush")
 
+        self.crc32 = zlib.crc32(data, self.crc32) & 0xffffffff
+
     def close(self):
         '''
         Close the Deflater.
         '''
         self._data.close()
         if self.compress:
             self._deflated.close()
 
@@ -767,23 +772,16 @@ class Deflater(object):
     @property
     def uncompressed_size(self):
         '''
         Return the size of the data written to the Deflater.
         '''
         return self._data.tell()
 
     @property
-    def crc32(self):
-        '''
-        Return the crc32 of the data written to the Deflater.
-        '''
-        return zlib.crc32(self._data.getvalue()) & 0xffffffff
-
-    @property
     def compressed_data(self):
         '''
         Return the compressed data, if the data should be compressed (real
         compressed size smaller than the uncompressed size), or the
         uncompressed data otherwise.
         '''
         if self.compressed:
             return self._deflated.getvalue()
--- a/python/mozbuild/mozpack/test/test_mozjar.py
+++ b/python/mozbuild/mozpack/test/test_mozjar.py
@@ -119,16 +119,24 @@ class TestDeflater(unittest.TestCase):
                             deflater.uncompressed_size)
         self.assertEqual(deflater.crc32, 0xd46b97ed)
         # The CRC is the same as when not compressed
         deflater = Deflater(False)
         self.assertFalse(deflater.compressed)
         deflater.write(self.wrap('aaaaaaaaaaaaanopqrstuvwxyz'))
         self.assertEqual(deflater.crc32, 0xd46b97ed)
 
+    def test_deflater_empty(self):
+        deflater = Deflater(False)
+        self.assertFalse(deflater.compressed)
+        self.assertEqual(deflater.uncompressed_size, 0)
+        self.assertEqual(deflater.compressed_size, deflater.uncompressed_size)
+        self.assertEqual(deflater.compressed_data, '')
+        self.assertEqual(deflater.crc32, 0)
+
 
 class TestDeflaterMemoryView(TestDeflater):
     def wrap(self, data):
         return memoryview(data)
 
 
 class TestJar(unittest.TestCase):
     optimize = False