Skip to content
Draft
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
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.

42 changes: 42 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1890,6 +1890,17 @@ def binary_subscr_tuple_int():
"BINARY_OP_SUBSCR_TUPLE_INT")
self.assert_no_opcode(binary_subscr_tuple_int, "BINARY_OP")

def binary_subscr_tuple_negative_int():
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
a = (1, 2, 3)
idx = -1
self.assertEqual(a[idx], 3)

binary_subscr_tuple_negative_int()
self.assert_specialized(binary_subscr_tuple_negative_int,
"BINARY_OP_SUBSCR_TUPLE_INT")
self.assert_no_opcode(binary_subscr_tuple_negative_int, "BINARY_OP")

def binary_subscr_dict():
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
a = {1: 2, 2: 3}
Expand Down Expand Up @@ -1967,6 +1978,16 @@ def binary_subscr_str_int():
self.assert_specialized(binary_subscr_str_int, "BINARY_OP_SUBSCR_STR_INT")
self.assert_no_opcode(binary_subscr_str_int, "BINARY_OP")

def binary_subscr_str_negative_int():
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
a = "foobar"
idx = -1
self.assertEqual(a[idx], "r")

binary_subscr_str_negative_int()
self.assert_specialized(binary_subscr_str_negative_int, "BINARY_OP_SUBSCR_STR_INT")
self.assert_no_opcode(binary_subscr_str_negative_int, "BINARY_OP")

def binary_subscr_str_int_non_compact():
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
a = "바이트코드_특수화"
Expand All @@ -1977,6 +1998,16 @@ def binary_subscr_str_int_non_compact():
self.assert_specialized(binary_subscr_str_int_non_compact, "BINARY_OP_SUBSCR_USTR_INT")
self.assert_no_opcode(binary_subscr_str_int_non_compact, "BINARY_OP_SUBSCR_STR_INT")

def binary_subscr_str_negative_int_non_compact():
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
a = "바이트코드_특수화"
idx = -1
self.assertEqual(a[idx], "화")

binary_subscr_str_negative_int_non_compact()
self.assert_specialized(binary_subscr_str_negative_int_non_compact, "BINARY_OP_SUBSCR_USTR_INT")
self.assert_no_opcode(binary_subscr_str_negative_int_non_compact, "BINARY_OP_SUBSCR_STR_INT")

def binary_subscr_getitems():
class C:
def __init__(self, val):
Expand Down Expand Up @@ -2026,6 +2057,17 @@ def store_subscr_defaultdict():
self.assert_specialized(store_subscr_defaultdict, "STORE_SUBSCR_DICT")
self.assert_no_opcode(store_subscr_defaultdict, "STORE_SUBSCR")

def store_subscr_list_negative_int():
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
a = [1, 2, 3]
idx = -1
a[idx] = 4
self.assertEqual(a, [1, 2, 4])

store_subscr_list_negative_int()
self.assert_specialized(store_subscr_list_negative_int, "STORE_SUBSCR_LIST_INT")
self.assert_no_opcode(store_subscr_list_negative_int, "STORE_SUBSCR")

def store_subscr_dict_subclass_override():
class MyDict(dict):
def __setitem__(self, key, value):
Expand Down
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.
38 changes: 19 additions & 19 deletions Modules/_testinternalcapi/test_cases.c.h

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

52 changes: 36 additions & 16 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,17 +1186,21 @@ 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_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);
Py_ssize_t index = _PyLong_CompactValue((PyLongObject *)sub);
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
if (index < 0) {
index += len;
}
EXIT_IF(index < 0 || len <= index);
uint8_t c = PyUnicode_1BYTE_DATA(str)[index];
assert(c < 128);
STAT_INC(BINARY_OP, hit);
Expand All @@ -1207,17 +1212,21 @@ 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_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);
Py_ssize_t index = _PyLong_CompactValue((PyLongObject *)sub);
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
if (index < 0) {
index += len;
}
EXIT_IF(index < 0 || len <= index);
// Specialize for reading an ASCII character from any string:
Py_UCS4 c = PyUnicode_READ_CHAR(str, index);
EXIT_IF(Py_ARRAY_LENGTH(_Py_SINGLETON(strings).ascii) <= c);
Expand Down Expand Up @@ -1256,10 +1265,12 @@ dummy_func(
assert(PyLong_CheckExact(sub));
assert(PyTuple_CheckExact(tuple));

// Deopt unless 0 <= sub < PyTuple_Size(list)
EXIT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject *)sub));
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
EXIT_IF(index >= PyTuple_GET_SIZE(tuple));
Py_ssize_t index = _PyLong_CompactValue((PyLongObject *)sub);
Py_ssize_t len = PyTuple_GET_SIZE(tuple);
if (index < 0) {
index += len;
}
EXIT_IF(index < 0 || index >= len);
}

op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res, ts, ss)) {
Expand All @@ -1270,7 +1281,10 @@ dummy_func(
assert(PyTuple_CheckExact(tuple));

STAT_INC(BINARY_OP, hit);
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
Py_ssize_t index = _PyLong_CompactValue((PyLongObject *)sub);
if (index < 0) {
index += PyTuple_GET_SIZE(tuple);
}
PyObject *res_o = PyTuple_GET_ITEM(tuple, index);
assert(res_o != NULL);
res = PyStackRef_FromPyObjectNew(res_o);
Expand Down Expand Up @@ -1411,8 +1425,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);
assert(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_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,7 +1445,6 @@ 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;
}
Expand Down
Loading
Loading