Skip to content

Commit 4d3116a

Browse files
committed
Do not drop GIL for small values
1 parent 1a032f6 commit 4d3116a

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/isal/isal_zlibmodule.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,15 @@ isal_zlib_adler32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
174174
return NULL;
175175
}
176176
}
177-
Py_BEGIN_ALLOW_THREADS
178-
value = isal_adler32(value, data.buf, (uint64_t)data.len);
179-
Py_END_ALLOW_THREADS
177+
uint64_t buffer_length = (uint64_t)data.len;
178+
/* Do not drop GIL for small values as it is too much overhead */
179+
if (buffer_length > 5 * 1024) {
180+
Py_BEGIN_ALLOW_THREADS
181+
value = isal_adler32(value, data.buf, buffer_length);
182+
Py_END_ALLOW_THREADS
183+
} else {
184+
value = isal_adler32(value, data.buf, buffer_length);
185+
}
180186
return_value = PyLong_FromUnsignedLong(value & 0xffffffffU);
181187
PyBuffer_Release(&data);
182188
return return_value;
@@ -221,9 +227,15 @@ isal_zlib_crc32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
221227
return NULL;
222228
}
223229
}
224-
Py_BEGIN_ALLOW_THREADS
225-
value = crc32_gzip_refl(value, data.buf, (uint64_t)data.len);
226-
Py_END_ALLOW_THREADS
230+
uint64_t buffer_length = (uint64_t)data.len;
231+
/* Do not drop GIL for small values as it is too much overhead */
232+
if (buffer_length > 5 * 1024) {
233+
Py_BEGIN_ALLOW_THREADS
234+
value = crc32_gzip_refl(value, data.buf, buffer_length);
235+
Py_END_ALLOW_THREADS
236+
} else {
237+
value = crc32_gzip_refl(value, data.buf, buffer_length);
238+
}
227239
return_value = PyLong_FromUnsignedLong(value & 0xffffffffU);
228240
PyBuffer_Release(&data);
229241
return return_value;

0 commit comments

Comments
 (0)