Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-132380: Avoid locking in type lookup.#135112

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
nascheme wants to merge1 commit intopython:main
base:main
Choose a base branch
Loading
fromnascheme:gh-132380-tp-lookup-no-lock

Conversation

nascheme
Copy link
Member

@naschemenascheme commentedJun 3, 2025
edited
Loading

This is a slightly modified version ofGH-132381 but based on the 'main' branch instead of 3.13. I originallyclosedGH-132381 because Sam had some concerns about the approach. However, on re-visting it, I still think this change is worth considering. It helps the non-interned string lookup case and should also help in general due to less contention on TYPE_LOCK.

If the type lookup cache (mcache) is missed, do not acquire TYPE_LOCK unless we need to assign a type version. We actually don't need to lock in order to do the MRO attribute lookup.

This avoids lock contention outlined inGH-132380. Performance results based on running LibCST on the numpy source:

Time [s]Config
35.9Main branch, free-threaded build
23.6Above config, with this PR applied
18.2Main branch, free-threaded with-X gil=1 (multi-process)

In a previous version of this PR, Sam was concerned about the addition of the_PyType_GetMRO() call, potentially causing refcount contention. I don't think that's a problem since the previous code immediately calledPy_INCREF() on the mro tuple and so this should make it no worse in that respect.

Sam also made the comment:

I think this splits up the (slow path) lookup and update of the MRO cache into two separate lock acquisitions. That doesn't seem safe to me. I think that would allow interleavings in which we permanently replace a good cache entry with a stale entry.

The lock is only held when assigning the type version. The MRO lookup and cache update are done without a lock. It could happen that we add an entry to the cache with an out-of-date (already stale) type version number. That has the downside of potentially replacing a good entry from the cache with a useless one. However, I think this case is so rare that it doesn't matter in practice and it is more efficient not to handle this case. The replacement is not permanent. The next time the "good" item is looked up, it will be re-added to the cache, replacing the stale entry. So, unless the type version is changing on every lookup, the addition of the stale entry is temporary.

We could handle it explicitly by re-checking the current type version after thefind_name_in_mro() call but I think that would overall decrease performance. I didn't try benchmarking this though. The cache miss path is already pretty slow so probably it won't hurt much to re-check the version.

pyperformance results

This attached script shows a behavioral difference due to this change. Since the type objects are allowed to be mutated while the MRO lookup is happening, you can get anAttributeError running this script while in previous versions I don't think that was possible. At least, not without having__hash__ or similar methods that mutate the type objects.

race_mro_lookup.py.txt

If the type lookup cache (mcache) is missed, do not acquire TYPE_LOCKunless we need to assign a type version.  We actually don't need to lockin order to do the MRO attribute lookup.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon will be requested when the pull request is marked ready for reviewmarkshannon is a code owner

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

1 participant
@nascheme

[8]ページ先頭

©2009-2025 Movatter.jp