8000 bpo-39542: Simplify _Py_NewReference() (GH-18332) · python/cpython@49932fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 49932fe

Browse files
authored
bpo-39542: Simplify _Py_NewReference() (GH-18332)
* Remove _Py_INC_REFTOTAL and _Py_DEC_REFTOTAL macros: modify directly _Py_RefTotal. * _Py_ForgetReference() is no longer defined if the Py_TRACE_REFS macro is not defined. * Remove _Py_NewReference() implementation from object.c: unify the two implementations in object.h inline function. * Fix Py_TRACE_REFS build: _Py_INC_TPALLOCS() macro has been removed.
1 parent 24e5ad4 commit 49932fe

File tree

7 files changed

+60
-58
lines changed

7 files changed

+60
-58
lines changed

Include/object.h

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -379,25 +379,11 @@ decision that's up to the implementer of each new type so if you want,
379379
you can count such references to the type object.)
380380
*/
381381

382-
/* First define a pile of simple helper macros, one set per special
383-
* build symbol. These either expand to the obvious things, or to
384-
* nothing at all when the special mode isn't in effect. The main
385-
* macros can later be defined just once then, yet expand to different
386-
* things depending on which special build options are and aren't in effect.
387-
* Trust me <wink>: while painful, this is 20x easier to understand than,
388-
* e.g, defining _Py_NewReference five different times in a maze of nested
389-
* #ifdefs (we used to do that -- it was impenetrable).
390-
*/
391382
#ifdef Py_REF_DEBUG
392383
PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
393384
PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
394385
PyObject *op);
395386
PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
396-
#define _Py_INC_REFTOTAL _Py_RefTotal++
397-
#define _Py_DEC_REFTOTAL _Py_RefTotal--
398-
#else
399-
#define _Py_INC_REFTOTAL
400-
#define _Py_DEC_REFTOTAL
401387
#endif /* Py_REF_DEBUG */
402388

403389
/* Update the Python traceback of an object. This function must be called
@@ -406,33 +392,33 @@ PyAPI_FUNC(int) _PyTraceMalloc_NewReference(PyObject *op);
406392

407393
#ifdef Py_TRACE_REFS
408394
/* Py_TRACE_REFS is such major surgery that we call external routines. */
409-
PyAPI_FUNC(void) _Py_NewReference(PyObject *);
410395
PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
411396
PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
412-
#else
413-
/* Without Py_TRACE_REFS, there's little enough to do that we expand code
414-
inline. */
397+
#endif
398+
399+
415400
static inline void _Py_NewReference(PyObject *op)
416401
{
417402
if (_Py_tracemalloc_config.tracing) {
418403
_PyTraceMalloc_NewReference(op);
419404
}
420-
_Py_INC_REFTOTAL;
405+
#ifdef Py_REF_DEBUG
406+
_Py_RefTotal++;
407+
#endif
421408
Py_REFCNT(op) = 1;
409+
#ifdef Py_TRACE_REFS
410+
_Py_AddToAllObjects(op, 1);
411+
#endif
422412
}
423413

424-
static inline void _Py_ForgetReference(PyObject *Py_UNUSED(op))
425-
{
426-
/* nothing to do */
427-
}
428-
#endif /* !Py_TRACE_REFS */
429-
430414

431415
PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
432416

433417
static inline void _Py_INCREF(PyObject *op)
434418
{
435-
_Py_INC_REFTOTAL;
419+
#ifdef Py_REF_DEBUG
420+
_Py_RefTotal++;
421+
#endif
436422
op->ob_refcnt++;
437423
}
438424

@@ -444,7 +430,9 @@ static inline void _Py_DECREF(
444430
#endif
445431
PyObject *op)
446432
{
447-
_Py_DEC_REFTOTAL;
433+
#ifdef Py_REF_DEBUG
434+
_Py_RefTotal--;
435+
#endif
448436
if (--op->ob_refcnt != 0) {
449437
#ifdef Py_REF_DEBUG
450438
if (op->ob_refcnt < 0) {

Modules/_testcapimodule.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3586,9 +3586,11 @@ slot_tp_del(PyObject *self)
35863586
self->ob_refcnt = refcnt;
35873587
}
35883588
assert(!PyType_IS_GC(Py_TYPE(self)) || _PyObject_GC_IS_TRACKED(self));
3589-
/* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
3590-
* we need to undo that. */
3591-
_Py_DEC_REFTOTAL;
3589+
/* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
3590+
_Py_RefTotal, so we need to undo that. */
3591+
#ifdef Py_REF_DEBUG
3592+
_Py_RefTotal--;
3593+
#endif
35923594
}
35933595

35943596
static PyObject *

Objects/bytesobject.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2948,8 +2948,12 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
29482948
return (*pv == NULL) ? -1 : 0;
29492949
}
29502950
/* XXX UNREF/NEWREF interface should be more symmetrical */
2951-
_Py_DEC_REFTOTAL;
2951+
#ifdef Py_REF_DEBUG
2952+
_Py_RefTotal--;
2953+
#endif
2954+
#ifdef Py_TRACE_REFS
29522955
_Py_ForgetReference(v);
2956+
#endif
29532957
*pv = (PyObject *)
29542958
PyObject_REALLOC(v, PyBytesObject_SIZE + newsize);
29552959
if (*pv == NULL) {

Objects/dictobject.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,19 @@ static void free_keys_object(PyDictKeysObject *keys);
311311
static inline void
312312
dictkeys_incref(PyDictKeysObject *dk)
313313
{
314-
_Py_INC_REFTOTAL;
314+
#ifdef Py_REF_DEBUG
315+
_Py_RefTotal++;
316+
#endif
315317
dk->dk_refcnt++;
316318
}
317319

318320
static inline void
319321
dictkeys_decref(PyDictKeysObject *dk)
320322
{
321323
assert(dk->dk_refcnt > 0);
322-
_Py_DEC_REFTOTAL;
324+
#ifdef Py_REF_DEBUG
325+
_Py_RefTotal--;
326+
#endif
323327
if (--dk->dk_refcnt == 0) {
324328
free_keys_object(dk);
325329
}
@@ -563,7 +567,9 @@ static PyDictKeysObject *new_keys_object(Py_ssize_t size)
563567
return NULL;
564568
}
565569
}
566-
_Py_INC_REFTOTAL;
570+
#ifdef Py_REF_DEBUG
571+
_Py_RefTotal++;
572+
#endif
567573
dk->dk_refcnt = 1;
568574
dk->dk_size = size;
569575
dk->dk_usable = usable;
@@ -687,10 +693,12 @@ clone_combined_dict(PyDictObject *orig)
687693
}
688694

689695
/* Since we copied the keys table we now have an extra reference
690-
in the system. Manually call _Py_INC_REFTOTAL to signal that
696+
in the system. Manually call increment _Py_RefTotal to signal that
691697
we have it now; calling dictkeys_incref would be an error as
692698
keys->dk_refcnt is already set to 1 (after memcpy). */
693-
_Py_INC_REFTOTAL;
699+
#ifdef Py_REF_DEBUG
700+
_Py_RefTotal++;
701+
#endif
694702

695703
return (PyObject *)new;
696704
}
@@ -1249,13 +1257,15 @@ dictresize(PyDictObject *mp, Py_ssize_t minsize)
12491257

12501258
assert(oldkeys->dk_lookup != lookdict_split);
12511259
assert(oldkeys->dk_refcnt == 1);
1260+
#ifdef Py_REF_DEBUG
1261+
_Py_RefTotal--;
1262+
#endif
12521263
if (oldkeys->dk_size == PyDict_MINSIZE &&
1253-
numfreekeys < PyDict_MAXFREELIST) {
1254-
_Py_DEC_REFTOTAL;
1264+
numfreekeys < PyDict_MAXFREELIST)
1265+
{
12551266
keys_free_list[numfreekeys++] = oldkeys;
12561267
}
12571268
else {
1258-
_Py_DEC_REFTOTAL;
12591269
PyObject_FREE(oldkeys);
12601270
}
12611271
}

Objects/object.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,11 @@ PyObject_CallFinalizerFromDealloc(PyObject *self)
232232
_PyObject_ASSERT(self,
233233
(!PyType_IS_GC(Py_TYPE(self))
234234
|| _PyObject_GC_IS_TRACKED(self)));
235-
/* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
236-
* we need to undo that. */
237-
_Py_DEC_REFTOTAL;
235+
/* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
236+
_Py_RefTotal, so we need to undo that. */
237+
#ifdef Py_REF_DEBUG
238+
_Py_RefTotal--;
239+
#endif
238240
return -1;
239241
}
240242

@@ -1804,19 +1806,6 @@ _PyTypes_Init(void)
18041806

18051807

18061808
#ifdef Py_TRACE_REFS
1807-
1808-
void
1809-
_Py_NewReference(PyObject *op)
1810-
{
1811-
if (_Py_tracemalloc_config.tracing) {
1812-
_PyTraceMalloc_NewReference(op);
1813-
}
1814-
_Py_INC_REFTOTAL;
1815-
op->ob_refcnt = 1;
1816-
_Py_AddToAllObjects(op, 1);
1817-
_Py_INC_TPALLOCS(op);
1818-
}
1819-
18201809
void
18211810
_Py_ForgetReference(PyObject *op)
18221811
{

Objects/tupleobject.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,10 +902,15 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
902902
}
903903

904904
/* XXX UNREF/NEWREF interface should be more symmetrical */
905-
_Py_DEC_REFTOTAL;
906-
if (_PyObject_GC_IS_TRACKED(v))
905+
#ifdef Py_REF_DEBUG
906+
_Py_RefTotal--;
907+
#endif
908+
if (_PyObject_GC_IS_TRACKED(v)) {
907909
_PyObject_GC_UNTRACK(v);
910+
}
911+
#ifdef Py_TRACE_REFS
908912
_Py_ForgetReference((PyObject *) v);
913+
#endif
909914
/* DECREF items deleted by shrinkage */
910915
for (i = newsize; i < oldsize; i++) {
911916
Py_CLEAR(v->ob_item[i]);

Objects/unicodeobject.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,8 +1037,12 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
10371037
_PyUnicode_UTF8(unicode) = NULL;
10381038
_PyUnicode_UTF8_LENGTH(unicode) = 0;
10391039
}
1040-
_Py_DEC_REFTOTAL;
1040+
#ifdef Py_REF_DEBUG
1041+
_Py_RefTotal--;
1042+
#endif
1043+
#ifdef Py_TRACE_REFS
10411044
_Py_ForgetReference(unicode);
1045+
#endif
10421046

10431047
new_unicode = (PyObject *)PyObject_REALLOC(unicode, new_size);
10441048
if (new_unicode == NULL) {

0 commit comments

Comments
 (0)
0