Skip to content

Commit ea72c6f

Browse files
authored
GH-107596: Specialize str[int] (GH-107597)
1 parent aab6f71 commit ea72c6f

10 files changed

+172
-97
lines changed

Include/internal/pycore_opcode.h

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_opcode_metadata.h

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/opcode.h

Lines changed: 52 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/_opcode_metadata.py

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Specialize subscripting :class:`str` objects by :class:`int` indexes.

Python/bytecodes.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ dummy_func(
509509
BINARY_SUBSCR_DICT,
510510
BINARY_SUBSCR_GETITEM,
511511
BINARY_SUBSCR_LIST_INT,
512+
BINARY_SUBSCR_STR_INT,
512513
BINARY_SUBSCR_TUPLE_INT,
513514
};
514515

@@ -574,6 +575,21 @@ dummy_func(
574575
Py_DECREF(list);
575576
}
576577

578+
inst(BINARY_SUBSCR_STR_INT, (unused/1, str, sub -- res)) {
579+
DEOPT_IF(!PyLong_CheckExact(sub), BINARY_SUBSCR);
580+
DEOPT_IF(!PyUnicode_CheckExact(str), BINARY_SUBSCR);
581+
DEOPT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject *)sub), BINARY_SUBSCR);
582+
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
583+
DEOPT_IF(PyUnicode_GET_LENGTH(str) <= index, BINARY_SUBSCR);
584+
// Specialize for reading an ASCII character from any string:
585+
Py_UCS4 c = PyUnicode_READ_CHAR(str, index);
586+
DEOPT_IF(Py_ARRAY_LENGTH(_Py_SINGLETON(strings).ascii) <= c, BINARY_SUBSCR);
587+
STAT_INC(BINARY_SUBSCR, hit);
588+
res = (PyObject*)&_Py_SINGLETON(strings).ascii[c];
589+
_Py_DECREF_SPECIALIZED(sub, (destructor)PyObject_Free);
590+
Py_DECREF(str);
591+
}
592+
577593
inst(BINARY_SUBSCR_TUPLE_INT, (unused/1, tuple, sub -- res)) {
578594
DEOPT_IF(!PyLong_CheckExact(sub), BINARY_SUBSCR);
579595
DEOPT_IF(!PyTuple_CheckExact(tuple), BINARY_SUBSCR);

Python/executor_cases.c.h

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)