Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-127119: Remove check on small ints in long_dealloc #127120

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Slightly optimize the :class:`int` deallocator by removing a redundant check.
22 changes: 13 additions & 9 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3611,24 +3611,28 @@ long_richcompare(PyObject *self, PyObject *other, int op)
Py_RETURN_RICHCOMPARE(result, 0, op);
}

static void
ZeroIntensity marked this conversation as resolved.
Show resolved Hide resolved
long_dealloc(PyObject *self)
#ifndef NDEBUG
static int _is_python_smallint(PyObject *op)
{
/* 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.
*/
PyLongObject *pylong = (PyLongObject*)self;
PyLongObject *pylong = (PyLongObject*)op;
if (pylong && _PyLong_IsCompact(pylong)) {
stwodigits ival = medium_value(pylong);
if (IS_SMALL_INT(ival)) {
PyLongObject *small_pylong = (PyLongObject *)get_small_int((sdigit)ival);
if (pylong == small_pylong) {
_Py_SetImmortal(self);
return;
return 1;
}
}
}
return 0;
}
#endif

static void
long_dealloc(PyObject *self)
{
// assert the small ints are not deallocated
assert(!_is_python_smallint(self));
ZeroIntensity marked this conversation as resolved.
Show resolved Hide resolved
Py_TYPE(self)->tp_free(self);
}

Expand Down
Loading