| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 206de41 commit 3ea488a
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 | // This value is added to `ob_ref_shared` for objects that use deferred | |
| 20 | 20 | // reference counting so that they are not immediately deallocated when the | |
@@ -291,7 +291,31 @@ extern bool _PyRefchain_IsTraced(PyInterpreterState *interp, PyObject *obj); | |||
| 291 | 291 | #ifndef Py_GIL_DISABLED | |
| 292 | 292 | # define _Py_INCREF_TYPE Py_INCREF | |
| 293 | 293 | # define _Py_DECREF_TYPE Py_DECREF | |
| 294 | + # define _Py_INCREF_CODE Py_INCREF | ||
| 295 | + # define _Py_DECREF_CODE Py_DECREF | ||
| 294 | 296 | #else | |
| 297 | + static inline void | ||
| 298 | + _Py_THREAD_INCREF_OBJECT(PyObject *obj, Py_ssize_t unique_id) | ||
| 299 | + { | ||
| 300 | + _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); | ||
| 301 | + | ||
| 302 | + // Unsigned comparison so that `unique_id=-1`, which indicates that | ||
| 303 | + // per-thread refcounting has been disabled on this object, is handled by | ||
| 304 | + // the "else". | ||
| 305 | + if ((size_t)unique_id < (size_t)tstate->refcounts.size) { | ||
| 306 | + # ifdef Py_REF_DEBUG | ||
| 307 | + _Py_INCREF_IncRefTotal(); | ||
| 308 | + # endif | ||
| 309 | + _Py_INCREF_STAT_INC(); | ||
| 310 | + tstate->refcounts.values[unique_id]++; | ||
| 311 | + } | ||
| 312 | + else { | ||
| 313 | + // The slow path resizes the per-thread refcount array if necessary. | ||
| 314 | + // It handles the unique_id=-1 case to keep the inlinable function smaller. | ||
| 315 | + _PyObject_ThreadIncrefSlow(obj, unique_id); | ||
| 316 | + } | ||
| 317 | + } | ||
| 318 | + | ||
| 295 | 319 | static inline void | |
| 296 | 320 | _Py_INCREF_TYPE(PyTypeObject *type) | |
| 297 | 321 | { | |
@@ -308,29 +332,38 @@ _Py_INCREF_TYPE(PyTypeObject *type) | |||
| 308 | 332 | # pragma GCC diagnostic push | |
| 309 | 333 | # pragma GCC diagnostic ignored "-Warray-bounds" | |
| 310 | 334 | #endif | |
| 335 | + _Py_THREAD_INCREF_OBJECT((PyObject *)type, ((PyHeapTypeObject *)type)->unique_id); | ||
| 336 | + #if defined(__GNUC__) && __GNUC__ >= 11 | ||
| 337 | + # pragma GCC diagnostic pop | ||
| 338 | + #endif | ||
| 339 | + } | ||
| 340 | + | ||
| 341 | + static inline void | ||
| 342 | + _Py_INCREF_CODE(PyCodeObject *co) | ||
| 343 | + { | ||
| 344 | + _Py_THREAD_INCREF_OBJECT((PyObject *)co, co->_co_unique_id); | ||
| 345 | + } | ||
| 311 | 346 | ||
| 347 | + static inline void | ||
| 348 | + _Py_THREAD_DECREF_OBJECT(PyObject *obj, Py_ssize_t unique_id) | ||
| 349 | + { | ||
| 312 | 350 | _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); | |
| 313 | - PyHeapTypeObject *ht = (PyHeapTypeObject *)type; | ||
| 314 | 351 | ||
| 315 | 352 | // Unsigned comparison so that `unique_id=-1`, which indicates that | |
| 316 | - // per-thread refcounting has been disabled on this type, is handled by | ||
| 353 | + // per-thread refcounting has been disabled on this object, is handled by | ||
| 317 | 354 | // the "else". | |
| 318 | - if ((size_t)ht->unique_id < (size_t)tstate->refcounts.size) { | ||
| 355 | + if ((size_t)unique_id < (size_t)tstate->refcounts.size) { | ||
| 319 | 356 | # ifdef Py_REF_DEBUG | |
| 320 | - _Py_INCREF_IncRefTotal(); | ||
| 357 | + _Py_DECREF_DecRefTotal(); | ||
| 321 | 358 | # endif | |
| 322 | - _Py_INCREF_STAT_INC(); | ||
| 323 | - tstate->refcounts.values[ht->unique_id]++; | ||
| 359 | + _Py_DECREF_STAT_INC(); | ||
| 360 | + tstate->refcounts.values[unique_id]--; | ||
| 324 | 361 | } | |
| 325 | 362 | else { | |
| 326 | - // The slow path resizes the thread-local refcount array if necessary. | ||
| 327 | - // It handles the unique_id=-1 case to keep the inlinable function smaller. | ||
| 328 | - _PyType_IncrefSlow(ht); | ||
| 363 | + // Directly decref the object if the id is not assigned or if | ||
| 364 | + // per-thread refcounting has been disabled on this object. | ||
| 365 | + Py_DECREF(obj); | ||
| 329 | 366 | } | |
| 330 | - | ||
| 331 | - #if defined(__GNUC__) && __GNUC__ >= 11 | ||
| 332 | - # pragma GCC diagnostic pop | ||
| 333 | - #endif | ||
| 334 | 367 | } | |
| 335 | 368 | ||
| 336 | 369 | static inline void | |
@@ -341,25 +374,14 @@ _Py_DECREF_TYPE(PyTypeObject *type) | |||
| 341 | 374 | _Py_DECREF_IMMORTAL_STAT_INC(); | |
| 342 | 375 | return; | |
| 343 | 376 | } | |
| 344 | - | ||
| 345 | - _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); | ||
| 346 | 377 | PyHeapTypeObject *ht = (PyHeapTypeObject *)type; | |
| 378 | + _Py_THREAD_DECREF_OBJECT((PyObject *)type, ht->unique_id); | ||
| 379 | + } | ||
| 347 | 380 | ||
| 348 | - // Unsigned comparison so that `unique_id=-1`, which indicates that | ||
| 349 | - // per-thread refcounting has been disabled on this type, is handled by | ||
| 350 | - // the "else". | ||
| 351 | - if ((size_t)ht->unique_id < (size_t)tstate->refcounts.size) { | ||
| 352 | - # ifdef Py_REF_DEBUG | ||
| 353 | - _Py_DECREF_DecRefTotal(); | ||
| 354 | - # endif | ||
| 355 | - _Py_DECREF_STAT_INC(); | ||
| 356 | - tstate->refcounts.values[ht->unique_id]--; | ||
| 357 | - } | ||
| 358 | - else { | ||
| 359 | - // Directly decref the type if the type id is not assigned or if | ||
| 360 | - // per-thread refcounting has been disabled on this type. | ||
| 361 | - Py_DECREF(type); | ||
| 362 | - } | ||
| 381 | + static inline void | ||
| 382 | + _Py_DECREF_CODE(PyCodeObject *co) | ||
| 383 | + { | ||
| 384 | + _Py_THREAD_DECREF_OBJECT((PyObject *)co, co->_co_unique_id); | ||
| 363 | 385 | } | |
| 364 | 386 | #endif | |
| 365 | 387 | ||
| 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 | ||
@@ -6043,7 +6043,7 @@ type_dealloc(PyObject *self) | |||
| 6043 | 6043 | Py_XDECREF(et->ht_module); | |
| 6044 | 6044 | PyMem_Free(et->_ht_tpname); | |
| 6045 | 6045 | #ifdef Py_GIL_DISABLED | |
| 6046 | - _PyObject_ReleaseUniqueId(et->unique_id); | ||
| 6046 | + assert(et->unique_id == -1); | ||
| 6047 | 6047 | #endif | |
| 6048 | 6048 | et->ht_token = NULL; | |
| 6049 | 6049 | 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