Skip to content

gh-139103: Improve namedtuple scaling in free-threaded build#144332

Merged
colesbury merged 9 commits intopython:mainfrom
colesbury:gh-139103-named-tuple
Feb 6, 2026
Merged

gh-139103: Improve namedtuple scaling in free-threaded build#144332
colesbury merged 9 commits intopython:mainfrom
colesbury:gh-139103-named-tuple

Conversation

@colesbury
Copy link
Contributor

@colesbury colesbury commented Jan 29, 2026

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 improves scaling of namedtuple instantiation across multiple threads.

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.
@colesbury
Copy link
Contributor Author

FYI, I think I'm going to try splitting out parts of this into other PRs.

@vstinner
Copy link
Member

vstinner commented Feb 2, 2026

FYI, I think I'm going to try splitting out parts of this into other PRs.

Thanks, since this PR is quite big :-)

@colesbury colesbury marked this pull request as ready for review February 3, 2026 18:06
@colesbury colesbury requested a review from mpage February 3, 2026 18:06
staticmethod *sm = (staticmethod *)
PyType_GenericAlloc(&PyStaticMethod_Type, 0);
if (sm != NULL) {
_PyObject_SetDeferredRefcount((PyObject *)sm);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merged for classmethod/staticmethod fix in the main branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've put the calls to _PyObject_SetDeferredRefcount() in sm_new() and PyStaticMethod_New() so that it's called exactly once during construction.

colesbury and others added 3 commits February 5, 2026 10:52
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The C changes LGTM.

Co-authored-by: Victor Stinner <vstinner@python.org>
@colesbury colesbury merged commit d891b2b into python:main Feb 6, 2026
71 checks passed
@colesbury colesbury deleted the gh-139103-named-tuple branch February 6, 2026 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants