https://github.com/python/cpython/commit/fc1f6b176e2053c792cc40b39be44c6918bb0460
commit: fc1f6b176e2053c792cc40b39be44c6918bb0460
branch: main
author: Kumar Aditya <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-03-26T10:12:10Z
summary:

gh-GH-131798: optimize jit attribute loads on immutable types (#146449)

files:
M Lib/test/test_capi/test_opt.py
M Python/optimizer_analysis.c

diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py
index 3217db192803d1..a451fb2850d70d 100644
--- a/Lib/test/test_capi/test_opt.py
+++ b/Lib/test/test_capi/test_opt.py
@@ -2808,9 +2808,6 @@ def testfunc(n):
         self.assertEqual(res, sum(range(TIER2_THRESHOLD)))
         uops = get_opnames(ex)
         self.assertIn("_CALL_LIST_APPEND", uops)
-        # We should remove these in the future
-        self.assertIn("_GUARD_NOS_LIST", uops)
-        self.assertIn("_GUARD_CALLABLE_LIST_APPEND", uops)
 
     def test_call_list_append_pop_top(self):
         def testfunc(n):
@@ -3046,6 +3043,8 @@ def f(n):
         self.assertEqual(res, TIER2_THRESHOLD)
         uops = get_opnames(ex)
         self.assertNotIn("_LOAD_ATTR_METHOD_NO_DICT", uops)
+        self.assertNotIn("_LOAD_CONST_UNDER_INLINE", uops)
+        self.assertIn("_LOAD_CONST_UNDER_INLINE_BORROW", uops)
 
     def test_store_fast_refcount_elimination(self):
         def foo(x):
diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c
index 520420e9878575..0c25c0c9bc0d1c 100644
--- a/Python/optimizer_analysis.c
+++ b/Python/optimizer_analysis.c
@@ -367,7 +367,11 @@ lookup_attr(JitOptContext *ctx, _PyBloomFilter 
*dependencies, _PyUOpInstruction
     if (type && PyType_Check(type)) {
         PyObject *lookup = _PyType_Lookup(type, name);
         if (lookup) {
-            int opcode = _Py_IsImmortal(lookup) ? immortal : mortal;
+            int opcode = mortal;
+            // if the object is immortal or the type is immutable, borrowing 
is safe
+            if (_Py_IsImmortal(lookup) || (type->tp_flags & 
Py_TPFLAGS_IMMUTABLETYPE)) {
+                opcode = immortal;
+            }
             ADD_OP(opcode, 0, (uintptr_t)lookup);
             PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
             _Py_BloomFilter_Add(dependencies, type);

_______________________________________________
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