Skip to content

Commit 924c06c

Browse files
committed
Experiment: Make all decrefs calls
1 parent a4625d5 commit 924c06c

3 files changed

Lines changed: 67 additions & 53 deletions

File tree

Include/internal/pycore_object.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ Py_ssize_t _Py_ExplicitMergeRefcount(PyObject *op, Py_ssize_t extra);
306306
# undef _Py_DEC_REFTOTAL
307307
#endif
308308

309+
PyAPI_FUNC(void) _Py_DecRefMortal(PyObject *op);
310+
PyAPI_FUNC(void) _Py_DecRefMortal_Debug(const char *filename, int lineno, PyObject *op);
309311

310312
extern int _PyType_CheckConsistency(PyTypeObject *type);
311313
extern int _PyDict_CheckConsistency(PyObject *mp, int check_content);
@@ -436,21 +438,8 @@ _Py_DECREF_CODE(PyCodeObject *co)
436438
#ifndef Py_GIL_DISABLED
437439
#ifdef Py_REF_DEBUG
438440

439-
static inline void Py_DECREF_MORTAL(const char *filename, int lineno, PyObject *op)
440-
{
441-
if (op->ob_refcnt <= 0) {
442-
_Py_NegativeRefcount(filename, lineno, op);
443-
}
444-
_Py_DECREF_STAT_INC();
445-
assert(!_Py_IsStaticImmortal(op));
446-
if (!_Py_IsImmortal(op)) {
447-
_Py_DECREF_DecRefTotal();
448-
}
449-
if (--op->ob_refcnt == 0) {
450-
_Py_Dealloc(op);
451-
}
452-
}
453-
#define Py_DECREF_MORTAL(op) Py_DECREF_MORTAL(__FILE__, __LINE__, _PyObject_CAST(op))
441+
442+
#define Py_DECREF_MORTAL(op) _Py_DecRefMortal_Debug(__FILE__, __LINE__, _PyObject_CAST(op))
454443

455444
static inline void _Py_DECREF_MORTAL_SPECIALIZED(const char *filename, int lineno, PyObject *op, destructor destruct)
456445
{

Include/refcount.h

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -392,45 +392,14 @@ static inline void Py_DECREF(PyObject *op)
392392

393393
#elif defined(Py_REF_DEBUG)
394394

395-
static inline void Py_DECREF(const char *filename, int lineno, PyObject *op)
396-
{
397-
#if SIZEOF_VOID_P > 4
398-
/* If an object has been freed, it will have a negative full refcnt
399-
* If it has not it been freed, will have a very large refcnt */
400-
if (op->ob_refcnt_full <= 0 || op->ob_refcnt > (((PY_UINT32_T)-1) - (1<<20))) {
401-
#else
402-
if (op->ob_refcnt <= 0) {
403-
#endif
404-
_Py_NegativeRefcount(filename, lineno, op);
405-
}
406-
if (_Py_IsImmortal(op)) {
407-
_Py_DECREF_IMMORTAL_STAT_INC();
408-
return;
409-
}
410-
_Py_DECREF_STAT_INC();
411-
_Py_DECREF_DecRefTotal();
412-
if (--op->ob_refcnt == 0) {
413-
_Py_Dealloc(op);
414-
}
415-
}
416-
#define Py_DECREF(op) Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
395+
396+
PyAPI_FUNC(void) _Py_DecRef_Debug(const char *filename, int lineno, PyObject *op);
397+
398+
#define Py_DECREF(op) _Py_DecRef_Debug(__FILE__, __LINE__, _PyObject_CAST(op))
417399

418400
#else
419401

420-
static inline Py_ALWAYS_INLINE void Py_DECREF(PyObject *op)
421-
{
422-
// Non-limited C API and limited C API for Python 3.9 and older access
423-
// directly PyObject.ob_refcnt.
424-
if (_Py_IsImmortal(op)) {
425-
_Py_DECREF_IMMORTAL_STAT_INC();
426-
return;
427-
}
428-
_Py_DECREF_STAT_INC();
429-
if (--op->ob_refcnt == 0) {
430-
_Py_Dealloc(op);
431-
}
432-
}
433-
#define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op))
402+
#define Py_DECREF(op) _Py_DecRef(_PyObject_CAST(op))
434403
#endif
435404

436405

Objects/object.c

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,65 @@ _Py_IncRef(PyObject *o)
349349
}
350350

351351
void
352-
_Py_DecRef(PyObject *o)
352+
_Py_DecRef(PyObject *op)
353353
{
354-
Py_DECREF(o);
354+
if (_Py_IsImmortal(op)) {
355+
_Py_DECREF_IMMORTAL_STAT_INC();
356+
return;
357+
}
358+
_Py_DECREF_STAT_INC();
359+
if (--op->ob_refcnt == 0) {
360+
_Py_Dealloc(op);
361+
}
362+
}
363+
364+
void
365+
_Py_DecRefMortal(PyObject *op)
366+
{
367+
_Py_DECREF_STAT_INC();
368+
if (--op->ob_refcnt == 0) {
369+
_Py_Dealloc(op);
370+
}
371+
}
372+
373+
374+
void
375+
_Py_DecRefMortal_Debug(const char *filename, int lineno, PyObject *op)
376+
{
377+
if (op->ob_refcnt <= 0) {
378+
_Py_NegativeRefcount(filename, lineno, op);
379+
}
380+
_Py_DECREF_STAT_INC();
381+
assert(!_Py_IsStaticImmortal(op));
382+
if (!_Py_IsImmortal(op)) {
383+
_Py_DECREF_DecRefTotal();
384+
}
385+
if (--op->ob_refcnt == 0) {
386+
_Py_Dealloc(op);
387+
}
388+
}
389+
390+
void
391+
_Py_DecRef_Debug(const char *filename, int lineno, PyObject *op)
392+
{
393+
#if SIZEOF_VOID_P > 4
394+
/* If an object has been freed, it will have a negative full refcnt
395+
* If it has not it been freed, will have a very large refcnt */
396+
if (op->ob_refcnt_full <= 0 || op->ob_refcnt > (((PY_UINT32_T)-1) - (1<<20))) {
397+
#else
398+
if (op->ob_refcnt <= 0) {
399+
#endif
400+
_Py_NegativeRefcount(filename, lineno, op);
401+
}
402+
if (_Py_IsImmortal(op)) {
403+
_Py_DECREF_IMMORTAL_STAT_INC();
404+
return;
405+
}
406+
_Py_DECREF_STAT_INC();
407+
_Py_DECREF_DecRefTotal();
408+
if (--op->ob_refcnt == 0) {
409+
_Py_Dealloc(op);
410+
}
355411
}
356412

357413
#ifdef Py_GIL_DISABLED

0 commit comments

Comments
 (0)