Skip to content

Specialize Py_DECREF() for Py_REF_DEBUG #16781

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

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 16 additions & 11 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,25 +461,30 @@ static inline void _Py_INCREF(PyObject *op)

#define Py_INCREF(op) _Py_INCREF(_PyObject_CAST(op))

#ifdef Py_REF_DEBUG
static inline void _Py_DECREF(const char *filename, int lineno,
PyObject *op)
{
(void)filename; /* may be unused, shut up -Wunused-parameter */
(void)lineno; /* may be unused, shut up -Wunused-parameter */
_Py_DEC_REFTOTAL;
if (--op->ob_refcnt != 0) {
#ifdef Py_REF_DEBUG
if (op->ob_refcnt < 0) {
_Py_NegativeRefcount(filename, lineno, op);
}
#endif
_Py_RefTotal--;
Copy link
Member

Choose a reason for hiding this comment

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

So this _Py_DEC_REFTOTAL would be converted in your plan?

Copy link
Member Author

Choose a reason for hiding this comment

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

_Py_DEC_REFTOTAL = _Py_RefTotal-- when Py_REF_DEBUG is defined. I prefer to put directly the real code, it may help debugging. It avoids the indirection of the preprocessor.

if (--op->ob_refcnt == 0) {
_Py_Dealloc(op);
Copy link
Member

Choose a reason for hiding this comment

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

This code dupe irks me. Can't you make the REF_DEBUG version call the regular version instead?

}
else {
else if (op->ob_refcnt < 0) {
_Py_NegativeRefcount(filename, lineno, op);
}
}

# define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
#else
static inline void _Py_DECREF(PyObject *op)
{
if (--op->ob_refcnt == 0) {
_Py_Dealloc(op);
}
}

#define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
# define Py_DECREF(op) _Py_DECREF(_PyObject_CAST(op))
#endif /* !Py_REF_DEBUG */


/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
Expand Down