From 58275c44c272bcc8eb6b4d78a0bf8561f948d48d Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Fri, 9 May 2025 20:30:18 +0100 Subject: [PATCH 1/2] zstd: remove empty_bytes from module state --- Modules/_zstd/_zstdmodule.c | 9 --------- Modules/_zstd/_zstdmodule.h | 2 -- Modules/_zstd/decompressor.c | 21 +++------------------ 3 files changed, 3 insertions(+), 29 deletions(-) diff --git a/Modules/_zstd/_zstdmodule.c b/Modules/_zstd/_zstdmodule.c index 4004bbb3461393..1200d200ae0995 100644 --- a/Modules/_zstd/_zstdmodule.c +++ b/Modules/_zstd/_zstdmodule.c @@ -587,11 +587,6 @@ do { \ _zstd_state* const mod_state = get_zstd_state(m); /* Reusable objects & variables */ - mod_state->empty_bytes = PyBytes_FromStringAndSize(NULL, 0); - if (mod_state->empty_bytes == NULL) { - return -1; - } - mod_state->CParameter_type = NULL; mod_state->DParameter_type = NULL; @@ -694,8 +689,6 @@ _zstd_traverse(PyObject *module, visitproc visit, void *arg) { _zstd_state* const mod_state = get_zstd_state(module); - Py_VISIT(mod_state->empty_bytes); - Py_VISIT(mod_state->ZstdDict_type); Py_VISIT(mod_state->ZstdCompressor_type); @@ -713,8 +706,6 @@ _zstd_clear(PyObject *module) { _zstd_state* const mod_state = get_zstd_state(module); - Py_CLEAR(mod_state->empty_bytes); - Py_CLEAR(mod_state->ZstdDict_type); Py_CLEAR(mod_state->ZstdCompressor_type); diff --git a/Modules/_zstd/_zstdmodule.h b/Modules/_zstd/_zstdmodule.h index 58622a5cb48b5e..80f4e7e58d5265 100644 --- a/Modules/_zstd/_zstdmodule.h +++ b/Modules/_zstd/_zstdmodule.h @@ -35,8 +35,6 @@ extern PyType_Spec zstd_compressor_type_spec; extern PyType_Spec zstd_decompressor_type_spec; struct _zstd_state { - PyObject *empty_bytes; - PyTypeObject *ZstdDict_type; PyTypeObject *ZstdCompressor_type; PyTypeObject *ZstdDecompressor_type; diff --git a/Modules/_zstd/decompressor.c b/Modules/_zstd/decompressor.c index d141e68efded26..1e4024ada0cb11 100644 --- a/Modules/_zstd/decompressor.c +++ b/Modules/_zstd/decompressor.c @@ -290,13 +290,7 @@ decompress_impl(ZstdDecompressor *self, ZSTD_inBuffer *in, /* The first AFE check for setting .at_frame_edge flag */ if (type == TYPE_ENDLESS_DECOMPRESSOR) { if (self->at_frame_edge && in->pos == in->size) { - _zstd_state* const mod_state = PyType_GetModuleState(Py_TYPE(self)); - if (mod_state == NULL) { - return NULL; - } - ret = mod_state->empty_bytes; - Py_INCREF(ret); - return ret; + return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES); } } @@ -747,16 +741,9 @@ _zstd_ZstdDecompressor_unused_data_get_impl(ZstdDecompressor *self) { PyObject *ret; - /* Thread-safe code */ - Py_BEGIN_CRITICAL_SECTION(self); - + /* Thread-safe code; CRITICAL_SECTION guards are managed by AC */ if (!self->eof) { - _zstd_state* const mod_state = PyType_GetModuleState(Py_TYPE(self)); - if (mod_state == NULL) { - return NULL; - } - ret = mod_state->empty_bytes; - Py_INCREF(ret); + return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES); } else { if (self->unused_data == NULL) { @@ -772,8 +759,6 @@ _zstd_ZstdDecompressor_unused_data_get_impl(ZstdDecompressor *self) } } - Py_END_CRITICAL_SECTION(); - return ret; } From e7a4d3430a4aebeb3d7d44101b7723dd71c94129 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Fri, 9 May 2025 20:50:16 +0100 Subject: [PATCH 2/2] delete redundant comment --- Modules/_zstd/decompressor.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Modules/_zstd/decompressor.c b/Modules/_zstd/decompressor.c index 1e4024ada0cb11..b3d91e102859da 100644 --- a/Modules/_zstd/decompressor.c +++ b/Modules/_zstd/decompressor.c @@ -741,7 +741,6 @@ _zstd_ZstdDecompressor_unused_data_get_impl(ZstdDecompressor *self) { PyObject *ret; - /* Thread-safe code; CRITICAL_SECTION guards are managed by AC */ if (!self->eof) { return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES); }