https://github.com/python/cpython/commit/35c77f2c0030744419f1415f849b705504243e56 commit: 35c77f2c0030744419f1415f849b705504243e56 branch: 3.15 author: Miss Islington (bot) <[email protected]> committer: kumaraditya303 <[email protected]> date: 2026-10-10T16:03:02+05:30 summary:
[3.15] gh-152075: Avoid lock contention in _Py_Specialize_LoadGlobal under free threading (gh-153720) (#158905) gh-152075: Avoid lock contention in _Py_Specialize_LoadGlobal under free threading (gh-153720) Under high thread concurrency in free-threaded builds, `_Py_Specialize_LoadGlobal` suffers from lock contention when acquiring the critical section mutexes for the `globals` and `builtins` dictionaries during bytecode specialization. This PR skips LOAD_GLOBAL bytecode specialization if acquiring the two object mutexes would block. (cherry picked from commit ac8ba0ca5a04cd7b27b44822d31df640e817fd78) Co-authored-by: Peter Hawkins <[email protected]> files: A Misc/NEWS.d/next/Core_and_Builtins/2026-07-14-20-19-48.gh-issue-152075.V9Rwhp.rst M Python/specialize.c diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-14-20-19-48.gh-issue-152075.V9Rwhp.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-14-20-19-48.gh-issue-152075.V9Rwhp.rst new file mode 100644 index 00000000000000..84c7d36b97acba --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-14-20-19-48.gh-issue-152075.V9Rwhp.rst @@ -0,0 +1,2 @@ +Reduced lock contention during LOAD_GLOBAL bytecode specialization under +free threading. diff --git a/Python/specialize.c b/Python/specialize.c index 12a8c624f24d71..8213682fa807c6 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -1455,6 +1455,14 @@ _Py_Specialize_LoadGlobal( PyObject *globals, PyObject *builtins, _Py_CODEUNIT *instr, PyObject *name) { +#ifdef Py_GIL_DISABLED + if (PyMutex_IsLocked(&globals->ob_mutex) || PyMutex_IsLocked(&builtins->ob_mutex)) { + // Skip specialization if either dictionary is locked to avoid lock + // contention. + unspecialize(instr); + return; + } +#endif Py_BEGIN_CRITICAL_SECTION2(globals, builtins); specialize_load_global_lock_held(globals, builtins, instr, name); Py_END_CRITICAL_SECTION2(); _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
