Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
JIT: Assign type to sliced string
  • Loading branch information
fluhus committed May 7, 2025
commit 9967aea6388025076b1ebb31a508d6a8216612fb
3 changes: 0 additions & 3 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ def _run_with_optimizer(self, testfunc, arg):
ex = get_first_executor(testfunc)
return res, ex


def test_int_type_propagation(self):
def testfunc(loops):
num = 0
Expand Down Expand Up @@ -1655,13 +1654,11 @@ def testfunc(n):
self.assertIn("_CONTAINS_OP_DICT", uops)
self.assertNotIn("_TO_BOOL_BOOL", uops)


def test_remove_guard_for_known_type_str(self):
def f(n):
for i in range(n):
false = i == TIER2_THRESHOLD
empty = "X"[:false]
empty += "" # Make JIT realize this is a string.
if empty:
return 1
return 0
Expand Down
12 changes: 12 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,18 @@ dummy_func(void) {
sym_set_const(callable, len);
}

op(_BINARY_SLICE, (container, start, stop -- res)) {
// Slicing a string always returns a string.
// TODO: We can apply this to lists and tuples as well.
// We'll start with string to simplify the process.
Comment on lines +1100 to +1102
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanna just add the others to this PR too? It's just two lines here and a couple of tests.

PyTypeObject *type = sym_get_type(container);
if (type == &PyUnicode_Type) {
res = sym_new_type(ctx, type);
} else {
res = sym_new_not_null(ctx);
}
}

// END BYTECODES //

}
9 changes: 8 additions & 1 deletion Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.