@@ -299,12 +299,6 @@ Py_ssize_t _Py_ExplicitMergeRefcount(PyObject *op, Py_ssize_t extra);
299299extern int _PyType_CheckConsistency (PyTypeObject * type );
300300extern int _PyDict_CheckConsistency (PyObject * mp , int check_content );
301301
302- /* Update the Python traceback of an object. This function must be called
303- when a memory block is reused from a free list.
304-
305- Internal function called by _Py_NewReference(). */
306- extern int _PyTraceMalloc_TraceRef (PyObject * op , PyRefTracerEvent event , void * );
307-
308302// Fast inlined version of PyType_HasFeature()
309303static inline int
310304_PyType_HasFeature (PyTypeObject * type , unsigned long feature ) {
@@ -342,20 +336,20 @@ _Py_THREAD_INCREF_OBJECT(PyObject *obj, Py_ssize_t unique_id)
342336{
343337 _PyThreadStateImpl * tstate = (_PyThreadStateImpl * )_PyThreadState_GET ();
344338
345- // Unsigned comparison so that `unique_id=-1`, which indicates that
346- // per-thread refcounting has been disabled on this object, is handled by
347- // the "else".
348- if (( size_t ) unique_id < (size_t )tstate -> refcounts .size ) {
339+ // The table index is `unique_id - 1` because 0 is not a valid unique id.
340+ // Unsigned comparison so that `idx=-1` is handled by the "else".
341+ size_t idx = ( size_t )( unique_id - 1 );
342+ if (idx < (size_t )tstate -> refcounts .size ) {
349343# ifdef Py_REF_DEBUG
350344 _Py_INCREF_IncRefTotal ();
351345# endif
352346 _Py_INCREF_STAT_INC ();
353- tstate -> refcounts .values [unique_id ]++ ;
347+ tstate -> refcounts .values [idx ]++ ;
354348 }
355349 else {
356350 // The slow path resizes the per-thread refcount array if necessary.
357- // It handles the unique_id=-1 case to keep the inlinable function smaller.
358- _PyObject_ThreadIncrefSlow (obj , unique_id );
351+ // It handles the unique_id=0 case to keep the inlinable function smaller.
352+ _PyObject_ThreadIncrefSlow (obj , idx );
359353 }
360354}
361355
@@ -392,15 +386,15 @@ _Py_THREAD_DECREF_OBJECT(PyObject *obj, Py_ssize_t unique_id)
392386{
393387 _PyThreadStateImpl * tstate = (_PyThreadStateImpl * )_PyThreadState_GET ();
394388
395- // Unsigned comparison so that `unique_id=-1`, which indicates that
396- // per-thread refcounting has been disabled on this object, is handled by
397- // the "else".
398- if (( size_t ) unique_id < (size_t )tstate -> refcounts .size ) {
389+ // The table index is `unique_id - 1` because 0 is not a valid unique id.
390+ // Unsigned comparison so that `idx=-1` is handled by the "else".
391+ size_t idx = ( size_t )( unique_id - 1 );
392+ if (idx < (size_t )tstate -> refcounts .size ) {
399393# ifdef Py_REF_DEBUG
400394 _Py_DECREF_DecRefTotal ();
401395# endif
402396 _Py_DECREF_STAT_INC ();
403- tstate -> refcounts .values [unique_id ]-- ;
397+ tstate -> refcounts .values [idx ]-- ;
404398 }
405399 else {
406400 // Directly decref the object if the id is not assigned or if
0 commit comments