| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9bda775 commit 018b2a1
11 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -132,6 +132,7 @@ typedef struct { | |||
| 132 | 132 | _PyCoCached *_co_cached; /* cached co_* attributes */ \ | |
| 133 | 133 | uintptr_t _co_instrumentation_version; /* current instrumentation version */ \ | |
| 134 | 134 | _PyCoMonitoringData *_co_monitoring; /* Monitoring data */ \ | |
| 135 | + Py_ssize_t _co_unique_id; /* ID used for per-thread refcounting */ \ | ||
| 135 | 136 | int _co_firsttraceable; /* index of first traceable instruction */ \ | |
| 136 | 137 | /* Scratch space for extra data relating to the code object. \ | |
| 137 | 138 | Type is a void* to keep the format private in codeobject.c to force \ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -272,7 +272,7 @@ typedef struct _heaptypeobject { | |||
| 272 | 272 | void *ht_token; // Storage for the "Py_tp_token" slot | |
| 273 | 273 | struct _specialization_cache _spec_cache; // For use by the specializer. | |
| 274 | 274 | #ifdef Py_GIL_DISABLED | |
| 275 | - Py_ssize_t unique_id; // ID used for thread-local refcounting | ||
| 275 | + Py_ssize_t unique_id; // ID used for per-thread refcounting | ||
| 276 | 276 | #endif | |
| 277 | 277 | /* here are optional user slots, followed by the members. */ | |
| 278 | 278 | } PyHeapTypeObject; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,7 +14,7 @@ extern "C" { | |||
| 14 | 14 | #include "pycore_interp.h" // PyInterpreterState.gc | |
| 15 | 15 | #include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_STORE_PTR_RELAXED | |
| 16 | 16 | #include "pycore_pystate.h" // _PyInterpreterState_GET() | |
| 17 | - #include "pycore_uniqueid.h" // _PyType_IncrefSlow | ||
| 17 | + #include "pycore_uniqueid.h" // _PyObject_ThreadIncrefSlow() | ||
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | 20 | #define _Py_IMMORTAL_REFCNT_LOOSE ((_Py_IMMORTAL_REFCNT >> 1) + 1) | |
@@ -311,7 +311,31 @@ extern bool _PyRefchain_IsTraced(PyInterpreterState *interp, PyObject *obj); | |||
| 311 | 311 | #ifndef Py_GIL_DISABLED | |
| 312 | 312 | # define _Py_INCREF_TYPE Py_INCREF | |
| 313 | 313 | # define _Py_DECREF_TYPE Py_DECREF | |
| 314 | + # define _Py_INCREF_CODE Py_INCREF | ||
| 315 | + # define _Py_DECREF_CODE Py_DECREF | ||
| 314 | 316 | #else | |
| 317 | + static inline void | ||
| 318 | + _Py_THREAD_INCREF_OBJECT(PyObject *obj, Py_ssize_t unique_id) | ||
| 319 | + { | ||
| 320 | + _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); | ||
| 321 | + | ||
| 322 | + // Unsigned comparison so that `unique_id=-1`, which indicates that | ||
| 323 | + // per-thread refcounting has been disabled on this object, is handled by | ||
| 324 | + // the "else". | ||
| 325 | + if ((size_t)unique_id < (size_t)tstate->refcounts.size) { | ||
| 326 | + # ifdef Py_REF_DEBUG | ||
| 327 | + _Py_INCREF_IncRefTotal(); | ||
| 328 | + # endif | ||
| 329 | + _Py_INCREF_STAT_INC(); | ||
| 330 | + tstate->refcounts.values[unique_id]++; | ||
| 331 | + } | ||
| 332 | + else { | ||
| 333 | + // The slow path resizes the thread-local refcount array if necessary. | ||
| 334 | + // It handles the unique_id=-1 case to keep the inlinable function smaller. | ||
| 335 | + _PyObject_ThreadIncrefSlow(obj, unique_id); | ||
| 336 | + } | ||
| 337 | + } | ||
| 338 | + | ||
| 315 | 339 | static inline void | |
| 316 | 340 | _Py_INCREF_TYPE(PyTypeObject *type) | |
| 317 | 341 | { | |
@@ -328,29 +352,38 @@ _Py_INCREF_TYPE(PyTypeObject *type) | |||
| 328 | 352 | # pragma GCC diagnostic push | |
| 329 | 353 | # pragma GCC diagnostic ignored "-Warray-bounds" | |
| 330 | 354 | #endif | |
| 355 | + _Py_THREAD_INCREF_OBJECT((PyObject *)type, ((PyHeapTypeObject *)type)->unique_id); | ||
| 356 | + #if defined(__GNUC__) && __GNUC__ >= 11 | ||
| 357 | + # pragma GCC diagnostic pop | ||
| 358 | + #endif | ||
| 359 | + } | ||
| 331 | 360 | ||
| 361 | + static inline void | ||
| 362 | + _Py_INCREF_CODE(PyCodeObject *co) | ||
| 363 | + { | ||
| 364 | + _Py_THREAD_INCREF_OBJECT((PyObject *)co, co->_co_unique_id); | ||
| 365 | + } | ||
| 366 | + | ||
| 367 | + static inline void | ||
| 368 | + _Py_THREAD_DECREF_OBJECT(PyObject *obj, Py_ssize_t unique_id) | ||
| 369 | + { | ||
| 332 | 370 | _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); | |
| 333 | - PyHeapTypeObject *ht = (PyHeapTypeObject *)type; | ||
| 334 | 371 | ||
| 335 | 372 | // Unsigned comparison so that `unique_id=-1`, which indicates that | |
| 336 | - // per-thread refcounting has been disabled on this type, is handled by | ||
| 373 | + // per-thread refcounting has been disabled on this object, is handled by | ||
| 337 | 374 | // the "else". | |
| 338 | - if ((size_t)ht->unique_id < (size_t)tstate->refcounts.size) { | ||
| 375 | + if ((size_t)unique_id < (size_t)tstate->refcounts.size) { | ||
| 339 | 376 | # ifdef Py_REF_DEBUG | |
| 340 | - _Py_INCREF_IncRefTotal(); | ||
| 377 | + _Py_DECREF_DecRefTotal(); | ||
| 341 | 378 | # endif | |
| 342 | - _Py_INCREF_STAT_INC(); | ||
| 343 | - tstate->refcounts.values[ht->unique_id]++; | ||
| 379 | + _Py_DECREF_STAT_INC(); | ||
| 380 | + tstate->refcounts.values[unique_id]--; | ||
| 344 | 381 | } | |
| 345 | 382 | else { | |
| 346 | - // The slow path resizes the thread-local refcount array if necessary. | ||
| 347 | - // It handles the unique_id=-1 case to keep the inlinable function smaller. | ||
| 348 | - _PyType_IncrefSlow(ht); | ||
| 383 | + // Directly decref the type if the type id is not assigned or if | ||
| 384 | + // per-thread refcounting has been disabled on this type. | ||
| 385 | + Py_DECREF(obj); | ||
| 349 | 386 | } | |
| 350 | - | ||
| 351 | - #if defined(__GNUC__) && __GNUC__ >= 11 | ||
| 352 | - # pragma GCC diagnostic pop | ||
| 353 | - #endif | ||
| 354 | 387 | } | |
| 355 | 388 | ||
| 356 | 389 | static inline void | |
@@ -361,25 +394,14 @@ _Py_DECREF_TYPE(PyTypeObject *type) | |||
| 361 | 394 | _Py_DECREF_IMMORTAL_STAT_INC(); | |
| 362 | 395 | return; | |
| 363 | 396 | } | |
| 364 | - | ||
| 365 | - _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); | ||
| 366 | 397 | PyHeapTypeObject *ht = (PyHeapTypeObject *)type; | |
| 398 | + _Py_THREAD_DECREF_OBJECT((PyObject *)type, ht->unique_id); | ||
| 399 | + } | ||
| 367 | 400 | ||
| 368 | - // Unsigned comparison so that `unique_id=-1`, which indicates that | ||
| 369 | - // per-thread refcounting has been disabled on this type, is handled by | ||
| 370 | - // the "else". | ||
| 371 | - if ((size_t)ht->unique_id < (size_t)tstate->refcounts.size) { | ||
| 372 | - # ifdef Py_REF_DEBUG | ||
| 373 | - _Py_DECREF_DecRefTotal(); | ||
| 374 | - # endif | ||
| 375 | - _Py_DECREF_STAT_INC(); | ||
| 376 | - tstate->refcounts.values[ht->unique_id]--; | ||
| 377 | - } | ||
| 378 | - else { | ||
| 379 | - // Directly decref the type if the type id is not assigned or if | ||
| 380 | - // per-thread refcounting has been disabled on this type. | ||
| 381 | - Py_DECREF(type); | ||
| 382 | - } | ||
| 401 | + static inline void | ||
| 402 | + _Py_DECREF_CODE(PyCodeObject *co) | ||
| 403 | + { | ||
| 404 | + _Py_THREAD_DECREF_OBJECT((PyObject *)co, co->_co_unique_id); | ||
| 383 | 405 | } | |
| 384 | 406 | #endif | |
| 385 | 407 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,7 +49,7 @@ struct _Py_unique_id_pool { | |||
| 49 | 49 | extern Py_ssize_t _PyObject_AssignUniqueId(PyObject *obj); | |
| 50 | 50 | ||
| 51 | 51 | // Releases the allocated id back to the pool. | |
| 52 | - extern void _PyObject_ReleaseUniqueId(Py_ssize_t unique_id); | ||
| 52 | + extern void _PyObject_DisablePerThreadRefcounting(PyObject *obj); | ||
| 53 | 53 | ||
| 54 | 54 | // Merges the per-thread reference counts into the corresponding objects. | |
| 55 | 55 | extern void _PyObject_MergePerThreadRefcounts(_PyThreadStateImpl *tstate); | |
@@ -61,8 +61,8 @@ extern void _PyObject_FinalizePerThreadRefcounts(_PyThreadStateImpl *tstate); | |||
| 61 | 61 | // Frees the interpreter's pool of type ids. | |
| 62 | 62 | extern void _PyObject_FinalizeUniqueIdPool(PyInterpreterState *interp); | |
| 63 | 63 | ||
| 64 | - // Increfs the type, resizing the per-thread refcount array if necessary. | ||
| 65 | - PyAPI_FUNC(void) _PyType_IncrefSlow(PyHeapTypeObject *type); | ||
| 64 | + // Increfs the object, resizing the thread-local refcount array if necessary. | ||
| 65 | + PyAPI_FUNC(void) _PyObject_ThreadIncrefSlow(PyObject *obj, Py_ssize_t unique_id); | ||
| 66 | 66 | ||
| 67 | 67 | #endif /* Py_GIL_DISABLED */ | |
| 68 | 68 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,7 @@ | |||
| 14 | 14 | #include "pycore_pystate.h" // _PyInterpreterState_GET() | |
| 15 | 15 | #include "pycore_setobject.h" // _PySet_NextEntry() | |
| 16 | 16 | #include "pycore_tuple.h" // _PyTuple_ITEMS() | |
| 17 | + #include "pycore_uniqueid.h" // _PyObject_AssignUniqueId() | ||
| 17 | 18 | #include "clinic/codeobject.c.h" | |
| 18 | 19 | ||
| 19 | 20 | static const char * | |
@@ -676,7 +677,7 @@ _PyCode_New(struct _PyCodeConstructor *con) | |||
| 676 | 677 | } | |
| 677 | 678 | init_code(co, con); | |
| 678 | 679 | #ifdef Py_GIL_DISABLED | |
| 679 | - _PyObject_SetDeferredRefcount((PyObject *)co); | ||
| 680 | + co->_co_unique_id = _PyObject_AssignUniqueId((PyObject *)co); | ||
| 680 | 681 | _PyObject_GC_TRACK(co); | |
| 681 | 682 | #endif | |
| 682 | 683 | Py_XDECREF(replacement_locations); | |
@@ -1864,6 +1865,9 @@ code_dealloc(PyCodeObject *co) | |||
| 1864 | 1865 | Py_XDECREF(co->co_qualname); | |
| 1865 | 1866 | Py_XDECREF(co->co_linetable); | |
| 1866 | 1867 | Py_XDECREF(co->co_exceptiontable); | |
| 1868 | + #ifdef Py_GIL_DISABLED | ||
| 1869 | + assert(co->_co_unique_id == -1); | ||
| 1870 | + #endif | ||
| 1867 | 1871 | if (co->_co_cached != NULL) { | |
| 1868 | 1872 | Py_XDECREF(co->_co_cached->_co_code); | |
| 1869 | 1873 | Py_XDECREF(co->_co_cached->_co_cellvars); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -116,7 +116,8 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr) | |||
| 116 | 116 | op->func_builtins = Py_NewRef(constr->fc_builtins); | |
| 117 | 117 | op->func_name = Py_NewRef(constr->fc_name); | |
| 118 | 118 | op->func_qualname = Py_NewRef(constr->fc_qualname); | |
| 119 | - op->func_code = Py_NewRef(constr->fc_code); | ||
| 119 | + _Py_INCREF_CODE((PyCodeObject *)constr->fc_code); | ||
| 120 | + op->func_code = constr->fc_code; | ||
| 120 | 121 | op->func_defaults = Py_XNewRef(constr->fc_defaults); | |
| 121 | 122 | op->func_kwdefaults = Py_XNewRef(constr->fc_kwdefaults); | |
| 122 | 123 | op->func_closure = Py_XNewRef(constr->fc_closure); | |
@@ -146,7 +147,8 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname | |||
| 146 | 147 | ||
| 147 | 148 | PyThreadState *tstate = _PyThreadState_GET(); | |
| 148 | 149 | ||
| 149 | - PyCodeObject *code_obj = (PyCodeObject *)Py_NewRef(code); | ||
| 150 | + PyCodeObject *code_obj = (PyCodeObject *)code; | ||
| 151 | + _Py_INCREF_CODE(code_obj); | ||
| 150 | 152 | ||
| 151 | 153 | assert(code_obj->co_name != NULL); | |
| 152 | 154 | PyObject *name = Py_NewRef(code_obj->co_name); | |
@@ -1094,7 +1096,7 @@ func_dealloc(PyObject *self) | |||
| 1094 | 1096 | } | |
| 1095 | 1097 | (void)func_clear((PyObject*)op); | |
| 1096 | 1098 | // These aren't cleared by func_clear(). | |
| 1097 | - Py_DECREF(op->func_code); | ||
| 1099 | + _Py_DECREF_CODE((PyCodeObject *)op->func_code); | ||
| 1098 | 1100 | Py_DECREF(op->func_name); | |
| 1099 | 1101 | Py_DECREF(op->func_qualname); | |
| 1100 | 1102 | PyObject_GC_Del(op); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5025,7 +5025,7 @@ PyType_FromMetaclass( | |||
| 5025 | 5025 | type->tp_dictoffset = dictoffset; | |
| 5026 | 5026 | ||
| 5027 | 5027 | #ifdef Py_GIL_DISABLED | |
| 5028 | - // Assign a type id to enable thread-local refcounting | ||
| 5028 | + // Assign a unique id to enable per-thread refcounting | ||
| 5029 | 5029 | res->unique_id = _PyObject_AssignUniqueId((PyObject *)res); | |
| 5030 | 5030 | #endif | |
| 5031 | 5031 | ||
@@ -6080,7 +6080,7 @@ type_dealloc(PyObject *self) | |||
| 6080 | 6080 | Py_XDECREF(et->ht_module); | |
| 6081 | 6081 | PyMem_Free(et->_ht_tpname); | |
| 6082 | 6082 | #ifdef Py_GIL_DISABLED | |
| 6083 | - _PyObject_ReleaseUniqueId(et->unique_id); | ||
| 6083 | + assert(et->unique_id == -1); | ||
| 6084 | 6084 | #endif | |
| 6085 | 6085 | et->ht_token = NULL; | |
| 6086 | 6086 | Py_TYPE(type)->tp_free((PyObject *)type); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,7 @@ | |||
| 15 | 15 | #include "pycore_tstate.h" // _PyThreadStateImpl | |
| 16 | 16 | #include "pycore_weakref.h" // _PyWeakref_ClearRef() | |
| 17 | 17 | #include "pydtrace.h" | |
| 18 | - #include "pycore_uniqueid.h" // _PyType_MergeThreadLocalRefcounts | ||
| 18 | + #include "pycore_uniqueid.h" // _PyObject_MergeThreadLocalRefcounts() | ||
| 19 | 19 | ||
| 20 | 20 | #ifdef Py_GIL_DISABLED | |
| 21 | 21 | ||
@@ -215,15 +215,10 @@ disable_deferred_refcounting(PyObject *op) | |||
| 215 | 215 | op->ob_gc_bits &= ~_PyGC_BITS_DEFERRED; | |
| 216 | 216 | op->ob_ref_shared -= _Py_REF_SHARED(_Py_REF_DEFERRED, 0); | |
| 217 | 217 | merge_refcount(op, 0); | |
| 218 | - } | ||
| 219 | 218 | ||
| 220 | - // Heap types also use per-thread refcounting -- disable it here. | ||
| 221 | - if (PyType_Check(op)) { | ||
| 222 | - if (PyType_HasFeature((PyTypeObject *)op, Py_TPFLAGS_HEAPTYPE)) { | ||
| 223 | - PyHeapTypeObject *ht = (PyHeapTypeObject *)op; | ||
| 224 | - _PyObject_ReleaseUniqueId(ht->unique_id); | ||
| 225 | - ht->unique_id = -1; | ||
| 226 | - } | ||
| 219 | + // Heap types and code objects also use per-thread refcounting, which | ||
| 220 | + // should also be disabled when we turn off deferred refcounting. | ||
| 221 | + _PyObject_DisablePerThreadRefcounting(op); | ||
| 227 | 222 | } | |
| 228 | 223 | ||
| 229 | 224 | // Generators and frame objects may contain deferred references to other | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,7 +28,7 @@ | |||
| 28 | 28 | #include "pycore_sliceobject.h" // _PySlice_Fini() | |
| 29 | 29 | #include "pycore_sysmodule.h" // _PySys_ClearAuditHooks() | |
| 30 | 30 | #include "pycore_traceback.h" // _Py_DumpTracebackThreads() | |
| 31 | - #include "pycore_uniqueid.h" // _PyType_FinalizeIdPool() | ||
| 31 | + #include "pycore_uniqueid.h" // _PyObject_FinalizeUniqueIdPool() | ||
| 32 | 32 | #include "pycore_typeobject.h" // _PyTypes_InitTypes() | |
| 33 | 33 | #include "pycore_typevarobject.h" // _Py_clear_generic_types() | |
| 34 | 34 | #include "pycore_unicodeobject.h" // _PyUnicode_InitTypes() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,7 +20,7 @@ | |||
| 20 | 20 | #include "pycore_runtime_init.h" // _PyRuntimeState_INIT | |
| 21 | 21 | #include "pycore_sysmodule.h" // _PySys_Audit() | |
| 22 | 22 | #include "pycore_obmalloc.h" // _PyMem_obmalloc_state_on_heap() | |
| 23 | - #include "pycore_uniqueid.h" // _PyType_FinalizeThreadLocalRefcounts() | ||
| 23 | + #include "pycore_uniqueid.h" // _PyObject_FinalizePerThreadRefcounts() | ||
| 24 | 24 | ||
| 25 | 25 | /* -------------------------------------------------------------------------- | |
| 26 | 26 | CAUTION | |
| Back | FazBrowse Home | New Git URL |
0 commit comments