Repository navigation
Make the pure-Python version bump atomic on GIL builds - #1610
Conversation
The GIL-build _incr_version() read, incremented and stored the shared counter across several bytecodes, so two threads could both hand out the same version. Draw versions from itertools.count instead, whose next() is a single C call. Free-threaded builds keep the lock.
|
This comment has been minimized.
This comment has been minimized.
Merging this PR will improve performance by 10.3%
Performance ChangesTip Curious why performance improved? Comment Comparing |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1610 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 43 43
Lines 7940 7987 +47
Branches 663 669 +6
=========================================
+ Hits 7940 7987 +47
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
CPython GIL builds only switch threads at calls and backward jumps, so they never interleave inside the old increment; only PyPy does.
What do these changes do?
On GIL builds the pure-Python
_incr_version()read, incremented and stored the shared version counter across several bytecodes, so two threads mutating different multidicts at once could be handed the same version. The counter is now anitertools.count, and the GIL build takesnext()from it, which is a single C call. The free-threaded build keeps_VERSION_LOCKaround the samenext(), sinceitertools.countis not documented as thread-safe there.test_pure_python_version_thread_safetyno longer carries_gil_build_race_skip. It lowers the switch interval to 1e-4 s while its threads run, which fails reliably on PyPy before the fix. CPython 3.10 and 3.14 GIL builds never showed the race, since they only switch threads at calls and backward jumps and the old increment had neither in the window.The C extension is unaffected; its counter was made atomic in #1446.
Are there changes in behavior for the user?
No, apart from pure-Python versions no longer colliding across threads.
Is it a substantial burden for the maintainers to support this?
No; the change is three lines and makes the increment simpler.
Related issue number
Related to #1605, whose pure-Python GIL-build skip in
test_versions_unique_across_threadscan be removed once both land.Checklist
CONTRIBUTORS.txt(N/A)CHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtDrafted with Claude Code (Claude Opus 5.5); reviewed by @asvetlov.
Agent run details (optional, for reviewers)
Reproduction, PyPy 3.10.16,
test_pure_python_version_thread_safetyunder pytest-cov: 4/4 failed before the fix, 4/4 passed after, about 4 s per run. Standalone, 16 threads x 2000__setitem__at a 1e-4 switch interval gave 59 to 231 duplicate versions per run before, 0 after.Full suite, each leg in its own venv and tree copy:
MULTIDICT_NO_EXTENSIONS=1 ... --no-c-extensions: 1321 passed, 16 skippedtest_circular_importsneeded multidict installed into the venv, then passed)Lint:
pre-commit(ruff, mypy) andmake doc-spellingpass.Instructions per operation,
benchmarks/callgrind_driver.py --impl multidict_py --include-multidict-only, CPython 3.14.7 GIL build (no wall-clock run):addadd_istrpopitemctor_itemspopsetdefault_newdelitemsetitem_replacesetitem_insertRead-only operations moved by less than 0.3%.