]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-113743: Use per-interpreter locks for types (#115541)
authorDino Viehland <dinoviehland@meta.com>
Fri, 16 Feb 2024 00:28:31 +0000 (16:28 -0800)
committerGitHub <noreply@github.com>
Fri, 16 Feb 2024 00:28:31 +0000 (16:28 -0800)
Move type-lock to per-interpreter lock to avoid heavy contention in interpreters test

Include/internal/pycore_typeobject.h
Objects/typeobject.c
Python/pystate.c

index 664f6fb212a57d2148e9d5d3bd3b2e14e6947a91..9134ab45cd003951f7a3627422768ec5f4ddb6a4 100644 (file)
@@ -22,7 +22,6 @@ struct _types_runtime_state {
     // bpo-42745: next_version_tag remains shared by all interpreters
     // because of static types.
     unsigned int next_version_tag;
-    PyMutex type_mutex;
 };
 
 
@@ -71,6 +70,7 @@ struct types_state {
     struct type_cache type_cache;
     size_t num_builtins_initialized;
     static_builtin_state builtins[_Py_MAX_STATIC_BUILTIN_TYPES];
+    PyMutex mutex;
 };
 
 
index e0711dfe8545b7e28fb583b2b7ec33ef88347d73..0118ee255ef0177c25ee9519a0f4bb27255c8111 100644 (file)
@@ -60,17 +60,18 @@ class object "PyObject *" "&PyBaseObject_Type"
 // in odd behaviors w.r.t. running with the GIL as the outer type lock could
 // be released and reacquired during a subclass update if there's contention
 // on the subclass lock.
+#define TYPE_LOCK &PyInterpreterState_Get()->types.mutex
 #define BEGIN_TYPE_LOCK()                                               \
     {                                                                   \
         _PyCriticalSection _cs;                                         \
-        _PyCriticalSection_Begin(&_cs, &_PyRuntime.types.type_mutex);   \
+        _PyCriticalSection_Begin(&_cs, TYPE_LOCK);                      \
 
 #define END_TYPE_LOCK()                                                 \
         _PyCriticalSection_End(&_cs);                                   \
     }
 
 #define ASSERT_TYPE_LOCK_HELD() \
-    _Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(&_PyRuntime.types.type_mutex)
+    _Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(TYPE_LOCK)
 
 #else
 
index b1d1a085d629b4c26140fbd6eae695aa2861fdec..24f9b7790915ab5d4f61957b75701af573b8fda9 100644 (file)
@@ -395,7 +395,7 @@ _Py_COMP_DIAG_POP
         &(runtime)->atexit.mutex, \
         &(runtime)->audit_hooks.mutex, \
         &(runtime)->allocators.mutex, \
-        &(runtime)->types.type_mutex, \
+        &(runtime)->_main_interpreter.types.mutex, \
     }
 
 static void