Skip to content

Commit

Permalink
Merge pull request #227 from jeandet/lib_deflate
Browse files Browse the repository at this point in the history
Adds automatic support for libdeflate which yields at least a 2x speedup
  • Loading branch information
dstansby committed Jul 28, 2023
2 parents b44282a + 282056a commit dad8076
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
8 changes: 4 additions & 4 deletions cdflib/cdfread.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import gzip
from .gzip_wrapper import gzip_inflate
import hashlib
import io
import os
Expand Down Expand Up @@ -579,7 +579,7 @@ def _uncompress_file(self) -> None:

if cType == 5:
self._f.seek(data_start)
decompressed_data = gzip.decompress(self._f.read(data_size))
decompressed_data = gzip_inflate(self._f.read(data_size))
elif cType == 1:
self._f.seek(data_start)
decompressed_data = self._uncompress_rle(self._f.read(data_size))
Expand Down Expand Up @@ -1982,7 +1982,7 @@ def _read_vvr_block(self, offset: int) -> bytes:
if section_type == 13:
# a CVVR
compressed_size = int.from_bytes(block[8:16], "big")
return gzip.decompress(block[16 : 16 + compressed_size])
return gzip_inflate(block[16 : 16 + compressed_size])
elif section_type == 7:
# a VVR
return block[4:]
Expand All @@ -2001,7 +2001,7 @@ def _read_vvr_block2(self, offset: int) -> bytes:
if section_type == 13:
# a CVVR
compressed_size = int.from_bytes(block[8:12], "big")
return gzip.decompress(block[12 : 12 + compressed_size])
return gzip_inflate(block[12 : 12 + compressed_size])
elif section_type == 7:
# a VVR
return block[4:]
Expand Down
6 changes: 3 additions & 3 deletions cdflib/cdfwrite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import binascii
import gzip
from .gzip_wrapper import gzip_deflate
import hashlib
import io
import logging
Expand Down Expand Up @@ -1031,7 +1031,7 @@ def _write_var_data_nonsparse(
endrec = recs - 1
endloc = len(data)
bdata = data[startloc:endloc]
cdata = gzip.compress(bdata, compression)
cdata = gzip_deflate(bdata, compression)
if len(cdata) < len(bdata):
n1offset = self._write_cvvr(f, cdata)
else:
Expand Down Expand Up @@ -2065,7 +2065,7 @@ def _write_ccr(self, f: io.BufferedWriter, g: io.BufferedWriter, level: int) ->
uSize = len(data)
section_type = self.CCR_
rfuA = 0
cData = gzip.compress(data, level)
cData = gzip_deflate(data, level)
block_size = self.CCR_BASE_SIZE64 + len(cData)
cprOffset = 0
ccr1 = bytearray(32)
Expand Down
7 changes: 7 additions & 0 deletions cdflib/gzip_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
try:
from deflate import gzip_decompress as gzip_inflate
from deflate import gzip_compress as gzip_deflate

except ImportError:
from gzip import decompress as gzip_inflate
from gzip import compress as gzip_deflate

0 comments on commit dad8076

Please sign in to comment.