https://github.com/python/cpython/commit/cfeede85a79de3347168dba9a683363769209a86
commit: cfeede85a79de3347168dba9a683363769209a86
branch: main
author: Sacul <[email protected]>
committer: Fidget-Spinner <[email protected]>
date: 2026-02-08T18:08:26Z
summary:
GH-131798: Optimize `_GUARD_TOS_SLICE` (GH-144470)
files:
A
Misc/NEWS.d/next/Core_and_Builtins/2026-02-04-12-19-48.gh-issue-131798.My5jLy.rst
M Lib/test/test_capi/test_opt.py
M Python/optimizer_bytecodes.c
M Python/optimizer_cases.c.h
diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py
index 43b268b0206a46..765cb69dc04df1 100644
--- a/Lib/test/test_capi/test_opt.py
+++ b/Lib/test/test_capi/test_opt.py
@@ -1986,6 +1986,23 @@ def f(n):
self.assertNotIn("_GUARD_NOS_TUPLE", uops)
self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops)
+ def test_remove_guard_for_known_type_slice(self):
+ def f(n):
+ x = 0
+ for _ in range(n):
+ l = [1, 2, 3]
+ slice_obj = slice(0, 1)
+ x += l[slice_obj][0] # guarded
+ x += l[slice_obj][0] # unguarded
+ return x
+ res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
+ self.assertEqual(res, TIER2_THRESHOLD * 2)
+ uops = get_opnames(ex)
+
+ count = count_ops(ex, "_GUARD_TOS_SLICE")
+ self.assertEqual(count, 1)
+ self.assertIn("_BINARY_OP_SUBSCR_LIST_INT", uops)
+
def test_remove_guard_for_tuple_bounds_check(self):
def f(n):
x = 0
diff --git
a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-04-12-19-48.gh-issue-131798.My5jLy.rst
b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-04-12-19-48.gh-issue-131798.My5jLy.rst
new file mode 100644
index 00000000000000..849889f81fc323
--- /dev/null
+++
b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-04-12-19-48.gh-issue-131798.My5jLy.rst
@@ -0,0 +1 @@
+Optimise ``_GUARD_TOS_SLICE`` in the JIT.
diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c
index 0863d5dd8f8df7..e82770742a356c 100644
--- a/Python/optimizer_bytecodes.c
+++ b/Python/optimizer_bytecodes.c
@@ -1374,6 +1374,13 @@ dummy_func(void) {
}
}
+ op(_GUARD_TOS_SLICE, (tos -- tos)) {
+ if (sym_matches_type(tos, &PySlice_Type)) {
+ ADD_OP(_NOP, 0, 0);
+ }
+ sym_set_type(tos, &PySlice_Type);
+ }
+
op(_GUARD_NOS_NULL, (null, unused -- null, unused)) {
if (sym_is_null(null)) {
ADD_OP(_NOP, 0, 0);
diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h
index 9a51d2fa366661..cc1d28f49442b4 100644
--- a/Python/optimizer_cases.c.h
+++ b/Python/optimizer_cases.c.h
@@ -370,6 +370,12 @@
}
case _GUARD_TOS_SLICE: {
+ JitOptRef tos;
+ tos = stack_pointer[-1];
+ if (sym_matches_type(tos, &PySlice_Type)) {
+ ADD_OP(_NOP, 0, 0);
+ }
+ sym_set_type(tos, &PySlice_Type);
break;
}
_______________________________________________
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]