Skip to content

Commit e6a0f2a

Browse files
committed
add _GUARD_TOS_SLICE to optimizer_bytecodes
1 parent 34e5a63 commit e6a0f2a

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,23 @@ def f(n):
20452045
self.assertNotIn("_GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS", uops)
20462046
self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops)
20472047

2048+
def test_remove_guard_for_known_type_slice(self):
2049+
def f(n):
2050+
x = 0
2051+
for _ in range(n):
2052+
l = [1, 2, 3]
2053+
slice_obj = slice(0, 1)
2054+
x += l[slice_obj][0] # guarded
2055+
x += l[slice_obj][0] # unguarded
2056+
return x
2057+
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
2058+
self.assertEqual(res, TIER2_THRESHOLD * 2)
2059+
uops = get_opnames(ex)
2060+
2061+
count = count_ops(ex, "_GUARD_TOS_SLICE")
2062+
self.assertEqual(count, 1)
2063+
self.assertIn("_BINARY_OP_SUBSCR_LIST_INT", uops)
2064+
20482065
def test_binary_subcsr_str_int_narrows_to_str(self):
20492066
def testfunc(n):
20502067
x = []

Python/optimizer_bytecodes.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,13 @@ dummy_func(void) {
14161416
ADD_OP(_NOP, 0, 0);
14171417
}
14181418
}
1419+
op(_GUARD_TOS_SLICE, (tos -- tos)){
1420+
if (sym_matches_type(tos, &PySlice_Type)){
1421+
ADD_OP(_NOP, 0, 0);
1422+
}
1423+
sym_set_type(tos, &PySlice_Type);
1424+
}
1425+
14191426

14201427
op(_GUARD_NOS_NULL, (null, unused -- null, unused)) {
14211428
if (sym_is_null(null)) {

Python/optimizer_cases.c.h

Lines changed: 6 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)