Skip to content

Commit 1efd993

Browse files
authored
gh-131798: Optimize _ITER_CHECK_RANGE and _ITER_CHECK_LIST in the JIT (GH-144583)
1 parent 9d7621b commit 1efd993

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3981,6 +3981,22 @@ def testfunc(n):
39813981
self.assertIn("_POP_TOP_NOP", uops)
39823982
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)
39833983

3984+
def test_iter_check_list(self):
3985+
def testfunc(n):
3986+
x = 0
3987+
for _ in range(n):
3988+
l = [1]
3989+
for num in l: # unguarded
3990+
x += num
3991+
return x
3992+
3993+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
3994+
self.assertEqual(res, TIER2_THRESHOLD)
3995+
uops = get_opnames(ex)
3996+
3997+
self.assertIn("_BUILD_LIST", uops)
3998+
self.assertNotIn("_ITER_CHECK_LIST", uops)
3999+
39844000
def test_match_class(self):
39854001
def testfunc(n):
39864002
class A:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimize ``_ITER_CHECK_RANGE`` and ``_ITER_CHECK_LIST`` in the JIT

Python/optimizer_bytecodes.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,24 @@ dummy_func(void) {
10911091
sym_set_type(iter, &PyTuple_Type);
10921092
}
10931093

1094+
op(_ITER_CHECK_LIST, (iter, null_or_index -- iter, null_or_index)) {
1095+
if (sym_matches_type(iter, &PyList_Type)) {
1096+
ADD_OP(_NOP, 0, 0);
1097+
}
1098+
else {
1099+
sym_set_type(iter, &PyList_Type);
1100+
}
1101+
}
1102+
1103+
op(_ITER_CHECK_RANGE, (iter, null_or_index -- iter, null_or_index)) {
1104+
if (sym_matches_type(iter, &PyRange_Type)) {
1105+
ADD_OP(_NOP, 0, 0);
1106+
}
1107+
else {
1108+
sym_set_type(iter, &PyRange_Type);
1109+
}
1110+
}
1111+
10941112
op(_ITER_NEXT_RANGE, (iter, null_or_index -- iter, null_or_index, next)) {
10951113
next = sym_new_type(ctx, &PyLong_Type);
10961114
}

Python/optimizer_cases.c.h

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)