FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-131798: JIT: Assign type to sliced string/list/tuple by fluhus · Pull Request #134671 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) .py  (1) .rst  (1) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
30 changes: 28 additions & 2 deletions Lib/test/test_capi/test_opt.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Nice!

if empty:
return 1
return 0
Expand Down Expand Up @@ -2219,6 +2217,34 @@ def f(n):
self.assertNotIn("_LOAD_ATTR_METHOD_NO_DICT", uops)
self.assertNotIn("_LOAD_ATTR_METHOD_LAZY_DICT", uops)

def test_remove_guard_for_slice_list(self):
def f(n):
for i in range(n):
false = i == TIER2_THRESHOLD
sliced = [1, 2, 3][:false]
if sliced:
return 1
return 0

res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertEqual(res, 0)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_TO_BOOL_LIST", uops)
self.assertNotIn("_GUARD_TOS_LIST", uops)

def test_remove_guard_for_slice_tuple(self):
def f(n):
for i in range(n):
false = i == TIER2_THRESHOLD
a, b = (1, 2, 3)[: false + 2]

_, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_UNPACK_SEQUENCE_TWO_TUPLE", uops)
self.assertNotIn("_GUARD_TOS_TUPLE", uops)


def global_identity(x):
return x
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make the JIT optimizer understand that slicing a string/list/tuple returns the same type.
14 changes: 14 additions & 0 deletions Python/optimizer_bytecodes.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,20 @@ dummy_func(void) {
sym_set_const(callable, list_append);
}

op(_BINARY_SLICE, (container, start, stop -- res)) {
// Slicing a string/list/tuple always returns the same type.
PyTypeObject *type = sym_get_type(container);
if (type == &PyUnicode_Type ||
type == &PyList_Type ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
type == &PyList_Type ||
type == &PyBytes_Type ||
type == &PyList_Type ||

I do not know whether slicing a bytes object occurs often enough to add PyBytes_Type here (and whether the jit slows down if we add more cases here), but if I understand correctly slicing bytes (or bytearray) results in a bytes (or bytearray) so we could add them.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@fluhus, this PR is probably fine for now, but feel free to add other sequence types like this in a follow-up PR (with tests)!

type == &PyTuple_Type)
{
res = sym_new_type(ctx, type);
}
else {
res = sym_new_not_null(ctx);
}
}

// END BYTECODES //

}
13 changes: 12 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.


Back | FazBrowse Home | New Git URL