Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/google-crc32c/src/google_crc32c/_crc32c.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
#include <Python.h>
#include <crc32c/crc32c.h>

/* The minimum buffer size in bytes (1MB) required to justify the overhead of releasing the GIL. */
static const Py_ssize_t gil_threshold = 1024 * 1024;

static int
_should_release_gil(Py_ssize_t length, PyObject *chunk_obj)
{
/* Checks if the chunk is immutable (bytes) to prevent concurrent modification,
* and large enough to benefit from releasing the GIL. */
return (length >= gil_threshold && PyBytes_Check(chunk_obj));
}

static PyObject *
_crc32c_extend(PyObject *self, PyObject *args)
Expand All @@ -10,12 +20,21 @@ _crc32c_extend(PyObject *self, PyObject *args)
uint32_t crc;
const char *chunk;
Py_ssize_t length;
PyThreadState *save = NULL;

if (!PyArg_ParseTuple(args, "ky#", &crc_input, &chunk, &length))
return NULL;

if (_should_release_gil(length, PyTuple_GET_ITEM(args, 1))) {
save = PyEval_SaveThread();
}

crc = crc32c_extend((uint32_t)crc_input, (const uint8_t*)chunk, length);

if (save) {
PyEval_RestoreThread(save);
}

return PyLong_FromUnsignedLong(crc);
}

Expand All @@ -26,12 +45,21 @@ _crc32c_value(PyObject *self, PyObject *args)
uint32_t crc;
const char *chunk;
Py_ssize_t length;
PyThreadState *save = NULL;

if (!PyArg_ParseTuple(args, "y#", &chunk, &length))
return NULL;

if (_should_release_gil(length, PyTuple_GET_ITEM(args, 0))) {
save = PyEval_SaveThread();
}

crc = crc32c_value((const uint8_t*)chunk, length);

if (save) {
PyEval_RestoreThread(save);
}

return PyLong_FromUnsignedLong(crc);
}

Expand Down
Loading