gh-150902: Optimize PyCriticalSection2 to skip locking the same locks as the ones held by the current CS2#151085
Merged
Conversation
…me locks as the ones held by the current CS2
colesbury
approved these changes
Jun 16, 2026
colesbury
left a comment
Contributor
There was a problem hiding this comment.
Looks good, a few comments from Claude:
1. Stale comment in _PyCriticalSection2_End (pycore_critical_section.h:199-202). This comment is now inaccurate and should be updated as part of this PR:
// if mutex1 is NULL, we used the fast path in
// _PyCriticalSection_BeginSlow for mutexes that are already held,
// which should only happen when mutex1 and mutex2 were the same mutex,
After this change, _cs_mutex == NULL also occurs when m1 != m2 but both were already held by the current CS2. The "should only happen when mutex1 and mutex2 were the same mutex" claim
no longer holds.
2. Pointer comparison in the asserts. assert(m1 < m2) and assert(prev2->_cs_base._cs_mutex < prev2->_cs_mutex2) compare PyMutex * directly. Relational comparison of unrelated pointers
is technically UB, and the header deliberately casts to (uintptr_t) when sorting (line 163). For consistency, cast here too:
assert((uintptr_t)m1 < (uintptr_t)m2);
4. Minor: ordering differs from the single-mutex version. Here the world-stopped check comes before the recursive-skip; in _PyCriticalSection_BeginSlow it's the reverse. Both produce
identical results (NULL mutexes, no push), so this is harmless — just a slight inconsistency.
Contributor
Author
|
I'll address them, thanks Claude |
Contributor
Author
|
... and it still cannot count 😁 |
Contributor
I deleted 3. because I didn't think it was relevant |
|
Thanks @dpdani for the PR, and @colesbury for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15. |
Contributor
Author
|
Ah ok, sorry Claude, was too quick to judge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This mimics an optimization already present for the single-mutex critical section.
PyCriticalSection2to skip locking when the requested locks were acquired in the current CS #150902