From f4997bb3ac961d6aaf07ce650cd074e28ce6ccd0 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Tue, 24 Sep 2024 13:08:18 -0700 Subject: 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. --- Python/sysmodule.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 887a916563a..ac343a8048e 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -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; -- cgit v1.2.3