8000 gh-90667: Add specializations of Py_DECREF when types are known by sweeneyde · Pull Request #30872 · python/cpython · GitHub
[go: up one dir, main page]

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 2 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
16 changes: 16 additions & 0 deletions Include/boolobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ PyAPI_FUNC(int) Py_IsTrue(PyObject *x);
PyAPI_FUNC(int) Py_IsFalse(PyObject *x);
#define Py_IsFalse(x) Py_Is((x), Py_False)

static inline void
_Py_DECREF_BOOL(PyObject *op)
{
assert(PyBool_Check(op));
#ifdef Py_REF_DEBUG
_Py_RefTotal--;
#endif
op->ob_refcnt--;
#ifdef Py_DEBUG
if (op->ob_refcnt <= 0) {
// Calls _Py_FatalRefcountError
_Py_Dealloc(op);
}
#endif
}

/* Macros for returning Py_True or Py_False, respectively */
#define Py_RETURN_TRUE return Py_NewRef(Py_True)
#define Py_RETURN_FALSE return Py_NewRef(Py_False)
Expand Down
10 changes: 10 additions & 0 deletions Include/internal/pycore_floatobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ struct _Py_float_state {
#endif
};

PyAPI_FUNC(void) _PyFloat_ExactDealloc(PyFloatObject *op);

static inline void
_Py_DECREF_FLOAT(PyObject *op)
{
assert(PyFloat_CheckExact(op));
_Py_DECREF_SPECIALIZED(op, (destructor)_PyFloat_ExactDealloc);
}


/* _PyFloat_{Pack,Unpack}{4,8}
*
* The struct and pickle (at least) modules need an efficient platform-
Expand Down
10 changes: 10 additions & 0 deletions Include/internal/pycore_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ PyAPI_FUNC(char*) _PyLong_FormatBytesWriter(
int base,
int alternate);

PyAPI_FUNC(void) _PyLong_ExactDealloc(PyLongObject *op);

static inline void
_Py_DECREF_INT(PyObject *op)
{
assert(PyLong_CheckExact(op));
_Py_DECREF_SPECIALIZED(op, (destructor)_PyLong_ExactDealloc);
}


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

#include "pycore_fileutils.h" // _Py_error_handler

void _PyUnicode_ExactDealloc(PyUnicodeObject *op);

static inline void
_Py_DECREF_STR(PyObject *op)
{
assert(PyUnicode_CheckExact(op));
_Py_DECREF_SPECIALIZED(op, (destructor)_PyUnicode_ExactDealloc);
}

/* runtime lifecycle */

Expand Down
33 changes: 33 additions & 0 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,22 @@ static inline void _Py_DECREF(
# define Py_DECREF(op) _Py_DECREF(_PyObject_CAST(op))
#endif

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);
}
}

/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
* and tp_dealloc implementations.
Expand Down Expand Up @@ -622,6 +638,23 @@ Don't forget to apply Py_INCREF() when returning this value!!!
PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
#define Py_None (&_Py_NoneStruct)

static inline void
_Py_DECREF_NONE(PyObject *op)
{
assert(op == Py_None);
#ifdef Py_REF_DEBUG
_Py_RefTotal--;
#endif
(void)op;
((PyObject *)Py_None)->ob_refcnt--;
#ifdef Py_DEBUG
if (((PyObject *)Py_None)->ob_refcnt <= 0) {
// Calls _Py_FatalRefcountError
_Py_Dealloc(op);
}
#endif
}

// Test if an object is the None singleton, the same as "x is None" in Python.
PyAPI_FUNC(int) Py_IsNone(PyObject *x);
#define Py_IsNone(x) Py_Is((x), Py_None)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type-specialized versions of the ``Py_DECREF()`` macro for ``float``, ``int``, ``str``, ``bool``, and ``None`` to avoid pointer-chasing at runtime when types are known at compile time.
11 changes: 6 additions & 5 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ raised for division by zero and mod by zero.
#include "pycore_bitutils.h" // _Py_bit_length()
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_dtoa.h" // _Py_dg_infinity()
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_long.h" // _PyLong_GetZero(), _Py_DECREF_INT
#include "pycore_floatobject.h" // _Py_DECREF_FLOAT
/* For DBL_EPSILON in _math.h */
#include <float.h>
/* For _Py_log1p with workarounds for buggy handling of zeros. */
Expand Down Expand Up @@ -3139,7 +3140,7 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
long i_result = PyLong_AsLongAndOverflow(result, &overflow);
/* If this already overflowed, don't even enter the loop. */
if (overflow == 0) {
Py_DECREF(result);
_Py_DECREF_INT(result);
result = NULL;
}
/* Loop over all the items in the iterable until we finish, we overflow
Expand All @@ -3158,7 +3159,7 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
if (overflow == 0 && !_check_long_mult_overflow(i_result, b)) {
long x = i_result * b;
i_result = x;
Py_DECREF(item);
_Py_DECREF_INT(item);
continue;
}
}
Expand Down Expand Up @@ -3187,7 +3188,7 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
*/
if (PyFloat_CheckExact(result)) {
double f_result = PyFloat_AS_DOUBLE(result);
Py_DECREF(result);
_Py_DECREF_FLOAT(result);
result = NULL;
while(result == NULL) {
item = PyIter_Next(iter);
Expand All @@ -3200,7 +3201,7 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
}
if (PyFloat_CheckExact(item)) {
f_result *= PyFloat_AS_DOUBLE(item);
Py_DECREF(item);
_Py_DECREF_FLOAT(item);
continue;
}
if (PyLong_CheckExact(item)) {
Expand Down
35 changes: 23 additions & 12 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,23 +238,34 @@ PyFloat_FromString(PyObject *v)
return result;
}

void
_PyFloat_ExactDealloc(PyFloatObject *op)
{
assert(PyFloat_CheckExact(op));
#if PyFloat_MAXFREELIST > 0
struct _Py_float_state *state = get_float_state();
#ifdef Py_DEBUG
// 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;
#else
PyObject_Free(op);
#endif
}

static void
float_dealloc(PyFloatObject *op)
{
#if PyFloat_MAXFREELIST > 0
if (PyFloat_CheckExact(op)) {
struct _Py_float_state *state = get_float_state();
#ifdef Py_DEBUG
// 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;
_PyFloat_ExactDealloc(op);
}
else
#endif
Expand Down
33 changes: 20 additions & 13 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,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((PyObject *)v);
return (PyLongObject *)get_small_int((sdigit)ival);
}
}
Expand Down Expand Up @@ -1824,7 +1824,7 @@ long_to_decimal_string_internal(PyObject *aa,
#undef WRITE_DIGITS
#undef WRITE_UNICODE_DIGITS

Py_DECREF(scratch);
_Py_DECREF_INT((PyObject *)scratch);
if (writer) {
writer->pos += strlen;
}
Expand Down Expand Up @@ -3479,15 +3479,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((PyObject *)t2);

(void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1));
Py_DECREF(t1);
_Py_DECREF_INT((PyObject *)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((PyObject *)ah);
_Py_DECREF_INT((PyObject *)al);
ah = al = NULL;

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

t3 = k_mul(t1, t2);
Py_DECREF(t1);
Py_DECREF(t2);
_Py_DECREF_INT((PyObject *)t1);
_Py_DECREF_INT((PyObject *)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((PyObject *)t3);

return long_normalize(ret);

Expand Down Expand Up @@ -3617,13 +3617,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((PyObject *)product);

bsize -= nbtouse;
nbdone += nbtouse;
}

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

fail:
Expand Down Expand Up @@ -5743,6 +5743,13 @@ int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
return long_obj;
}

void
_PyLong_ExactDealloc(PyLongObject *op)
{
assert(PyLong_CheckExact(op));
PyObject_Del(op);
}

static PyObject *
long_long_meth(PyObject *self, PyObject *Py_UNUSED(ignored))
{
Expand Down
7 changes: 7 additions & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -15463,6 +15463,13 @@ unicode_subtype_new(PyTypeObject *type, PyObject *unicode)
return NULL;
}

void
_PyUnicode_ExactDealloc(PyUnicodeObject *op)
{
assert(PyUnicode_CheckExact(op));
PyObject_Free(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 @@ -2520,7 +2520,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_FLOAT(item);
continue;
}
if (PyLong_Check(item)) {
Expand Down
Loading
0