Skip to content

Commit 128619b

Browse files
committed
Do not release GIL on small values to prevent overhead costs
1 parent e3df5a9 commit 128619b

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

src/zlib_ng/zlib_ngmodule.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,14 +1466,21 @@ zlib_adler32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
14661466

14671467
Py_ssize_t len = data.len ;
14681468
uint8_t *buf = data.buf;
1469-
Py_BEGIN_ALLOW_THREADS
1470-
while ((size_t)len > UINT32_MAX) {
1471-
value = zng_adler32(value, buf, UINT32_MAX);
1472-
buf += (size_t) UINT32_MAX;
1473-
len -= (size_t) UINT32_MAX;
1474-
}
1475-
value = zng_adler32(value, buf, (uint32_t)len);
1476-
Py_END_ALLOW_THREADS
1469+
1470+
/* Do not drop GIL for small values as it increases overhead */
1471+
if (len > 1024 * 5) {
1472+
Py_BEGIN_ALLOW_THREADS
1473+
while ((size_t)len > UINT32_MAX) {
1474+
value = zng_adler32(value, buf, UINT32_MAX);
1475+
buf += (size_t) UINT32_MAX;
1476+
len -= (size_t) UINT32_MAX;
1477+
}
1478+
value = zng_adler32(value, buf, (uint32_t)len);
1479+
Py_END_ALLOW_THREADS
1480+
} else {
1481+
value = zng_adler32(value, buf, (uint32_t)len);
1482+
}
1483+
14771484
return_value = PyLong_FromUnsignedLong(value & 0xffffffffU);
14781485
PyBuffer_Release(&data);
14791486
return return_value;
@@ -1521,14 +1528,21 @@ zlib_crc32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
15211528

15221529
Py_ssize_t len = data.len ;
15231530
uint8_t *buf = data.buf;
1524-
Py_BEGIN_ALLOW_THREADS
1525-
while ((size_t)len > UINT32_MAX) {
1526-
value = zng_crc32(value, buf, UINT32_MAX);
1527-
buf += (size_t) UINT32_MAX;
1528-
len -= (size_t) UINT32_MAX;
1529-
}
1530-
value = zng_crc32(value, buf, (uint32_t)len);
1531-
Py_END_ALLOW_THREADS
1531+
1532+
/* Do not drop GIL for small values as it increases overhead */
1533+
if (len > 1024 * 5) {
1534+
Py_BEGIN_ALLOW_THREADS
1535+
while ((size_t)len > UINT32_MAX) {
1536+
value = zng_crc32(value, buf, UINT32_MAX);
1537+
buf += (size_t) UINT32_MAX;
1538+
len -= (size_t) UINT32_MAX;
1539+
}
1540+
value = zng_crc32(value, buf, (uint32_t)len);
1541+
Py_END_ALLOW_THREADS
1542+
} else {
1543+
value = zng_crc32(value, buf, (uint32_t)len);
1544+
}
1545+
15321546
return_value = PyLong_FromUnsignedLong(value & 0xffffffffU);
15331547
PyBuffer_Release(&data);
15341548
return return_value;

0 commit comments

Comments
 (0)