Skip to content

gh-90667: Add specializations of Py_DECREF when types are known #30872

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

Merged
merged 18 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Include/internal/pycore_floatobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ struct _Py_float_state {
#endif
};

void _PyFloat_ExactDealloc(PyObject *op);


PyAPI_FUNC(void) _PyFloat_DebugMallocStats(FILE* out);

Expand Down
37 changes: 36 additions & 1 deletion Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ extern "C" {
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_runtime.h" // _PyRuntime


#define _PyObject_IMMORTAL_INIT(type) \
{ \
.ob_refcnt = 999999999, \
Expand All @@ -26,6 +25,42 @@ extern "C" {
.ob_size = size, \
}

PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalRefcountErrorFunc(
const char *func,
const char *message);

#define _Py_FatalRefcountError(message) _Py_FatalRefcountErrorFunc(__func__, message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OOI, why move this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to avoid a dependence between the pycore_... includes. The unicodeobject.c and tupleobject.c implementations both only used pycore_pyerrors for _Py_FatalRefcountError(), which doesn't really concern itself with the implementation details of exceptions.


static inline void
_Py_DECREF_SPECIALIZED(PyObject *op, const destructor destruct)
{
#ifdef Py_REF_DEBUG
_Py_RefTotal--;
#endif
if (--op->ob_refcnt != 0) {
assert(op->ob_refcnt > 0);
}
else {
#ifdef Py_TRACE_REFS
_Py_ForgetReference(op);
#endif
destruct(op);
}
}

static inline void
_Py_DECREF_NO_DEALLOC(PyObject *op)
{
#ifdef Py_REF_DEBUG
_Py_RefTotal--;
#endif
op->ob_refcnt--;
#ifdef Py_DEBUG
if (op->ob_refcnt <= 0) {
_Py_FatalRefcountError("Expected a positive remaining refcount");
}
#endif
}

PyAPI_FUNC(int) _PyType_CheckConsistency(PyTypeObject *type);
PyAPI_FUNC(int) _PyDict_CheckConsistency(PyObject *mp, int check_content);
Expand Down
7 changes: 0 additions & 7 deletions Include/internal/pycore_pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,6 @@ extern PyObject* _Py_Offer_Suggestions(PyObject* exception);
PyAPI_FUNC(Py_ssize_t) _Py_UTF8_Edit_Cost(PyObject *str_a, PyObject *str_b,
Py_ssize_t max_cost);

PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalRefcountErrorFunc(
const char *func,
const char *message);

#define _Py_FatalRefcountError(message) _Py_FatalRefcountErrorFunc(__func__, message)


#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern "C" {

#include "pycore_fileutils.h" // _Py_error_handler

void _PyUnicode_ExactDealloc(PyObject *op);

/* runtime lifecycle */

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type-specialized versions of the ``Py_DECREF()``, and use them for ``float``, ``int``, ``str``, ``bool``, and ``None`` to avoid pointer-chasing at runtime where types are known at C compile time.
2 changes: 1 addition & 1 deletion Objects/boolobject.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* Boolean type, a subtype of int */

#include "Python.h"
#include "pycore_object.h" // _Py_FatalRefcountError()
#include "pycore_runtime.h" // _Py_ID()
#include "pycore_pyerrors.h" // _Py_FatalRefcountError()

/* We define bool_repr to return "False" or "True" */

Expand Down
41 changes: 27 additions & 14 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,28 +238,41 @@ PyFloat_FromString(PyObject *v)
return result;
}

static void
float_dealloc(PyFloatObject *op)
void
_PyFloat_ExactDealloc(PyObject *obj)
{
assert(PyFloat_CheckExact(obj));
PyFloatObject *op = (PyFloatObject *)obj;
#if PyFloat_MAXFREELIST > 0
if (PyFloat_CheckExact(op)) {
struct _Py_float_state *state = get_float_state();
struct _Py_float_state *state = get_float_state();
#ifdef Py_DEBUG
// float_dealloc() must not be called after _PyFloat_Fini()
assert(state->numfree != -1);
// float_dealloc() must not be called after _PyFloat_Fini()
assert(state->numfree != -1);
#endif
if (state->numfree >= PyFloat_MAXFREELIST) {
PyObject_Free(op);
return;
}
state->numfree++;
Py_SET_TYPE(op, (PyTypeObject *)state->free_list);
state->free_list = op;
if (state->numfree >= PyFloat_MAXFREELIST) {
PyObject_Free(op);
return;
}
state->numfree++;
Py_SET_TYPE(op, (PyTypeObject *)state->free_list);
state->free_list = op;
#else
PyObject_Free(op);
#endif
}

static void
float_dealloc(PyObject *op)
{
assert(PyFloat_Check(op));
#if PyFloat_MAXFREELIST > 0
if (PyFloat_CheckExact(op)) {
_PyFloat_ExactDealloc(op);
}
else
#endif
{
Py_TYPE(op)->tp_free((PyObject *)op);
Py_TYPE(op)->tp_free(op);
}
}

Expand Down
38 changes: 23 additions & 15 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ medium_value(PyLongObject *x)
#define IS_SMALL_INT(ival) (-_PY_NSMALLNEGINTS <= (ival) && (ival) < _PY_NSMALLPOSINTS)
#define IS_SMALL_UINT(ival) ((ival) < _PY_NSMALLPOSINTS)

static inline int is_medium_int(stwodigits x)
static inline void
_Py_DECREF_INT(PyLongObject *op)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this ultimately equivalent to

#define _Py_DECREF_INT(op) _Py_DECREF_SPECIALIZED(op, PyObject_Free)

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed _PyLongExactDealloc, but I'd like to keep the assertion around to make sure we're never accidentally calling this on int subclasses.

{
assert(PyLong_CheckExact(op));
_Py_DECREF_SPECIALIZED((PyObject *)op, PyObject_Free);
}

static inline int
is_medium_int(stwodigits x)
{
/* Take care that we are comparing unsigned values. */
twodigits x_plus_mask = ((twodigits)x) + PyLong_MASK;
Expand All @@ -58,7 +66,7 @@ maybe_small_long(PyLongObject *v)
if (v && IS_MEDIUM_VALUE(v)) {
stwodigits ival = medium_value(v);
if (IS_SMALL_INT(ival)) {
Py_DECREF(v);
_Py_DECREF_INT(v);
return (PyLongObject *)get_small_int((sdigit)ival);
}
}
Expand Down Expand Up @@ -1856,7 +1864,7 @@ long_to_decimal_string_internal(PyObject *aa,
#undef WRITE_DIGITS
#undef WRITE_UNICODE_DIGITS

Py_DECREF(scratch);
_Py_DECREF_INT(scratch);
if (writer) {
writer->pos += strlen;
}
Expand Down Expand Up @@ -3561,15 +3569,15 @@ k_mul(PyLongObject *a, PyLongObject *b)
*/
i = Py_SIZE(ret) - shift; /* # digits after shift */
(void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2));
Py_DECREF(t2);
_Py_DECREF_INT(t2);

(void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1));
Py_DECREF(t1);
_Py_DECREF_INT(t1);

/* 6. t3 <- (ah+al)(bh+bl), and add into result. */
if ((t1 = x_add(ah, al)) == NULL) goto fail;
Py_DECREF(ah);
Py_DECREF(al);
_Py_DECREF_INT(ah);
_Py_DECREF_INT(al);
ah = al = NULL;

if (a == b) {
Expand All @@ -3580,21 +3588,21 @@ k_mul(PyLongObject *a, PyLongObject *b)
Py_DECREF(t1);
goto fail;
}
Py_DECREF(bh);
Py_DECREF(bl);
_Py_DECREF_INT(bh);
_Py_DECREF_INT(bl);
bh = bl = NULL;

t3 = k_mul(t1, t2);
Py_DECREF(t1);
Py_DECREF(t2);
_Py_DECREF_INT(t1);
_Py_DECREF_INT(t2);
if (t3 == NULL) goto fail;
assert(Py_SIZE(t3) >= 0);

/* Add t3. It's not obvious why we can't run out of room here.
* See the (*) comment after this function.
*/
(void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3));
Py_DECREF(t3);
_Py_DECREF_INT(t3);

return long_normalize(ret);

Expand Down Expand Up @@ -3699,13 +3707,13 @@ k_lopsided_mul(PyLongObject *a, PyLongObject *b)
/* Add into result. */
(void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone,
product->ob_digit, Py_SIZE(product));
Py_DECREF(product);
_Py_DECREF_INT(product);

bsize -= nbtouse;
nbdone += nbtouse;
}

Py_DECREF(bslice);
_Py_DECREF_INT(bslice);
return long_normalize(ret);

fail:
Expand Down Expand Up @@ -5993,7 +6001,7 @@ PyTypeObject PyLong_Type = {
0, /* tp_init */
0, /* tp_alloc */
long_new, /* tp_new */
PyObject_Del, /* tp_free */
PyObject_Free, /* tp_free */
};

static PyTypeObject Int_InfoType;
Expand Down
2 changes: 1 addition & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "pycore_floatobject.h" // _PyFloat_DebugMallocStats()
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
#include "pycore_namespace.h" // _PyNamespace_Type
#include "pycore_object.h" // _PyType_CheckConsistency()
#include "pycore_object.h" // _PyType_CheckConsistency(), _Py_FatalRefcountError()
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
#include "pycore_pystate.h" // _PyThreadState_GET()
Expand Down
3 changes: 1 addition & 2 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED()
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_object.h" // _PyObject_GC_TRACK()
#include "pycore_pyerrors.h" // _Py_FatalRefcountError()
#include "pycore_object.h" // _PyObject_GC_TRACK(), _Py_FatalRefcountError()

/*[clinic input]
class tuple "PyTupleObject *" "&PyTuple_Type"
Expand Down
10 changes: 8 additions & 2 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_interp.h" // PyInterpreterState.fs_codec
#include "pycore_long.h" // _PyLong_FormatWriter()
#include "pycore_object.h" // _PyObject_GC_TRACK()
#include "pycore_object.h" // _PyObject_GC_TRACK(), _Py_FatalRefcountError()
#include "pycore_pathconfig.h" // _Py_DumpPathConfig()
#include "pycore_pyerrors.h" // _Py_FatalRefcountError()
#include "pycore_pylifecycle.h" // _Py_SetFileSystemEncoding()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
Expand Down Expand Up @@ -15368,6 +15367,13 @@ unicode_subtype_new(PyTypeObject *type, PyObject *unicode)
return NULL;
}

void
_PyUnicode_ExactDealloc(PyObject *op)
{
assert(PyUnicode_CheckExact(op));
unicode_dealloc(op);
}

PyDoc_STRVAR(unicode_doc,
"str(object='') -> str\n\
str(bytes_or_buffer[, encoding[, errors]]) -> str\n\
Expand Down
2 changes: 1 addition & 1 deletion Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2511,7 +2511,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
}
if (PyFloat_CheckExact(item)) {
f_result += PyFloat_AS_DOUBLE(item);
Py_DECREF(item);
_Py_DECREF_SPECIALIZED(item, _PyFloat_ExactDealloc);
continue;
}
if (PyLong_Check(item)) {
Expand Down
Loading