Skip to content

Commit 0184adb

Browse files
committed
More fine grained GIL escaping to benefit bgzip blocks
1 parent f486e2b commit 0184adb

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/isal/isal_zlibmodule.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,11 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
14411441
}
14421442
Py_ssize_t bytes_written = 0;
14431443
while (1) {
1444+
/* Allow escaping the GIL except when updating the buffer or when
1445+
throwing errors. This makes a big difference for BGZF format gzip
1446+
blocks. */
1447+
PyThreadState *_save;
1448+
Py_UNBLOCK_THREADS
14441449
uint8_t *current_pos = self->current_pos;
14451450
uint8_t *buffer_end = self->buffer_end;
14461451
switch(self->stream_phase) {
@@ -1452,6 +1457,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
14521457
// Reached EOF
14531458
self->_size = self->_pos;
14541459
self->current_pos = current_pos;
1460+
Py_BLOCK_THREADS;
14551461
return bytes_written;
14561462
}
14571463
if ((remaining) < 10) {
@@ -1461,13 +1467,15 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
14611467
uint8_t magic2 = current_pos[1];
14621468

14631469
if (!(magic1 == 0x1f && magic2 == 0x8b)) {
1464-
PyErr_Format(BadGzipFile,
1470+
Py_BLOCK_THREADS;
1471+
PyErr_Format(BadGzipFile,
14651472
"Not a gzipped file (%R)",
14661473
PyBytes_FromStringAndSize((char *)current_pos, 2));
14671474
return -1;
14681475
};
14691476
uint8_t method = current_pos[2];
14701477
if (method != 8) {
1478+
Py_BLOCK_THREADS;
14711479
PyErr_SetString(BadGzipFile, "Unknown compression method");
14721480
return -1;
14731481
}
@@ -1511,6 +1519,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
15111519
uint16_t crc = crc32_gzip_refl(
15121520
0, current_pos, header_cursor - current_pos) & 0xFFFF;
15131521
if (header_crc != crc) {
1522+
Py_BLOCK_THREADS;
15141523
PyErr_Format(
15151524
BadGzipFile,
15161525
"Corrupted gzip header. Checksums do not "
@@ -1530,10 +1539,9 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
15301539
self->state.next_out = out_buffer;
15311540
self->state.avail_out = out_buffer_size;
15321541
int ret;
1533-
Py_BEGIN_ALLOW_THREADS
15341542
ret = isal_inflate(&self->state);
1535-
Py_END_ALLOW_THREADS
15361543
if (ret != ISAL_DECOMP_OK) {
1544+
Py_BLOCK_THREADS;
15371545
isal_inflate_error(ret);
15381546
return -1;
15391547
}
@@ -1548,6 +1556,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
15481556
break;
15491557
}
15501558
self->current_pos = current_pos;
1559+
Py_BLOCK_THREADS;
15511560
return bytes_written;
15521561
}
15531562
current_pos -= bitbuffer_size(&self->state);
@@ -1560,6 +1569,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
15601569
uint32_t crc = *(uint32_t *)current_pos;
15611570
current_pos += 4;
15621571
if (crc != self->state.crc) {
1572+
Py_BLOCK_THREADS;
15631573
PyErr_Format(
15641574
BadGzipFile,
15651575
"CRC check failed %u != %u",
@@ -1570,6 +1580,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
15701580
uint32_t length = *(uint32_t *)current_pos;
15711581
current_pos += 4;
15721582
if (length != self->state.total_out) {
1583+
Py_BLOCK_THREADS;
15731584
PyErr_SetString(BadGzipFile, "Incorrect length of data produced");
15741585
return -1;
15751586
}
@@ -1590,6 +1601,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
15901601
default:
15911602
Py_UNREACHABLE();
15921603
}
1604+
Py_BLOCK_THREADS;
15931605
// If buffer_end is reached, nothing was returned and all bytes are
15941606
// read we have an EOFError.
15951607
if (self->all_bytes_read) {

0 commit comments

Comments
 (0)