Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-126868: Add freelist for compact int objects#126865
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
0713034be58ade3f50b54fa97302d72486fe07c218328e0c1e1dc2b39df776b6b73046db8247ed1e4aa2644e85a9f86b6e88274d61e548bd13bc3e960220c0593d621efde111928e912437c24cb03494819f64f614681c1File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Increase performance of :class:`int` by adding a freelist for compact ints. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -6,6 +6,7 @@ | ||
| #include "pycore_bitutils.h" // _Py_popcount32() | ||
| #include "pycore_initconfig.h" // _PyStatus_OK() | ||
| #include "pycore_call.h" // _PyObject_MakeTpCall | ||
| #include "pycore_freelist.h" // _Py_FREELIST_FREE, _Py_FREELIST_POP | ||
| #include "pycore_long.h" // _Py_SmallInts | ||
| #include "pycore_object.h" // _PyObject_Init() | ||
| #include "pycore_runtime.h" // _PY_NSMALLPOSINTS | ||
| @@ -42,7 +43,7 @@ static inline void | ||
| _Py_DECREF_INT(PyLongObject *op) | ||
| { | ||
| assert(PyLong_CheckExact(op)); | ||
| _Py_DECREF_SPECIALIZED((PyObject *)op, (destructor)PyObject_Free); | ||
| _Py_DECREF_SPECIALIZED((PyObject *)op, _PyLong_ExactDealloc); | ||
| } | ||
| static inline int | ||
| @@ -220,15 +221,18 @@ _PyLong_FromMedium(sdigit x) | ||
| { | ||
| assert(!IS_SMALL_INT(x)); | ||
| assert(is_medium_int(x)); | ||
| /* We could use a freelist here */ | ||
| PyLongObject *v = PyObject_Malloc(sizeof(PyLongObject)); | ||
| PyLongObject *v = (PyLongObject *)_Py_FREELIST_POP(PyLongObject, ints); | ||
| if (v == NULL) { | ||
| PyErr_NoMemory(); | ||
| return NULL; | ||
| v = PyObject_Malloc(sizeof(PyLongObject)); | ||
| if (v == NULL) { | ||
| PyErr_NoMemory(); | ||
| return NULL; | ||
| } | ||
| _PyObject_Init((PyObject*)v, &PyLong_Type); | ||
| } | ||
| digit abs_x = x < 0 ? -x : x; | ||
| _PyLong_SetSignAndDigitCount(v, x<0?-1:1, 1); | ||
| _PyObject_Init((PyObject*)v, &PyLong_Type); | ||
| v->long_value.ob_digit[0] = abs_x; | ||
| return (PyObject*)v; | ||
| } | ||
| @@ -3611,24 +3615,60 @@ long_richcompare(PyObject *self, PyObject *other, int op) | ||
| Py_RETURN_RICHCOMPARE(result, 0, op); | ||
| } | ||
| static inline int | ||
| compact_int_is_small(PyObject *self) | ||
| { | ||
| 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; | ||
| } | ||
| void | ||
| _PyLong_ExactDealloc(PyObject *self) | ||
| { | ||
| assert(PyLong_CheckExact(self)); | ||
| 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); | ||
Member 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 just realised that we didnt check for if it's a smallint cached value here. It should be rejected there
| ||
| return; | ||
| } | ||
| PyObject_Free(self); | ||
| } | ||
| static void | ||
| long_dealloc(PyObject *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. | ||
| */ | ||
| PyLongObject *pylong = (PyLongObject*)self; | ||
| 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; | ||
| } | ||
| 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; | ||
| } | ||
| } | ||
| Py_TYPE(self)->tp_free(self); | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need it to be exported like that or could you live with a simple
extern? If not, can you explain which file needs this export?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's needed by the JIT on Windows, as it's used in bytecodes.c. This is a pretty common pattern in the internal C API unfortunately
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For semantic purposes, would it be better to have a macro named differently? (it would be a simple alias but it could help semantics and reviewers)