GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

@113xiaoji 113xiaoji commented Nov 12, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

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

  • Use the deque's critical section in dequeiter_next(), dequereviter_next() and dequeiter_reduce().
  • Use relaxed atomic stores for the remaining-item count, paired with the unlocked __length_hint__ read.
  • Add shared forward/reverse iterator and concurrent mutation tests in test_free_threading.test_collections.
  • Add the NEWS entry for Free-threaded build: deque iteration and list(deque) take a critical section per item #161850.

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.

All reactions
113xiaoji and others added 2 commits November 12, 2026 14:52
@113xiaoji @claude
Lock only the deque when advancing a deque iterator
314f28d
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]>
@113xiaoji @claude
pythongh-161850: Add NEWS entry for deque iterator locking
d0ed2da
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@python-cla-bot

python-cla-bot Bot commented Nov 12, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 12, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Nov 12, 2026
Free-threaded build: deque iteration and list(deque) take a critical section per item #161850
Open
@113xiaoji 113xiaoji mentioned this pull request Nov 12, 2026
gh-161850: Copy a deque into a list under a single critical section #161854
Draft
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@rhettinger 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

Labels

type-feature A feature request or enhancement

Projects

None yet

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.

1 participant

@113xiaoji
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.