Skip to content
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
8 changes: 4 additions & 4 deletions Include/internal/pycore_opcode_metadata.h

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

9 changes: 7 additions & 2 deletions Include/internal/pycore_uop_ids.h

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

21 changes: 21 additions & 0 deletions Include/internal/pycore_uop_metadata.h

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve optimized handling of non-negative integer indices for lists,
tuples, and strings.
56 changes: 29 additions & 27 deletions Modules/_testinternalcapi/test_cases.c.h

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

31 changes: 17 additions & 14 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,8 @@ dummy_func(
macro(STORE_SLICE) = _SPECIALIZE_STORE_SLICE + _STORE_SLICE;

macro(BINARY_OP_SUBSCR_LIST_INT) =
_GUARD_TOS_INT + _GUARD_NOS_LIST + unused/5 + _BINARY_OP_SUBSCR_LIST_INT + _POP_TOP_INT + POP_TOP;
_GUARD_TOS_INT + _GUARD_NOS_LIST + unused/5 +
_BINARY_OP_SUBSCR_LIST_INT + _POP_TOP_INT + POP_TOP;

op(_BINARY_OP_SUBSCR_LIST_INT, (list_st, sub_st -- res, ls, ss)) {
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
Expand Down Expand Up @@ -1185,15 +1186,15 @@ dummy_func(
}

macro(BINARY_OP_SUBSCR_STR_INT) =
_GUARD_TOS_INT + _GUARD_NOS_COMPACT_ASCII + unused/5 + _BINARY_OP_SUBSCR_STR_INT + _POP_TOP_INT + _POP_TOP_UNICODE;
_GUARD_TOS_NON_NEGATIVE_COMPACT_INT + _GUARD_NOS_COMPACT_ASCII +
unused/5 + _BINARY_OP_SUBSCR_STR_INT + _POP_TOP_INT + _POP_TOP_UNICODE;

op(_BINARY_OP_SUBSCR_STR_INT, (str_st, sub_st -- res, s, i)) {
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
PyObject *str = PyStackRef_AsPyObjectBorrow(str_st);

assert(PyLong_CheckExact(sub));
assert(PyUnicode_CheckExact(str));
EXIT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject*)sub));
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
EXIT_IF(PyUnicode_GET_LENGTH(str) <= index);
uint8_t c = PyUnicode_1BYTE_DATA(str)[index];
Expand All @@ -1207,15 +1208,15 @@ dummy_func(
}

macro(BINARY_OP_SUBSCR_USTR_INT) =
_GUARD_TOS_INT + _GUARD_NOS_UNICODE + unused/5 + _BINARY_OP_SUBSCR_USTR_INT + _POP_TOP_INT + _POP_TOP_UNICODE;
_GUARD_TOS_NON_NEGATIVE_COMPACT_INT + _GUARD_NOS_UNICODE +
unused/5 + _BINARY_OP_SUBSCR_USTR_INT + _POP_TOP_INT + _POP_TOP_UNICODE;

op(_BINARY_OP_SUBSCR_USTR_INT, (str_st, sub_st -- res, s, i)) {
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
PyObject *str = PyStackRef_AsPyObjectBorrow(str_st);

assert(PyLong_CheckExact(sub));
assert(PyUnicode_CheckExact(str));
EXIT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject*)sub));
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
EXIT_IF(PyUnicode_GET_LENGTH(str) <= index);
// Specialize for reading an ASCII character from any string:
Expand All @@ -1240,7 +1241,7 @@ dummy_func(
}

macro(BINARY_OP_SUBSCR_TUPLE_INT) =
_GUARD_TOS_INT +
_GUARD_TOS_NON_NEGATIVE_COMPACT_INT +
_GUARD_NOS_TUPLE +
_GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS +
unused/5 +
Expand All @@ -1256,8 +1257,7 @@ dummy_func(
assert(PyLong_CheckExact(sub));
assert(PyTuple_CheckExact(tuple));

// Deopt unless 0 <= sub < PyTuple_Size(list)
EXIT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject *)sub));
// Deopt unless sub < PyTuple_Size(list)
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
EXIT_IF(index >= PyTuple_GET_SIZE(tuple));
}
Expand Down Expand Up @@ -1411,8 +1411,15 @@ dummy_func(

macro(STORE_SUBSCR) = _SPECIALIZE_STORE_SUBSCR + _STORE_SUBSCR;

op(_GUARD_TOS_NON_NEGATIVE_COMPACT_INT, (value -- value)) {
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
EXIT_IF(!PyLong_CheckExact(value_o));
EXIT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject *)value_o));
}

macro(STORE_SUBSCR_LIST_INT) =
_GUARD_TOS_INT + _GUARD_NOS_LIST + unused/1 + _STORE_SUBSCR_LIST_INT + _POP_TOP_INT + POP_TOP;
_GUARD_TOS_NON_NEGATIVE_COMPACT_INT + _GUARD_NOS_LIST +
unused/1 + _STORE_SUBSCR_LIST_INT + _POP_TOP_INT + POP_TOP;

op(_STORE_SUBSCR_LIST_INT, (value, list_st, sub_st -- ls, ss)) {
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
Expand All @@ -1424,11 +1431,7 @@ dummy_func(
Py_ssize_t index = _PyLong_CompactValue((PyLongObject *)sub);
DEOPT_IF(!LOCK_OBJECT(list));
Py_ssize_t len = PyList_GET_SIZE(list);
// Ensure index < len(list)
if (index < 0) {
index += len;
}
if (index < 0 || index >= len) {
if (index >= len) {
UNLOCK_OBJECT(list);
DEOPT_IF(true);
}
Expand Down
Loading
Loading