Skip to content

Commit 49ab675

Browse files
committed
Make block size configurable
1 parent 1278c7b commit 49ab675

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/isal/igzip_threaded.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121

2222
def open(filename, mode="rb", compresslevel=igzip._COMPRESS_LEVEL_TRADEOFF,
23-
encoding=None, errors=None, newline=None, *, threads=1):
23+
encoding=None, errors=None, newline=None, *, threads=1,
24+
block_size=1024 * 1024):
2425
"""
2526
Utilize threads to read and write gzip objects and escape the GIL.
2627
Comparable to gzip.open. This method is only usable for streamed reading
@@ -39,6 +40,8 @@ def open(filename, mode="rb", compresslevel=igzip._COMPRESS_LEVEL_TRADEOFF,
3940
:param threads: If 0 will defer to igzip.open, if < 0 will use all threads
4041
available to the system. Reading gzip can only
4142
use one thread.
43+
:param block_size: Determines how large the blocks in the read/write
44+
queues are for threaded reading and writing.
4245
:return: An io.BufferedReader, io.BufferedWriter, or io.TextIOWrapper,
4346
depending on the mode.
4447
"""
@@ -61,20 +64,21 @@ def open(filename, mode="rb", compresslevel=igzip._COMPRESS_LEVEL_TRADEOFF,
6164
else:
6265
raise TypeError("filename must be a str or bytes object, or a file")
6366
if "r" in mode:
64-
gzip_file = io.BufferedReader(_ThreadedGzipReader(binary_file))
67+
gzip_file = io.BufferedReader(
68+
_ThreadedGzipReader(binary_file, block_size=block_size))
6569
else:
66-
write_buffer_size = 1024 * 1024
6770
# Deflating random data results in an output a little larger than the
6871
# input. Making the output buffer 10% larger is sufficient overkill.
69-
compress_buffer_size = write_buffer_size + max(
70-
write_buffer_size // 10, 500)
72+
compress_buffer_size = block_size + max(
73+
block_size // 10, 500)
7174
gzip_file = io.BufferedWriter(
7275
_ThreadedGzipWriter(
7376
fp=binary_file,
7477
buffer_size=compress_buffer_size,
7578
level=compresslevel,
76-
threads=threads),
77-
buffer_size=write_buffer_size
79+
threads=threads
80+
),
81+
buffer_size=block_size
7882
)
7983
if "t" in mode:
8084
return io.TextIOWrapper(gzip_file, encoding, errors, newline)
@@ -84,7 +88,7 @@ def open(filename, mode="rb", compresslevel=igzip._COMPRESS_LEVEL_TRADEOFF,
8488
class _ThreadedGzipReader(io.RawIOBase):
8589
def __init__(self, fp, queue_size=2, block_size=1024 * 1024):
8690
self.raw = fp
87-
self.fileobj = igzip._IGzipReader(fp, buffersize=8 * 1024 * 1024)
91+
self.fileobj = igzip._IGzipReader(fp, buffersize=8 * block_size)
8892
self.pos = 0
8993
self.read_file = False
9094
self.queue = queue.Queue(queue_size)

0 commit comments

Comments
 (0)