Skip to content

Commit 18ef0f0

Browse files
[3.14] gh-152075: Avoid lock contention in _Py_Specialize_LoadGlobal under free threading (gh-153720) (#158904)
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 ac8ba0c) Co-authored-by: Peter Hawkins <phawkins@google.com>
1 parent e0c28e2 commit 18ef0f0

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reduced lock contention during LOAD_GLOBAL bytecode specialization under
2+
free threading.

‎Python/specialize.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,14 @@ _Py_Specialize_LoadGlobal(
17761776
PyObject *globals, PyObject *builtins,
17771777
_Py_CODEUNIT *instr, PyObject *name)
17781778
{
1779+
#ifdef Py_GIL_DISABLED
1780+
if (PyMutex_IsLocked(&globals->ob_mutex) || PyMutex_IsLocked(&builtins->ob_mutex)) {
1781+
// Skip specialization if either dictionary is locked to avoid lock
1782+
// contention.
1783+
unspecialize(instr);
1784+
return;
1785+
}
1786+
#endif
17791787
Py_BEGIN_CRITICAL_SECTION2(globals, builtins);
17801788
specialize_load_global_lock_held(globals, builtins, instr, name);
17811789
Py_END_CRITICAL_SECTION2();

0 commit comments

Comments
 (0)