Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-135871: Reload lock internal state while spinning inPyMutex_LockTimed#146064

Merged
colesbury merged 5 commits intopython:mainfrom
dpdani:pymutex-spinning-reload
Mar 23, 2026
Merged

gh-135871: Reload lock internal state while spinning inPyMutex_LockTimed#146064
colesbury merged 5 commits intopython:mainfrom
dpdani:pymutex-spinning-reload

Conversation

@dpdani
Copy link
Contributor

@dpdanidpdani commentedMar 17, 2026
edited
Loading

This PR adds atomic loads in the slow path ofPyMutex 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 thelockbench 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 theif statement at line 107
  • RELOAD_SPIN_COUNT = ...: tweak the value at line 30

TheRELOAD_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 betweenmain and this PR, which picks theRELOAD_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.

Copy link
Contributor

@colesburycolesbury left a 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.

dpdaniand others added3 commitsMarch 23, 2026 16:06
Co-authored-by: Sam Gross <colesbury@gmail.com>
Change-Id: I5df04888df81de26a04450a9a1a04d4db5026383
@colesburycolesbury merged commitdaa159f intopython:mainMar 23, 2026
93 of 94 checks passed
CuriousLearner added a commit to CuriousLearner/cpython that referenced this pull requestMar 24, 2026
…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)
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@colesburycolesburycolesbury approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@dpdani@colesbury@diegorusso

[8]ページ先頭

©2009-2026 Movatter.jp