-
-
Notifications
You must be signed in to change notification settings - Fork 34k
gh-139103: Improve namedtuple scaling in free-threaded build #144332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
|
FYI, I think I'm going to try splitting out parts of this into other PRs. |
Thanks, since this PR is quite big :-) |
Objects/funcobject.c
Outdated
| staticmethod *sm = (staticmethod *) | ||
| PyType_GenericAlloc(&PyStaticMethod_Type, 0); | ||
| if (sm != NULL) { | ||
| _PyObject_SetDeferredRefcount((PyObject *)sm); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner
left a comment
There was a problem hiding this 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>
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.