Skip to content

Commit cfeede8

Browse files
authored
GH-131798: Optimize _GUARD_TOS_SLICE (GH-144470)
1 parent d736349 commit cfeede8

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,23 @@ def f(n):
19861986
self.assertNotIn("_GUARD_NOS_TUPLE", uops)
19871987
self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops)
19881988

1989+
def test_remove_guard_for_known_type_slice(self):
1990+
def f(n):
1991+
x = 0
1992+
for _ in range(n):
1993+
l = [1, 2, 3]
1994+
slice_obj = slice(0, 1)
1995+
x += l[slice_obj][0] # guarded
1996+
x += l[slice_obj][0] # unguarded
1997+
return x
1998+
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
1999+
self.assertEqual(res, TIER2_THRESHOLD * 2)
2000+
uops = get_opnames(ex)
2001+
2002+
count = count_ops(ex, "_GUARD_TOS_SLICE")
2003+
self.assertEqual(count, 1)
2004+
self.assertIn("_BINARY_OP_SUBSCR_LIST_INT", uops)
2005+
19892006
def test_remove_guard_for_tuple_bounds_check(self):
19902007
def f(n):
19912008
x = 0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimise ``_GUARD_TOS_SLICE`` in the JIT.

Python/optimizer_bytecodes.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,13 @@ dummy_func(void) {
13741374
}
13751375
}
13761376

1377+
op(_GUARD_TOS_SLICE, (tos -- tos)) {
1378+
if (sym_matches_type(tos, &PySlice_Type)) {
1379+
ADD_OP(_NOP, 0, 0);
1380+
}
1381+
sym_set_type(tos, &PySlice_Type);
1382+
}
1383+
13771384
op(_GUARD_NOS_NULL, (null, unused -- null, unused)) {
13781385
if (sym_is_null(null)) {
13791386
ADD_OP(_NOP, 0, 0);

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)