diff options
author | Brandt Bucher <brandtbucher@microsoft.com> | 2023-08-08 20:42:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-08 20:42:43 (GMT) |
commit | ea72c6fe3b6db5f4e8ce3d3405c0ea65dc002faf (patch) | |
tree | 97e185191d3f852d6533f5db5af5f32d7cc3d5f6 /Python/bytecodes.c | |
parent | aab6f7173a3b825599629dd6fa5cb7e477421595 (diff) | |
download | cpython-ea72c6fe3b6db5f4e8ce3d3405c0ea65dc002faf.zip cpython-ea72c6fe3b6db5f4e8ce3d3405c0ea65dc002faf.tar.gz cpython-ea72c6fe3b6db5f4e8ce3d3405c0ea65dc002faf.tar.bz2 |
GH-107596: Specialize str[int] (GH-107597)
Diffstat (limited to 'Python/bytecodes.c')
-rw-r--r-- | Python/bytecodes.c | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 90e26d3..d6bfb62 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -509,6 +509,7 @@ dummy_func( BINARY_SUBSCR_DICT, BINARY_SUBSCR_GETITEM, BINARY_SUBSCR_LIST_INT, + BINARY_SUBSCR_STR_INT, BINARY_SUBSCR_TUPLE_INT, }; @@ -574,6 +575,21 @@ dummy_func( Py_DECREF(list); } + inst(BINARY_SUBSCR_STR_INT, (unused/1, str, sub -- res)) { + DEOPT_IF(!PyLong_CheckExact(sub), BINARY_SUBSCR); + DEOPT_IF(!PyUnicode_CheckExact(str), BINARY_SUBSCR); + DEOPT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject *)sub), BINARY_SUBSCR); + Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0]; + DEOPT_IF(PyUnicode_GET_LENGTH(str) <= index, BINARY_SUBSCR); + // Specialize for reading an ASCII character from any string: + Py_UCS4 c = PyUnicode_READ_CHAR(str, index); + DEOPT_IF(Py_ARRAY_LENGTH(_Py_SINGLETON(strings).ascii) <= c, BINARY_SUBSCR); + STAT_INC(BINARY_SUBSCR, hit); + res = (PyObject*)&_Py_SINGLETON(strings).ascii[c]; + _Py_DECREF_SPECIALIZED(sub, (destructor)PyObject_Free); + Py_DECREF(str); + } + inst(BINARY_SUBSCR_TUPLE_INT, (unused/1, tuple, sub -- res)) { DEOPT_IF(!PyLong_CheckExact(sub), BINARY_SUBSCR); DEOPT_IF(!PyTuple_CheckExact(tuple), BINARY_SUBSCR); |