-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-127119: Faster check for small ints in long_dealloc #127620
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
Changes from 27 commits
cfb70cb
426dd09
4639642
d9c26d3
2f73d47
3b6e1fe
03184a7
5eca812
fedc102
c733d25
829a595
39da0ea
5058e53
8a3d00f
878207a
068a16a
a65ec5a
38fe25f
32b6e44
8543c78
e232ca4
f1ce753
99a2fc7
f6a76b0
9894866
7834406
d91b6e3
ebc7e17
aaf110b
8fcc1d0
aff8812
ebb0bca
8d8e794
4a07ba1
1d5b2e0
cfb9120
b606c09
b4c8444
f76c0e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Slightly optimize the :class:`int` deallocator. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe include the benchmarks results (it's a 4% improvement which is still noticable IMO). You should mention that it only concerns PGO builds as well. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3616,32 +3616,25 @@ long_richcompare(PyObject *self, PyObject *other, int op) | |
} | ||
|
||
static inline int | ||
compact_int_is_small(PyObject *self) | ||
/// Return 1 if the object is one of the immortal small ints | ||
_long_is_small_int(PyObject *op) | ||
markshannon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
PyLongObject *pylong = (PyLongObject *)self; | ||
assert(_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) { | ||
return 1; | ||
} | ||
} | ||
return 0; | ||
PyLongObject *long_object = (PyLongObject *)op; | ||
return (long_object->long_value.lv_tag & IMMORTALITY_BIT_MASK) != 0; | ||
} | ||
|
||
void | ||
_PyLong_ExactDealloc(PyObject *self) | ||
{ | ||
assert(PyLong_CheckExact(self)); | ||
#ifndef Py_GIL_DISABLED | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should excluding the no-gil build. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. According to https://peps.python.org/pep-0683/#stable-abi the no-gil implementation can work with older stable API extensions. |
||
if (_long_is_small_int(self)) { | ||
// See PEP 683, section Accidental De-Immortalizing for details | ||
_Py_SetImmortal(self); | ||
return; | ||
} | ||
#endif | ||
if (_PyLong_IsCompact((PyLongObject *)self)) { | ||
#ifndef Py_GIL_DISABLED | ||
if (compact_int_is_small(self)) { | ||
// See PEP 683, section Accidental De-Immortalizing for details | ||
_Py_SetImmortal(self); | ||
return; | ||
} | ||
#endif | ||
_Py_FREELIST_FREE(ints, self, PyObject_Free); | ||
return; | ||
} | ||
|
@@ -3651,24 +3644,22 @@ _PyLong_ExactDealloc(PyObject *self) | |
static void | ||
long_dealloc(PyObject *self) | ||
{ | ||
assert(self); | ||
if (_PyLong_IsCompact((PyLongObject *)self)) { | ||
if (compact_int_is_small(self)) { | ||
/* 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 | ||
*/ | ||
_Py_SetImmortal(self); | ||
return; | ||
} | ||
if (PyLong_CheckExact(self)) { | ||
_Py_FREELIST_FREE(ints, self, PyObject_Free); | ||
return; | ||
} | ||
#ifndef Py_GIL_DISABLED | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise |
||
if (_long_is_small_int(self)) { | ||
/* 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 | ||
*/ | ||
_Py_SetImmortal(self); | ||
return; | ||
} | ||
#endif | ||
if (PyLong_CheckExact(self) && _PyLong_IsCompact((PyLongObject *)self)) { | ||
_Py_FREELIST_FREE(ints, self, PyObject_Free); | ||
return; | ||
} | ||
|
||
Py_TYPE(self)->tp_free(self); | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.