Description
Feature or enhancement
Currently the calling APIs such as PyObject_VectorcallMethod
use _PyObject_GetMethod
to avoid creating a bound method object, however _PyObject_GetMethod
increfs and decrefs the object even when the underlying object supports deferred reference counting. This leads to reference counting contention on the object and it doesn't scale well in free threading. This API is heavily used by modules such as asyncio so it is important that _PyObject_GetMethod
should scale well with threads.
Lines 829 to 859 in 54a6875
Proposal:
To take advantage of deferred reference counting, we should add stack ref variant of _PyObject_GetMethod
and use it in all calling APIs to avoid reference counting contention on the function object.
Implementation:
- The new stack ref variant
_PyObject_GetMethodStackRef
will use_PyType_LookupStackRefAndVersion
when looking up method from type cache. - A new stack ref variant of
_PyObject_TryGetInstanceAttributeStackRef
will be added to to be used with_PyObject_GetMethodStackRef
. - Possibly we can avoid incref and decref when looking up the attribute on instance dict by delaying freeing of object's dictionary and using
_Py_dict_lookup_threadsafe_stackref
.Lines 1632 to 1640 in 54a6875