Repository navigation
Conversation
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 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
list(d), [*d], list.extend(d) and PySequence_Fast(d) -- and hence str.join(d), which hands the deque's iterator to list.extend() -- fetched one item per tp_iternext call, each entering a critical section. Add a fast path that copies all remaining items of a deque or a forward deque iterator while holding the list's and the deque's critical sections once. The _collections module registers it with listobject.c, since the deque type lives in that module. The result is identical to running the iterator to exhaustion: no user code runs, the mutation check matches next(), and the list's allocation matches the generic path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Keep the mutable deque hook pointer in PyInterpreterState instead of a C global so check-c-globals accepts it. Preserve release/acquire publication for concurrent free-threaded registration. Validated with ordinary and free-threaded pydebug builds, the focused C globals check, and deque/list/collections/embed/interpreter tests. pythongh-161850
Copying a whole deque under one critical section holds the deque's lock for O(n). When several threads keep copying a shared deque, a writer then rarely gets the lock: on a Kunpeng 950, 7 threads calling list(d) on a 1000-item deque cut a rotate() writer's throughput by ~99% and pushed its p99 latency to ~1 ms (the PyMutex fairness timeout). Copy at most BLOCKLEN items per critical section instead. Between chunks the deque's state is checked again and a mutation raises RuntimeError, exactly as next() does, so the observable behavior under concurrent mutation is the same as iterating item by item. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Describe the 64-item bound as an element-transfer bound and retain the initial capacity reservation and concurrent-mutation limitations. Update the NEWS entry to describe chunked copying instead of one lock. Refs pythongh-161850.
113xiaoji
marked this pull request as ready for review
November 15, 2026 12:35
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.
Summary
Depends on #161853. This branch includes that prerequisite, so the diff against
maincontains both changes. The follow-up adds fast paths for materializing a deque or the remaining items of a forward deque iterator into a list.Transfer at most
BLOCKLEN(64) elements per critical section on the list and deque, releasing the locks between chunks. This amortizes per-item locking without retaining the original one-shot element-copy critical section for the entire deque.Refs #161850.
Changes
list(d),[*d],list.extend(d)and the iterator path used bystr.join(d).RuntimeErroron iterator-invalidating mutation, and advance shared iterator position/count under the deque lock._collections, with release/acquire publication for free-threaded access.Concurrency and tradeoffs
The 64-item bound applies to element transfer, not total lock duration: initial list capacity reservation still covers all remaining items while holding the locks. This does not guarantee bounded waiting or eliminate writer starvation for every workload.
State-preserving deque mutations are not rejected by the structural-state check. The operation does not promise an atomic snapshot or reproduce every interleaving of repeated
next()calls. The supported built-in-slot paths avoid user-defined iteration/length callbacks; the element-copy loop does not call Python iteration methods.On a CPython 3.14.6 backport on Kunpeng 950, the chunked revision substantially improves writer progress over the one-shot revision, while retaining private-buffer throughput gains. A matched three-block, 5-second, batch-1 stress test with a 65,536-element deque and seven readers plus one writer measured writer throughput of about 458,691 ops/s before bulk copying, 698 ops/s with one-shot copying, and 46,342 ops/s with chunking. Large shared deques therefore retain a significant contention tradeoff. These are backport measurements, not performance claims for this main-based port.
Validation
git diff --checkpass. The latest follow-up changes only the implementation comment and NEWS; C content outside block comments is unchanged from R3 implementation revisionda66db3b89ef3251647c80328785235c7722ea12.da66db3b89ef3251647c80328785235c7722ea12, lint and the generated-files job passed, including C globals, exported symbols, limited ABI and regeneration checks. See the current-head Checks tab for the latest complete CI status; results from the earlier pre-chunking revision are not presented as validation of R3.Review is requested for the fast-path hook design, iterator/list concurrency behavior and chunking tradeoffs. The prerequisite remains open; this PR is not ready to merge independently.
AI tools assisted preparation, review and benchmark analysis.
list(deque)take a critical section per item #161850