]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-131798: Optimize `_ITER_CHECK_RANGE` and `_ITER_CHECK_LIST` in the JIT (GH-144583)
authorSacul <183588943+Sacul0457@users.noreply.github.com>
Tue, 17 Mar 2026 14:07:17 +0000 (22:07 +0800)
committerGitHub <noreply@github.com>
Tue, 17 Mar 2026 14:07:17 +0000 (22:07 +0800)
Lib/test/test_capi/test_opt.py
Misc/NEWS.d/next/Core_and_Builtins/2026-02-08-01-19-50.gh-issue-131798.PaWDNH.rst [new file with mode: 0644]
Python/optimizer_bytecodes.c
Python/optimizer_cases.c.h

index 1385a7f840d7bec51603ab48ccb0cea28cfc2d9f..2a126a7b29cc7308fed93cf25b0f43070f54a2ab 100644 (file)
@@ -3981,6 +3981,22 @@ class TestUopsOptimization(unittest.TestCase):
         self.assertIn("_POP_TOP_NOP", uops)
         self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)
 
+    def test_iter_check_list(self):
+        def testfunc(n):
+            x = 0
+            for _ in range(n):
+                l = [1]
+                for num in l: # unguarded
+                    x += num
+            return x
+
+        res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
+        self.assertEqual(res, TIER2_THRESHOLD)
+        uops = get_opnames(ex)
+
+        self.assertIn("_BUILD_LIST", uops)
+        self.assertNotIn("_ITER_CHECK_LIST", uops)
+
     def test_match_class(self):
         def testfunc(n):
             class A:
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-08-01-19-50.gh-issue-131798.PaWDNH.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-08-01-19-50.gh-issue-131798.PaWDNH.rst
new file mode 100644 (file)
index 0000000..fe80c2d
--- /dev/null
@@ -0,0 +1 @@
+Optimize ``_ITER_CHECK_RANGE`` and ``_ITER_CHECK_LIST`` in the JIT
index f3a391b2e37ef98f9f6087a89779ac12cde4cd14..9b77fd0c32d9d9071804058256a1f0c2b1862136 100644 (file)
@@ -1091,6 +1091,24 @@ dummy_func(void) {
         sym_set_type(iter, &PyTuple_Type);
     }
 
+    op(_ITER_CHECK_LIST, (iter, null_or_index -- iter, null_or_index)) {
+        if (sym_matches_type(iter, &PyList_Type)) {
+            ADD_OP(_NOP, 0, 0);
+        }
+        else {
+            sym_set_type(iter, &PyList_Type);
+        }
+    }
+
+    op(_ITER_CHECK_RANGE, (iter, null_or_index -- iter, null_or_index)) {
+        if (sym_matches_type(iter, &PyRange_Type)) {
+            ADD_OP(_NOP, 0, 0);
+        }
+        else {
+            sym_set_type(iter, &PyRange_Type);
+        }
+    }
+
     op(_ITER_NEXT_RANGE, (iter, null_or_index -- iter, null_or_index, next)) {
        next = sym_new_type(ctx, &PyLong_Type);
     }
index 52a0c08ac677b71633bd74d18b7db608ae4aa926..eef07cedaf38e2c8f175bfc48f58025550e96bc2 100644 (file)
         /* _INSTRUMENTED_FOR_ITER is not a viable micro-op for tier 2 */
 
         case _ITER_CHECK_LIST: {
+            JitOptRef iter;
+            iter = stack_pointer[-2];
+            if (sym_matches_type(iter, &PyList_Type)) {
+                ADD_OP(_NOP, 0, 0);
+            }
+            else {
+                sym_set_type(iter, &PyList_Type);
+            }
             break;
         }
 
         }
 
         case _ITER_CHECK_RANGE: {
+            JitOptRef iter;
+            iter = stack_pointer[-2];
+            if (sym_matches_type(iter, &PyRange_Type)) {
+                ADD_OP(_NOP, 0, 0);
+            }
+            else {
+                sym_set_type(iter, &PyRange_Type);
+            }
             break;
         }