8000 Faster check for small ints in long_dealloc · python/cpython@cfb70cb · GitHub
[go: up one dir, main page]

Skip to content

Commit cfb70cb

Browse files
committed
Faster check for small ints in long_dealloc
1 parent e51da64 commit cfb70cb

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

Objects/longobject.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3611,24 +3611,28 @@ long_richcompare(PyObject *self, PyObject *other, int op)
36113611
Py_RETURN_RICHCOMPARE(result, 0, op);
36123612
}
36133613

3614+
static inline int
3615+
/// Return 1 if the object is one of the immortal small ints
3616+
_long_is_small_int(PyObject *op)
3617+
{
3618+
return (((PyObject *)(&_PyLong_SMALL_INTS[0]) <= op)
3619+
&& (op <= (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS +_PY_NSMALLPOSINTS]));
3620+
}
36143621
static void
36153622
long_dealloc(PyObject *self)
36163623
{
3617-
/* This should never get called, but we also don't want to SEGV if
3618-
* we accidentally decref small Ints out of existence. Instead,
3619-
* since small Ints are immortal, re-set the reference count.
3620-
*/
3621-
PyLongObject *pylong = (PyLongObject*)self;
3622-
if (pylong && _PyLong_IsCompact(pylong)) {
3623-
stwodigits ival = medium_value(pylong);
3624-
if (IS_SMALL_INT(ival)) {
3625-
PyLongObject *small_pylong = (PyLongObject *)get_small_int((sdigit)ival);
3626-
if (pylong == small_pylong) {
3627-
_Py_SetImmortal(self);
3628-
return;
3629-
}
3630-
}
3624+
#ifndef Py_GIL_DISABLED
3625+
if (_long_is_small_int(self)) {
3626+
/* This should never get called, but we also don't want to SEGV if
3627+
* we accidentally decref small Ints out of existence. Instead,
3628+
* since small Ints are immortal, re-set the reference count.
3629+
*
3630+
* See PEP 683, section Accidental De-Immortalizing for details
3631+
*/
3632+
_Py_SetImmortal(self);
3633+
return;
36313634
}
3635+
#endif
36323636
Py_TYPE(self)->tp_free(self);
36333637
}
36343638

0 commit comments

Comments
 (0)
0