Conversation
n1snt
force-pushed
the
gh-158525-sparse-dict-copy
branch
from
September 30, 2026 22:26
6f571fa to
dc3a363
Compare
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.
When copying a sparse mutable dict,
PyDict_Copy()already holds the source lock. The compaction fallback calleddict_merge(), which tried to acquire the source again as part of a two-object critical section. In a free-threaded build, this caused the thread to spin and park on its own lock.Call
dict_dict_merge()directly when the source uses the built-in iterator. The destination is newly created and not visible to other threads. Keep the generic merge path for frozendicts and subclasses with custom iteration.The existing
test_copy_noncompacttest covers the changed copy path.Benchmark
Linux arm64, free-threaded current main release build:
compact.copy()sparse.copy()dict(sparse)Tests
./python -m test -v test_dict test_free_threading.test_dict./python -m test -v test_dictAI use: I used an AI assistant to help inspect the locking path and prepare the change. I reviewed the diff and ran the tests and benchmarks above.
Fixes #158525