Uh oh!
There was an error while loading. Please reload this page.
gh-139103: Improve namedtuple scaling in free-threaded build - #144332
Conversation
Add `_Py_type_getattro_stackref`, a variant of type attribute lookup that returns `_PyStackRef` instead of `PyObject*`. This allows returning deferred references in the free-threaded build, reducing reference count contention when accessing type attributes. This significantly improves scaling of namedtuple instantiation across multiple threads.
Uh oh!
There was an error while loading. Please reload this page.
colesbury
commented
Feb 2, 2026
FYI, I think I'm going to try splitting out parts of this into other PRs. |
vstinner
commented
Feb 2, 2026
Thanks, since this PR is quite big :-) |
| staticmethod *sm = (staticmethod *) | ||
| PyType_GenericAlloc(&PyStaticMethod_Type, 0); | ||
| if (sm != NULL) { | ||
| _PyObject_SetDeferredRefcount((PyObject *)sm); |
There was a problem hiding this comment.
Should we also call it in sm_init()? If not, should we move this _PyObject_SetDeferredRefcount() call to typeobject.c after the two PyStaticMethod_New() calls?
There was a problem hiding this comment.
Hmmm... maybe? We'll need to guard the call to _PyObject_SetDeferredRefcount() because calling it multiple times on the same object will trigger an assertion failure.
I'll update this after your PR is merged
There was a problem hiding this comment.
I merged for classmethod/staticmethod fix in the main branch.
There was a problem hiding this comment.
I've put the calls to _PyObject_SetDeferredRefcount() in sm_new() and PyStaticMethod_New() so that it's called exactly once during construction.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Victor Stinner <vstinner@python.org>
Uh oh!
There was an error while loading. Please reload this page.
…ythongh-144332) Add `_Py_type_getattro_stackref`, a variant of type attribute lookup that returns `_PyStackRef` instead of `PyObject*`. This allows returning deferred references in the free-threaded build, reducing reference count contention when accessing type attributes. This significantly improves scaling of namedtuple instantiation across multiple threads. * Add blurb * Rename PyObject_GetAttrStackRef to _PyObject_GetAttrStackRef * Apply suggestion from @vstinner Co-authored-by: Victor Stinner <vstinner@python.org> * Apply suggestion from @vstinner Co-authored-by: Victor Stinner <vstinner@python.org> * format * Update Include/internal/pycore_function.h Co-authored-by: Victor Stinner <vstinner@python.org> --------- Co-authored-by: Victor Stinner <vstinner@python.org>
…ythongh-144332) Add `_Py_type_getattro_stackref`, a variant of type attribute lookup that returns `_PyStackRef` instead of `PyObject*`. This allows returning deferred references in the free-threaded build, reducing reference count contention when accessing type attributes. This significantly improves scaling of namedtuple instantiation across multiple threads. * Add blurb * Rename PyObject_GetAttrStackRef to _PyObject_GetAttrStackRef * Apply suggestion from @vstinner Co-authored-by: Victor Stinner <vstinner@python.org> * Apply suggestion from @vstinner Co-authored-by: Victor Stinner <vstinner@python.org> * format * Update Include/internal/pycore_function.h Co-authored-by: Victor Stinner <vstinner@python.org> --------- Co-authored-by: Victor Stinner <vstinner@python.org>
Add
_Py_type_getattro_stackref, a variant of type attribute lookup that returns_PyStackRefinstead ofPyObject*. This allows returning deferred references in the free-threaded build, reducing reference count contention when accessing type attributes.This improves scaling of namedtuple instantiation across multiple threads.