| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a71e32c commit 3be4530
9 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -463,6 +463,7 @@ struct _Py_global_strings { | |||
| 463 | 463 | STRUCT_FOR_ID(globals) | |
| 464 | 464 | STRUCT_FOR_ID(groupindex) | |
| 465 | 465 | STRUCT_FOR_ID(groups) | |
| 466 | + STRUCT_FOR_ID(gzip_trailer) | ||
| 466 | 467 | STRUCT_FOR_ID(h) | |
| 467 | 468 | STRUCT_FOR_ID(handle) | |
| 468 | 469 | STRUCT_FOR_ID(handle_seq) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -221,7 +221,8 @@ def __init__(self, filename=None, mode=None, | |||
| 221 | 221 | zlib.DEFLATED, | |
| 222 | 222 | -zlib.MAX_WBITS, | |
| 223 | 223 | zlib.DEF_MEM_LEVEL, | |
| 224 | - 0) | ||
| 224 | + 0, | ||
| 225 | + gzip_trailer=True) | ||
| 225 | 226 | self._write_mtime = mtime | |
| 226 | 227 | self._buffer_size = _WRITE_BUFFER_SIZE | |
| 227 | 228 | self._buffer = io.BufferedWriter(_WriteBufferStream(self), | |
@@ -245,8 +246,6 @@ def __repr__(self): | |||
| 245 | 246 | ||
| 246 | 247 | def _init_write(self, filename): | |
| 247 | 248 | self.name = filename | |
| 248 | - self.crc = zlib.crc32(b"") | ||
| 249 | - self.size = 0 | ||
| 250 | 249 | self.writebuf = [] | |
| 251 | 250 | self.bufsize = 0 | |
| 252 | 251 | self.offset = 0 # Current file offset for seek(), tell(), etc | |
@@ -310,8 +309,6 @@ def _write_raw(self, data): | |||
| 310 | 309 | ||
| 311 | 310 | if length > 0: | |
| 312 | 311 | self.fileobj.write(self.compress.compress(data)) | |
| 313 | - self.size += length | ||
| 314 | - self.crc = zlib.crc32(data, self.crc) | ||
| 315 | 312 | self.offset += length | |
| 316 | 313 | ||
| 317 | 314 | return length | |
@@ -355,9 +352,6 @@ def close(self): | |||
| 355 | 352 | if self.mode == WRITE: | |
| 356 | 353 | self._buffer.flush() | |
| 357 | 354 | fileobj.write(self.compress.flush()) | |
| 358 | - write32u(fileobj, self.crc) | ||
| 359 | - # self.size may exceed 2 GiB, or even 4 GiB | ||
| 360 | - write32u(fileobj, self.size & 0xffffffff) | ||
| 361 | 355 | elif self.mode == READ: | |
| 362 | 356 | self._buffer.close() | |
| 363 | 357 | finally: | |
@@ -611,10 +605,11 @@ def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None): | |||
| 611 | 605 | # This is faster and with less overhead. | |
| 612 | 606 | return zlib.compress(data, level=compresslevel, wbits=31) | |
| 613 | 607 | header = _create_simple_gzip_header(compresslevel, mtime) | |
| 614 | - trailer = struct.pack("<LL", zlib.crc32(data), (len(data) & 0xffffffff)) | ||
| 615 | - # Wbits=-15 creates a raw deflate block. | ||
| 616 | - return (header + zlib.compress(data, level=compresslevel, wbits=-15) + | ||
| 617 | - trailer) | ||
| 608 | + # Wbits=-15 creates a raw deflate block. Gzip_trailer=True computes CRC32 | ||
| 609 | + # and writes gzip trailer with zlib, which on some platforms is faster | ||
| 610 | + # than doing it manually. | ||
| 611 | + return (header + zlib.compress(data, level=compresslevel, wbits=-15, | ||
| 612 | + gzip_trailer=True)) | ||
| 618 | 613 | ||
| 619 | 614 | ||
| 620 | 615 | def decompress(data): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -370,7 +370,6 @@ def __init__(self, name, mode, comptype, fileobj, bufsize, | |||
| 370 | 370 | except ImportError: | |
| 371 | 371 | raise CompressionError("zlib module is not available") from None | |
| 372 | 372 | self.zlib = zlib | |
| 373 | - self.crc = zlib.crc32(b"") | ||
| 374 | 373 | if mode == "r": | |
| 375 | 374 | self.exception = zlib.error | |
| 376 | 375 | self._init_read_gz() | |
@@ -421,7 +420,8 @@ def _init_write_gz(self, compresslevel): | |||
| 421 | 420 | self.zlib.DEFLATED, | |
| 422 | 421 | -self.zlib.MAX_WBITS, | |
| 423 | 422 | self.zlib.DEF_MEM_LEVEL, | |
| 424 | - 0) | ||
| 423 | + 0, | ||
| 424 | + gzip_trailer=True) | ||
| 425 | 425 | timestamp = struct.pack("<L", int(time.time())) | |
| 426 | 426 | self.__write(b"\037\213\010\010" + timestamp + b"\002\377") | |
| 427 | 427 | if self.name.endswith(".gz"): | |
@@ -434,8 +434,6 @@ def _init_write_gz(self, compresslevel): | |||
| 434 | 434 | def write(self, s): | |
| 435 | 435 | """Write string s to the stream. | |
| 436 | 436 | """ | |
| 437 | - if self.comptype == "gz": | ||
| 438 | - self.crc = self.zlib.crc32(s, self.crc) | ||
| 439 | 437 | self.pos += len(s) | |
| 440 | 438 | if self.comptype != "tar": | |
| 441 | 439 | s = self.cmp.compress(s) | |
@@ -465,9 +463,6 @@ def close(self): | |||
| 465 | 463 | if self.mode == "w" and self.buf: | |
| 466 | 464 | self.fileobj.write(self.buf) | |
| 467 | 465 | self.buf = b"" | |
| 468 | - if self.comptype == "gz": | ||
| 469 | - self.fileobj.write(struct.pack("<L", self.crc)) | ||
| 470 | - self.fileobj.write(struct.pack("<L", self.pos & 0xffffFFFF)) | ||
| 471 | 466 | finally: | |
| 472 | 467 | if not self._extfileobj: | |
| 473 | 468 | self.fileobj.close() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + Write gzip trailer with zlib, improving gzip compression performance on s390x by roughly 40%. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments