From 6ff1422149ba6dc3e48fbaabc72a921410cb7a7a Mon Sep 17 00:00:00 2001 From: Daniele Parmeggiani Date: Tue, 22 Sep 2026 17:11:46 +0100 Subject: [PATCH 1/4] gh-157914: Revert "gh-135871: Reload lock internal state while spinning in `PyMutex_LockTimed` (gh-146064)" This reverts commit daa159f98b3689ae6a587bfb978b931762f9dbc9. --- Misc/NEWS.d/3.15.0a8.rst | 11 ----------- Python/lock.c | 15 --------------- 2 files changed, 26 deletions(-) diff --git a/Misc/NEWS.d/3.15.0a8.rst b/Misc/NEWS.d/3.15.0a8.rst index 28e9ca85166620..d3ddee949ad6b7 100644 --- a/Misc/NEWS.d/3.15.0a8.rst +++ b/Misc/NEWS.d/3.15.0a8.rst @@ -305,17 +305,6 @@ Base32 converters in :mod:`base64`. Patch by James Seo. .. -.. date: 2026-03-19-16-16-40 -.. gh-issue: 135871 -.. nonce: jSExZ3 -.. section: Core and Builtins - -Improve multithreaded scaling of PyMutex in low-contention scenarios by -reloading the lock's internal state, without slowing down high-contention -scenarios. - -.. - .. date: 2026-03-19-01-19-34 .. gh-issue: 146096 .. nonce: R9tkJX diff --git a/Python/lock.c b/Python/lock.c index b636b91e79678c..b669e3dcdbc999 100644 --- a/Python/lock.c +++ b/Python/lock.c @@ -27,10 +27,8 @@ static const PyTime_t TIME_TO_BE_FAIR_NS = 1000*1000; // enabled. #if Py_GIL_DISABLED static const int MAX_SPIN_COUNT = 40; -static const int RELOAD_SPIN_MASK = 3; #else static const int MAX_SPIN_COUNT = 0; -static const int RELOAD_SPIN_MASK = 1; #endif struct mutex_entry { @@ -81,16 +79,6 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) }; Py_ssize_t spin_count = 0; -#ifdef Py_GIL_DISABLED - // Using thread-id as a way of reducing contention further in the reload below. - // It adds a pseudo-random starting offset to the recurrence, so that threads - // are less likely to try and run compare-exchange at the same time. - // The lower bits of platform thread ids are likely to not be random, - // hence the right shift. - const Py_ssize_t tid = (Py_ssize_t)(_Py_ThreadId() >> 12); -#else - const Py_ssize_t tid = 0; -#endif for (;;) { if ((v & _Py_LOCKED) == 0) { // The lock is unlocked. Try to grab it. @@ -104,9 +92,6 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) // Spin for a bit. _Py_yield(); spin_count++; - if (((spin_count + tid) & RELOAD_SPIN_MASK) == 0) { - v = _Py_atomic_load_uint8_relaxed(&m->_bits); - } continue; } From 5d3d91952afdd0e0e82dab9bfb8a727a65f46619 Mon Sep 17 00:00:00 2001 From: Daniele Parmeggiani Date: Thu, 24 Sep 2026 17:31:18 +0100 Subject: [PATCH 2/4] news --- Misc/NEWS.d/3.15.0a8.rst | 11 +++++++++++ .../2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst | 3 +++ 2 files changed, 14 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst diff --git a/Misc/NEWS.d/3.15.0a8.rst b/Misc/NEWS.d/3.15.0a8.rst index d3ddee949ad6b7..28e9ca85166620 100644 --- a/Misc/NEWS.d/3.15.0a8.rst +++ b/Misc/NEWS.d/3.15.0a8.rst @@ -305,6 +305,17 @@ Base32 converters in :mod:`base64`. Patch by James Seo. .. +.. date: 2026-03-19-16-16-40 +.. gh-issue: 135871 +.. nonce: jSExZ3 +.. section: Core and Builtins + +Improve multithreaded scaling of PyMutex in low-contention scenarios by +reloading the lock's internal state, without slowing down high-contention +scenarios. + +.. + .. date: 2026-03-19-01-19-34 .. gh-issue: 146096 .. nonce: R9tkJX diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst new file mode 100644 index 00000000000000..75ff789e940f16 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst @@ -0,0 +1,3 @@ +Revert commit ``daa159f98b3689ae6a587bfb978b931762f9dbc9`` from PR +https://github.com/python/cpython/pull/146064. The linked PyMutex change was +causing a significant performance regression in some cases. From 53d7a08d63b2d04376e5ec431203b73b321fe95d Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Mon, 28 Sep 2026 01:45:51 +0900 Subject: [PATCH 3/4] Apply suggestion from @hugovk Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- .../2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst index 75ff789e940f16..0ed01d118c9230 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst @@ -1,3 +1,2 @@ -Revert commit ``daa159f98b3689ae6a587bfb978b931762f9dbc9`` from PR -https://github.com/python/cpython/pull/146064. The linked PyMutex change was +Revert :gh`146064`. The linked PyMutex change was causing a significant performance regression in some cases. From 8241f7e455d4a52dbe1231b57309363b1f69e135 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Mon, 28 Sep 2026 01:55:51 +0900 Subject: [PATCH 4/4] Fix sphinx-lint error in NEWS entry --- .../2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst index 0ed01d118c9230..b381a568cf14ac 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst @@ -1,2 +1,2 @@ -Revert :gh`146064`. The linked PyMutex change was +Revert :gh:`146064`. The linked PyMutex change was causing a significant performance regression in some cases.