mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
gh-124218: Use per-thread refcounts for code objects (#125216)
Use per-thread refcounting for the reference from function objects to their corresponding code object. This can be a source of contention when frequently creating nested functions. Deferred refcounting alone isn't a great fit here because these references are on the heap and may be modified by other libraries.
This commit is contained in:
parent
206de4155b
commit
3ea488aac4
11 changed files with 126 additions and 83 deletions
|
@ -15,7 +15,7 @@
|
|||
#include "pycore_tstate.h" // _PyThreadStateImpl
|
||||
#include "pycore_weakref.h" // _PyWeakref_ClearRef()
|
||||
#include "pydtrace.h"
|
||||
#include "pycore_uniqueid.h" // _PyType_MergeThreadLocalRefcounts
|
||||
#include "pycore_uniqueid.h" // _PyObject_MergeThreadLocalRefcounts()
|
||||
|
||||
#ifdef Py_GIL_DISABLED
|
||||
|
||||
|
@ -215,15 +215,10 @@ disable_deferred_refcounting(PyObject *op)
|
|||
op->ob_gc_bits &= ~_PyGC_BITS_DEFERRED;
|
||||
op->ob_ref_shared -= _Py_REF_SHARED(_Py_REF_DEFERRED, 0);
|
||||
merge_refcount(op, 0);
|
||||
}
|
||||
|
||||
// Heap types also use per-thread refcounting -- disable it here.
|
||||
if (PyType_Check(op)) {
|
||||
if (PyType_HasFeature((PyTypeObject *)op, Py_TPFLAGS_HEAPTYPE)) {
|
||||
PyHeapTypeObject *ht = (PyHeapTypeObject *)op;
|
||||
_PyObject_ReleaseUniqueId(ht->unique_id);
|
||||
ht->unique_id = -1;
|
||||
}
|
||||
// Heap types and code objects also use per-thread refcounting, which
|
||||
// should also be disabled when we turn off deferred refcounting.
|
||||
_PyObject_DisablePerThreadRefcounting(op);
|
||||
}
|
||||
|
||||
// Generators and frame objects may contain deferred references to other
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue