Title: gh-135871: Reload lock internal state while spinning in `PyMutex_LockTimed` by dpdani · Pull Request #146064 · python/cpython · GitHub
Open Graph Title: gh-135871: Reload lock internal state while spinning in `PyMutex_LockTimed` by dpdani · Pull Request #146064 · python/cpython
X Title: gh-135871: Reload lock internal state while spinning in `PyMutex_LockTimed` by dpdani · Pull Request #146064 · python/cpython
Description: 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. And here's the comparison between main and this PR, which picks the RELOAD_SPIN_COUNT = 3 strategy: 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: gh-135871
Open Graph Description: 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 ...
X Description: 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 ...
Opengraph URL: https://github.com/python/cpython/pull/146064
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:e089a8c0-8130-67c3-228e-9db1255746d3 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 8A5C:3061A4:DDD610:12DB1FB:6A516D48 |
| html-safe-nonce | 7acba171bc83fed52f69dda6ca1b2e60a771d6a87a03eebf6f29d73907cd9f7e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QTVDOjMwNjFBNDpEREQ2MTA6MTJEQjFGQjo2QTUxNkQ0OCIsInZpc2l0b3JfaWQiOiIyNzgzMDQ5OTk4MjU3OTc0NjAwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 5ca0731a0104d0a230af5aa0b06087a7f75168d0ddabf0bd6626f475d823a1c8 |
| hovercard-subject-tag | pull_request:3410155281 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/146064/files |
| twitter:image | https://avatars.githubusercontent.com/u/8658291?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/8658291?s=400&v=4 |
| og:image:alt | 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 ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 60eaaf6d4dbaa817031b556aebd8a0a7eeb81b97 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width