GeezerVison is showing github.com — gh-161850: Lock only the deque when advancing a deque iterator by 113xiaoji · Pull Request #161853 · python/cpython
Conversation
113xiaoji
commented
Nov 12, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Summary
Refs #161850.
Serialize forward and reverse deque iteration, and iterator reduction, using the deque's mutex alone. The iterator retains its deque throughout its lifetime, and its mutable iteration state is protected by that mutex. This removes the iterator mutex acquisition/release from the per-item path.
Changes
Test Plan
- Source review and
git diff --check pass for this main-based branch.
- The main-based CI run passed all 48 jobs on head
d0ed2da495922d2887672d3fd0c2cdeb22df4278, including ordinary/free-threaded builds and TSan jobs.
- Performance measurements below were run on a CPython 3.14.6 backport, not this main-based (3.16 development) port. This PR stays draft for core review.
On the tested 950, the 3.14.6 backport's 1000-element deque iteration benchmark decreased from about 52.3 to 30.6 microseconds. This is supporting evidence for the approach, not a performance claim for this branch.
AI tools assisted preparation of the change and submission text.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
In the free-threaded build, dequeiter_next(), dequereviter_next() and
dequeiter_reduce() entered a two-object critical section on the iterator
and its deque. All writes to the iterator's state already happen while
holding the deque's lock, so the deque's critical section alone is enough
to serialize concurrent next() calls on a shared iterator. This halves
the atomic read-modify-write operations per item.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
All commit authors signed the Contributor License Agreement.

|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
rhettinger
Awaiting requested review from rhettinger
rhettinger will be requested when the pull request is marked ready for review
rhettinger is a code owner
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.