Skip to content

Commit e45c280

Browse files
committed
JIT: Remove guard for slicing known type list
1 parent e281791 commit e45c280

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,22 @@ def f(n):
22172217
self.assertNotIn("_LOAD_ATTR_METHOD_NO_DICT", uops)
22182218
self.assertNotIn("_LOAD_ATTR_METHOD_LAZY_DICT", uops)
22192219

2220+
def test_remove_guard_for_slice_list(self):
2221+
def f(n):
2222+
for i in range(n):
2223+
false = i == TIER2_THRESHOLD
2224+
sliced = [1, 2, 3][:false]
2225+
if sliced:
2226+
return 1
2227+
return 0
2228+
2229+
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
2230+
self.assertEqual(res, 0)
2231+
self.assertIsNotNone(ex)
2232+
uops = get_opnames(ex)
2233+
self.assertIn("_TO_BOOL_LIST", uops)
2234+
self.assertNotIn("_GUARD_TOS_LIST", uops)
2235+
22202236

22212237
def global_identity(x):
22222238
return x

Python/optimizer_bytecodes.c

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

12191219
op(_BINARY_SLICE, (container, start, stop -- res)) {
1220-
// Slicing a string always returns a string.
1221-
// TODO: We can apply this to lists and tuples as well.
1222-
// We'll start with string to simplify the process.
1220+
// Slicing a string/list always returns the same type.
12231221
PyTypeObject *type = sym_get_type(container);
1224-
if (type == &PyUnicode_Type) {
1222+
if (type == &PyUnicode_Type ||
1223+
type == &PyList_Type) {
12251224
res = sym_new_type(ctx, type);
12261225
} else {
12271226
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)