https://github.com/python/cpython/commit/ba1e1c192b1ec661d67db66d10c6f059440d352d
commit: ba1e1c192b1ec661d67db66d10c6f059440d352d
branch: main
author: Kumar Aditya <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-04-11T15:43:53Z
summary:

gh-131798: do not watch immutable types in JIT (#148383)

files:
M Python/optimizer_analysis.c
M Python/optimizer_bytecodes.c
M Python/optimizer_cases.c.h

diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c
index 069f4aeffa64fc..ca9bcc8a40c35e 100644
--- a/Python/optimizer_analysis.c
+++ b/Python/optimizer_analysis.c
@@ -397,8 +397,10 @@ lookup_attr(JitOptContext *ctx, _PyBloomFilter 
*dependencies, _PyUOpInstruction
             if (suffix != _NOP) {
                 ADD_OP(suffix, 2, 0);
             }
-            PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
-            _Py_BloomFilter_Add(dependencies, type);
+            if ((type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
+                PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
+                _Py_BloomFilter_Add(dependencies, type);
+            }
             return sym_new_const(ctx, lookup);
         }
     }
@@ -466,10 +468,13 @@ lookup_super_attr(JitOptContext *ctx, _PyBloomFilter 
*dependencies,
     if (suffix != _NOP) {
         ADD_OP(suffix, 2, 0);
     }
-    PyType_Watch(TYPE_WATCHER_ID, (PyObject *)su_type);
-    _Py_BloomFilter_Add(dependencies, su_type);
-    PyType_Watch(TYPE_WATCHER_ID, (PyObject *)obj_type);
-    _Py_BloomFilter_Add(dependencies, obj_type);
+    // if obj_type is immutable, then all its superclasses are immutable
+    if ((obj_type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
+        PyType_Watch(TYPE_WATCHER_ID, (PyObject *)su_type);
+        _Py_BloomFilter_Add(dependencies, su_type);
+        PyType_Watch(TYPE_WATCHER_ID, (PyObject *)obj_type);
+        _Py_BloomFilter_Add(dependencies, obj_type);
+    }
     return sym_new_const_steal(ctx, lookup);
 }
 
diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c
index c7fe34bf785c0b..c12a4f4131bc7e 100644
--- a/Python/optimizer_bytecodes.c
+++ b/Python/optimizer_bytecodes.c
@@ -142,8 +142,10 @@ dummy_func(void) {
             PyTypeObject *probable_type = sym_get_probable_type(owner);
             if (probable_type->tp_version_tag == type_version && 
sym_set_type_version(owner, type_version)) {
                 // Promote the probable type version to a known one.
-                PyType_Watch(TYPE_WATCHER_ID, (PyObject *)probable_type);
-                _Py_BloomFilter_Add(dependencies, probable_type);
+                if ((probable_type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) 
{
+                    PyType_Watch(TYPE_WATCHER_ID, (PyObject *)probable_type);
+                    _Py_BloomFilter_Add(dependencies, probable_type);
+                }
             }
         }
     }
diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h
index 98287e7f841761..2db2c87cb3610b 100644
--- a/Python/optimizer_cases.c.h
+++ b/Python/optimizer_cases.c.h
@@ -2451,8 +2451,10 @@
             } else {
                 PyTypeObject *probable_type = sym_get_probable_type(owner);
                 if (probable_type->tp_version_tag == type_version && 
sym_set_type_version(owner, type_version)) {
-                    PyType_Watch(TYPE_WATCHER_ID, (PyObject *)probable_type);
-                    _Py_BloomFilter_Add(dependencies, probable_type);
+                    if ((probable_type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) 
== 0) {
+                        PyType_Watch(TYPE_WATCHER_ID, (PyObject 
*)probable_type);
+                        _Py_BloomFilter_Add(dependencies, probable_type);
+                    }
                 }
             }
             break;

_______________________________________________
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]

Reply via email to