Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 14 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2287,6 +2287,20 @@ def testfunc(n):
self.assertNotIn("_GUARD_TOS_INT", uops)
self.assertIn("_POP_TOP_NOP", uops)

def test_call_len_string(self):
def testfunc(n):
for _ in range(n):
_ = len("abc")
d = ''
_ = len(d)

_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
print(uops)

This comment was marked as resolved.

self.assertNotIn("_CALL_LEN", uops)
self.assertIn("_SHUFFLE_3_LOAD_CONST_INLINE_BORROW", uops)

def test_call_len_known_length_small_int(self):
# Make sure that len(t) is optimized for a tuple of length 5.
# See https://github.com/python/cpython/issues/139393.
Expand Down
19 changes: 16 additions & 3 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1417,12 +1417,25 @@ dummy_func(void) {

op(_CALL_LEN, (callable, null, arg -- res, a, c)) {
res = sym_new_type(ctx, &PyLong_Type);
Py_ssize_t tuple_length = sym_tuple_length(arg);
if (tuple_length >= 0) {
PyObject *temp = PyLong_FromSsize_t(tuple_length);
PyObject *temp = NULL;

Py_ssize_t length = sym_tuple_length(arg);
if (length >= 0) {
temp = PyLong_FromSsize_t(length);
if (temp == NULL) {
goto error;
}
}
else if (sym_is_const(ctx, arg)) {
PyObject *const_val = sym_get_const(ctx, arg);
if (const_val != NULL && PyUnicode_CheckExact(const_val)) {
temp = PyLong_FromSsize_t(PyUnicode_GET_LENGTH(const_val));
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.

Can we also deal with the PyBytes_CheckExact + PyBytes_GET_SIZE?

if (temp == NULL) {
goto error;
}
}
}
Comment on lines +1428 to +1441
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.

I think we can optimize the logic here:

        if (length < 0 && sym_is_const(ctx, arg)) {
            PyObject *const_val = sym_get_const(ctx, arg);
            if (const_val != NULL && PyUnicode_CheckExact(const_val)) {
                length = PyUnicode_GET_LENGTH(const_val);
            }
        }
        if (length >= 0) {
            PyObject *temp = PyLong_FromSsize_t(length);
            if (temp == NULL) {
                goto error;
            }
            if (_Py_IsImmortal(temp)) {
                ADD_OP(_SHUFFLE_3_LOAD_CONST_INLINE_BORROW,
                        0, (uintptr_t)temp);
            }
            res = sym_new_const(ctx, temp);
            Py_DECREF(temp);
        }

if (temp != NULL) {
if (_Py_IsImmortal(temp)) {
ADD_OP(_SHUFFLE_3_LOAD_CONST_INLINE_BORROW,
0, (uintptr_t)temp);
Expand Down
18 changes: 15 additions & 3 deletions Python/optimizer_cases.c.h

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

Loading