diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-22-07-58-00.gh-issue-127119.p9Yv4U.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-22-07-58-00.gh-issue-127119.p9Yv4U.rst new file mode 100644 index 00000000000000..68b8b1d37cffc1 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-22-07-58-00.gh-issue-127119.p9Yv4U.rst @@ -0,0 +1 @@ +Slightly optimize the :class:`int` deallocator by removing a redundant check. diff --git a/Objects/longobject.c b/Objects/longobject.c index 4aa35685b509f2..469937a3cfb2ce 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -3614,9 +3614,13 @@ long_richcompare(PyObject *self, PyObject *other, int op) static void long_dealloc(PyObject *self) { +#if SIZEOF_VOID_P <= 4 /* same condition as in refcount.h */ +#ifndef Py_GIL_DISABLED /* This should never get called, but we also don't want to SEGV if * we accidentally decref small Ints out of existence. Instead, * since small Ints are immortal, re-set the reference count. + * + * See PEP 683, section Accidental De-Immortalizing for details */ PyLongObject *pylong = (PyLongObject*)self; if (pylong && _PyLong_IsCompact(pylong)) { @@ -3629,6 +3633,8 @@ long_dealloc(PyObject *self) } } } +#endif +#endif Py_TYPE(self)->tp_free(self); }