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-135871: Reload lock internal state while spinning in `PyMutex_LockTimed` by dpdani · Pull Request #146064 · python/cpython

Conversation

@dpdani

@dpdani dpdani commented Mar 17, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

This PR adds atomic loads in the slow path of PyMutex to increase the number of lock acquisitions per second that threads can make on a shared mutex.

The tricky part here is to avoid degrading the performance when the lock is highly contended; that is, when many threads are trying to acquire the mutex at a high frequency. This is because the current strategy of never reloading the mutex's state is, maybe counter-intuitively, the best strategy: avoid disturbing the thread that currently holds the mutex.

I've run the lockbench script to assess the performance using the following two scenarios, which were suggested by @colesbury:

  • low contention:
./python.exe Tools/lockbench/lockbench.py --work-inside 5 --work-outside 50 --num-locks 24 --acquisitions 3 --random-locks
  • high contention:
./python.exe Tools/lockbench/lockbench.py --work-inside 5 --work-outside 5

The results are from my M4 MacBook, and Python was compiled with --disable-gil --enable-optimizations --with-lto.

The different lines below represent different reloading strategies:

  • old: main
  • unconditional: remove the if statement at line 107
  • RELOAD_SPIN_COUNT = ...: tweak the value at line 30

The RELOAD_SPIN_COUNT = ... strategies also pseudo-randomize the reload recurrence by having each thread add its own thread-id to the counter. On my machine this nets a ~15/20% improvement over the same strategy, without this pseudo-randomization.

lockbench_compare

And here's the comparison between main and this PR, which picks the RELOAD_SPIN_COUNT = 3 strategy:

lockbench_old_vs_spin_3

The fact that the high contention case shows as an improvement in this chart is misleading: I'd say it's in the noise range.

  • Issue: Non-blocking _PyMutex_LockTimed spins and may fail unnecessarily in no-GIL builds #135871

Sorry, something went wrong.

Uh oh!

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

All reactions
@dpdani
pythongh-135871: Reload lock internal state while spinning in `_PyMut…
7217ad0
…ex_LockTimed`
@bedevere-app bedevere-app Bot mentioned this pull request Mar 17, 2026
Non-blocking _PyMutex_LockTimed spins and may fail unnecessarily in no-GIL builds #135871
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Mar 17, 2026
@blurb-it
📜🤖 Added by blurb_it.
54932d6
@diegorusso diegorusso added the topic-free-threading label Mar 23, 2026
@diegorusso
diegorusso requested a review from colesbury March 23, 2026 13:54
colesbury
colesbury approved these changes Mar 23, 2026
View reviewed changes

@colesbury colesbury left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor comments, but otherwise LGTM.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Python/lock.c Resolved

Uh oh!

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

Comment thread Python/lock.c Resolved

Uh oh!

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

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Mar 23, 2026
dpdani and others added 3 commits March 23, 2026 16:06
@dpdani @colesbury
parentheses for casting
f1eaffa
Co-authored-by: Sam Gross <[email protected]>
@dpdani
RELOAD_SPIN_COUNT -> RELOAD_SPIN_MASK
5fee9e4
Change-Id: I5df04888df81de26a04450a9a1a04d4db5026383
@colesbury
Merge branch 'main' into pymutex-spinning-reload
e7b874e
@colesbury
colesbury merged commit daa159f into python:main Mar 23, 2026
93 of 94 checks passed

Uh oh!

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

@bedevere-app bedevere-app Bot removed the awaiting merge label Mar 23, 2026
CuriousLearner added a commit to CuriousLearner/cpython that referenced this pull request Mar 24, 2026
@CuriousLearner
Merge branch 'fix-pythongh-138577' of github.com:CuriousLearner/cpyth…
78b2c6a
…on into fix-pythongh-138577

* 'fix-pythongh-138577' of github.com:CuriousLearner/cpython:
  pythongh-146202: Create tmp_dir in regrtest worker (python#146347)
  pythongh-144319: obtain SeLockMemoryPrivilege on Windows (python#144928)
  pythongh-146199: Fix error handling in `code_richcompare` when `PyObject_RichCompareBool` fails (python#146200)
  pythongh-146197: Include a bit more information in sys._emscripten_info.runtime (python#146346)
  pythongh-135871: Reload lock internal state while spinning in `PyMutex_LockTimed` (pythongh-146064)
  pythongh-145719: Add `.efi` file detection in `mimetypes` (python#145720)
@dpdani
dpdani deleted the pymutex-spinning-reload branch March 24, 2026 09:46
@pablogsal pablogsal mentioned this pull request Apr 4, 2026
gh-130273: Fix traceback color output with unicode characters #142529
Merged
clin1234 pushed a commit to clin1234/cpython that referenced this pull request Apr 16, 2026
@dpdani @clin1234
pythongh-135871: Reload lock internal state while spinning in `PyMute…
28b767f
…x_LockTimed` (pythongh-146064)

Add atomic loads in the slow path of PyMutex to increase the number
of lock acquisitions per second that threads can make on a shared mutex.
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@dpdani @ljfp
pythongh-135871: Reload lock internal state while spinning in `PyMute…
29cb7de
…x_LockTimed` (pythongh-146064)

Add atomic loads in the slow path of PyMutex to increase the number
of lock acquisitions per second that threads can make on a shared mutex.
@dpdani dpdani mentioned this pull request Sep 21, 2026
Performance regression from PyMutex spin-loop reloading in free-threaded builds #157914
Closed
corona10 pushed a commit that referenced this pull request Sep 27, 2026
@dpdani
gh-157914: Revert "gh-135871: Reload lock internal state while spinni…
baec764
…ng in `PyMutex_LockTimed` (gh-146064)" (gh-158082)
hugovk pushed a commit that referenced this pull request Sep 30, 2026
@miss-islington @dpdani
[3.15] gh-157914: Revert "gh-135871: Reload lock internal state while…
55ab336
… spinning in `PyMutex_LockTimed` (gh-146064)" (gh-158082) (#158290)

Co-authored-by: Daniele Parmeggiani <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@colesbury colesbury colesbury approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

topic-free-threading

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.

3 participants

@dpdani @colesbury @diegorusso
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.