Skip to content

gh-131798: JIT: Assign type to sliced string #133609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 0 additions & 2 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1655,13 +1655,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
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.

Loading