Skip to content

Lock contention inside _PyType_LookupRef #132380

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

Open
nascheme opened this issue Apr 11, 2025 · 1 comment
Open

Lock contention inside _PyType_LookupRef #132380

nascheme opened this issue Apr 11, 2025 · 1 comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage topic-free-threading type-feature A feature request or enhancement

Comments

@nascheme
Copy link
Member

nascheme commented Apr 11, 2025

Feature or enhancement

Proposal:

As part of adding free-threading support to LibCST, we noticed there is a lot of lock contention on TYPE_LOCK inside the _PyType_LookupRef function. In the LibCST, the common "visitor" pattern is used. For example, in the _visitors.py module there is the code:

visit_func = getattr(self, f"visit_{type(node).__name__}", None)

The second argument to getattr() is a non-interned string and it causes the cached and lock-free path of _PyType_LookupRef() never to be taken. Instead, the TYPE_LOCK mutex is acquired on each lookup. This obviously scales very badly if there are multiple threads looking up class methods using this pattern.

Testing was done with Python 3.13 but I believe the same issue exists with 3.14.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@nascheme nascheme added performance Performance or resource usage topic-free-threading type-feature A feature request or enhancement labels Apr 11, 2025
nascheme added a commit to nascheme/cpython that referenced this issue Apr 11, 2025
Change find_name_in_mro() so we don't need hold TYPE_LOCK.  Change
_PyType_LookupRef() so that TYPE_LOCK is acquired only if the type
doesn't have a version tag already assigned.  If the version is already
assigned, we can avoid acquiring TYPE_LOCK.
@nascheme
Copy link
Member Author

GH-127266 includes changes that effectively does the same thing as GH-132380 (avoid taking lock if the type version is already assigned).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage topic-free-threading type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants