gh-123923: Defer refcounting for f_funcobj in _PyInterpreterFrame (#124026)

Use a `_PyStackRef` and defer the reference to `f_funcobj` when
possible. This avoids some reference count contention in the common case
of executing the same code object from multiple threads concurrently in
the free-threaded build.
This commit is contained in:
Sam Gross 2024-09-24 13:08:18 -07:00 committed by GitHub
parent d3c76dff44
commit f4997bb3ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 143 additions and 137 deletions

View file

@ -2384,10 +2384,11 @@ sys__getframemodulename_impl(PyObject *module, int depth)
while (f && (_PyFrame_IsIncomplete(f) || depth-- > 0)) {
f = f->previous;
}
if (f == NULL || f->f_funcobj == NULL) {
if (f == NULL || PyStackRef_IsNull(f->f_funcobj)) {
Py_RETURN_NONE;
}
PyObject *r = PyFunction_GetModule(f->f_funcobj);
PyObject *func = PyStackRef_AsPyObjectBorrow(f->f_funcobj);
PyObject *r = PyFunction_GetModule(func);
if (!r) {
PyErr_Clear();
r = Py_None;