From 97e6499c7a5abb7ba6eef83ed6e1744b65032d24 Mon Sep 17 00:00:00 2001 From: Daniele Parmeggiani Date: Sun, 27 Sep 2026 18:29:07 +0100 Subject: [PATCH] gh-157914: Revert "gh-135871: Reload lock internal state while spinning in `PyMutex_LockTimed` (gh-146064)" (gh-158082) (cherry picked from commit baec76438e32f50983f48c1658a55015b036204b) Co-authored-by: Daniele Parmeggiani --- ...2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst | 2 ++ Python/lock.c | 15 --------------- 2 files changed, 2 insertions(+), 15 deletions(-) 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/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 000000000000000..b381a568cf14acf --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-24-17-30-48.gh-issue-157914.5fVJob.rst @@ -0,0 +1,2 @@ +Revert :gh:`146064`. The linked PyMutex change was +causing a significant performance regression in some cases. diff --git a/Python/lock.c b/Python/lock.c index af136fefd299d37..84f18a0c15a769c 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; }