Skip to content

Commit 4eade4a

Browse files
committed
JIT: Remove guard for slicing known type tuple
1 parent e45c280 commit 4eade4a

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,6 +2233,18 @@ def f(n):
22332233
self.assertIn("_TO_BOOL_LIST", uops)
22342234
self.assertNotIn("_GUARD_TOS_LIST", uops)
22352235

2236+
def test_remove_guard_for_slice_tuple(self):
2237+
def f(n):
2238+
for i in range(n):
2239+
false = i == TIER2_THRESHOLD
2240+
a, b = (1, 2, 3)[: false + 2]
2241+
2242+
_, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
2243+
self.assertIsNotNone(ex)
2244+
uops = get_opnames(ex)
2245+
self.assertIn("_UNPACK_SEQUENCE_TWO_TUPLE", uops)
2246+
self.assertNotIn("_GUARD_TOS_TUPLE", uops)
2247+
22362248

22372249
def global_identity(x):
22382250
return x

Python/optimizer_bytecodes.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,10 +1217,11 @@ dummy_func(void) {
12171217
}
12181218

12191219
op(_BINARY_SLICE, (container, start, stop -- res)) {
1220-
// Slicing a string/list always returns the same type.
1220+
// Slicing a string/list/tuple always returns the same type.
12211221
PyTypeObject *type = sym_get_type(container);
12221222
if (type == &PyUnicode_Type ||
1223-
type == &PyList_Type) {
1223+
type == &PyList_Type ||
1224+
type == &PyTuple_Type) {
12241225
res = sym_new_type(ctx, type);
12251226
} else {
12261227
res = sym_new_not_null(ctx);

Python/optimizer_cases.c.h

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)