Skip to content

Commit 9967aea

Browse files
committed
JIT: Assign type to sliced string
1 parent 1a137bc commit 9967aea

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
@@ -1096,6 +1096,18 @@ dummy_func(void) {
10961096
sym_set_const(callable, len);
10971097
}
10981098

1099+
op(_BINARY_SLICE, (container, start, stop -- res)) {
1100+
// Slicing a string always returns a string.
1101+
// TODO: We can apply this to lists and tuples as well.
1102+
// We'll start with string to simplify the process.
1103+
PyTypeObject *type = sym_get_type(container);
1104+
if (type == &PyUnicode_Type) {
1105+
res = sym_new_type(ctx, type);
1106+
} else {
1107+
res = sym_new_not_null(ctx);
1108+
}
1109+
}
1110+
10991111
// END BYTECODES //
11001112

11011113
}

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)