Skip to content

Commit 5cb08f2

Browse files
committed
Use arrange input buffer
1 parent 84e506a commit 5cb08f2

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

src/isal/isal_zlib.pyx

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ cdef Py_ssize_t Py_ssize_t_min(Py_ssize_t a, Py_ssize_t b):
9494
else:
9595
return b
9696

97+
ctypedef fused stream_or_state:
98+
isal_zstream
99+
inflate_state
100+
101+
cdef unsigned long unsigned_long_min(unsigned long a, unsigned long b):
102+
if a <= b:
103+
return a
104+
else:
105+
return b
106+
107+
cdef void arrange_input_buffer(stream_or_state *stream, Py_ssize_t *remains):
108+
stream.avail_in = unsigned_long_min(<unsigned long>remains[0], UINT32_MAX)
109+
remains[0] -= stream.avail_in
110+
97111
cpdef bytes compress(data,
98112
int level=ISAL_DEFAULT_COMPRESSION_I,
99113
int wbits = ISAL_DEF_MAX_HIST_BITS):
@@ -118,12 +132,9 @@ cpdef bytes compress(data,
118132
out = []
119133

120134
# initialise input
121-
cdef Py_ssize_t max_input_buffer = UINT32_MAX
122-
cdef Py_ssize_t total_length = len(data)
123-
cdef Py_ssize_t remains = total_length
124-
cdef Py_ssize_t ibuflen = total_length
125-
cdef Py_ssize_t position = 0
126-
cdef bytes ibuf
135+
cdef Py_ssize_t ibuflen = len(data)
136+
cdef unsigned char * ibuf = data
137+
stream.next_in = ibuf
127138

128139
# initialise helper variables
129140
cdef int err
@@ -134,12 +145,7 @@ cpdef bytes compress(data,
134145
# This loop runs n times (at least twice). n-1 times to fill the input
135146
# buffer with data. The nth time the input is empty. In that case
136147
# stream.flush is set to FULL_FLUSH and the end_of_stream is activated.
137-
ibuflen = Py_ssize_t_min(remains, max_input_buffer)
138-
ibuf = data[position: position + ibuflen]
139-
position += ibuflen
140-
stream.next_in = ibuf
141-
remains -= ibuflen
142-
stream.avail_in = ibuflen
148+
arrange_input_buffer(&stream, &ibuflen)
143149
if ibuflen == 0:
144150
stream.flush = FULL_FLUSH
145151
stream.end_of_stream = 1
@@ -184,12 +190,9 @@ cpdef decompress(data,
184190
&stream.crc_flag)
185191

186192
# initialise input
187-
cdef Py_ssize_t max_input_buffer = UINT32_MAX
188-
cdef Py_ssize_t total_length = len(data)
189-
cdef Py_ssize_t remains = total_length
190-
cdef Py_ssize_t ibuflen = total_length
191-
cdef Py_ssize_t position = 0
192-
cdef bytes ibuf
193+
cdef Py_ssize_t ibuflen = len(data)
194+
cdef unsigned char * ibuf = data
195+
stream.next_in = ibuf
193196

194197
# Initialise output buffer
195198
cdef unsigned long obuflen = bufsize
@@ -199,13 +202,8 @@ cpdef decompress(data,
199202

200203
# Implementation imitated from CPython's zlibmodule.c
201204
try:
202-
while ibuflen != 0 and stream.block_state != ISAL_BLOCK_FINISH:
203-
ibuflen = Py_ssize_t_min(remains, max_input_buffer)
204-
ibuf = data[position: position + ibuflen]
205-
position += ibuflen
206-
stream.next_in = ibuf
207-
remains -= ibuflen
208-
stream.avail_in = ibuflen
205+
while ibuflen != 0 or stream.block_state != ISAL_BLOCK_FINISH:
206+
arrange_input_buffer(&stream, &ibuflen)
209207

210208
# This loop reads all the input bytes. The check is at the end,
211209
# because when the block state is not at FINISH, the function needs

0 commit comments

Comments
 (0)