Skip to content

Commit 61207d2

Browse files
committed
JIT: Assign type to sliced string
1 parent 1000283 commit 61207d2

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@ def _run_with_optimizer(self, testfunc, arg):
458458
ex = get_first_executor(testfunc)
459459
return res, ex
460460

461-
462461
def test_int_type_propagation(self):
463462
def testfunc(loops):
464463
num = 0
@@ -1655,13 +1654,11 @@ def testfunc(n):
16551654
self.assertIn("_CONTAINS_OP_DICT", uops)
16561655
self.assertNotIn("_TO_BOOL_BOOL", uops)
16571656

1658-
16591657
def test_remove_guard_for_known_type_str(self):
16601658
def f(n):
16611659
for i in range(n):
16621660
false = i == TIER2_THRESHOLD
16631661
empty = "X"[:false]
1664-
empty += "" # Make JIT realize this is a string.
16651662
if empty:
16661663
return 1
16671664
return 0

Python/optimizer_bytecodes.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,18 @@ dummy_func(void) {
12161216
sym_set_const(callable, list_append);
12171217
}
12181218

1219+
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.
1223+
PyTypeObject *type = sym_get_type(container);
1224+
if (type == &PyUnicode_Type) {
1225+
res = sym_new_type(ctx, type);
1226+
} else {
1227+
res = sym_new_not_null(ctx);
1228+
}
1229+
}
1230+
12191231
// END BYTECODES //
12201232

12211233
}

Python/optimizer_cases.c.h

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