mirror of
https://github.com/python/cpython.git
synced 2025-10-22 14:42:22 +00:00
Speedup str[a🅱️step] for step != 1
Try to stop the scanner of the maximum character before the end using a limit depending on the kind (e.g. 256 for PyUnicode_2BYTE_KIND).
This commit is contained in:
parent
ae86485517
commit
c80d6d20d5
1 changed files with 23 additions and 3 deletions
|
@ -1520,6 +1520,22 @@ unicode_fromascii(const unsigned char* u, Py_ssize_t size)
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Py_UCS4
|
||||||
|
kind_maxchar_limit(unsigned int kind)
|
||||||
|
{
|
||||||
|
switch(kind) {
|
||||||
|
case PyUnicode_1BYTE_KIND:
|
||||||
|
return 0x80;
|
||||||
|
case PyUnicode_2BYTE_KIND:
|
||||||
|
return 0x100;
|
||||||
|
case PyUnicode_4BYTE_KIND:
|
||||||
|
return 0x10000;
|
||||||
|
default:
|
||||||
|
assert(0 && "invalid kind");
|
||||||
|
return 0x10ffff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static PyObject*
|
static PyObject*
|
||||||
_PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size)
|
_PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size)
|
||||||
{
|
{
|
||||||
|
@ -12335,7 +12351,7 @@ unicode_subscript(PyUnicodeObject* self, PyObject* item)
|
||||||
PyObject *result;
|
PyObject *result;
|
||||||
void *src_data, *dest_data;
|
void *src_data, *dest_data;
|
||||||
int src_kind, dest_kind;
|
int src_kind, dest_kind;
|
||||||
Py_UCS4 ch, max_char;
|
Py_UCS4 ch, max_char, kind_limit;
|
||||||
|
|
||||||
if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self),
|
if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self),
|
||||||
&start, &stop, &step, &slicelength) < 0) {
|
&start, &stop, &step, &slicelength) < 0) {
|
||||||
|
@ -12354,13 +12370,17 @@ unicode_subscript(PyUnicodeObject* self, PyObject* item)
|
||||||
start, start + slicelength);
|
start, start + slicelength);
|
||||||
}
|
}
|
||||||
/* General case */
|
/* General case */
|
||||||
max_char = 127;
|
max_char = 0;
|
||||||
src_kind = PyUnicode_KIND(self);
|
src_kind = PyUnicode_KIND(self);
|
||||||
|
kind_limit = kind_maxchar_limit(src_kind);
|
||||||
src_data = PyUnicode_DATA(self);
|
src_data = PyUnicode_DATA(self);
|
||||||
for (cur = start, i = 0; i < slicelength; cur += step, i++) {
|
for (cur = start, i = 0; i < slicelength; cur += step, i++) {
|
||||||
ch = PyUnicode_READ(src_kind, src_data, cur);
|
ch = PyUnicode_READ(src_kind, src_data, cur);
|
||||||
if (ch > max_char)
|
if (ch > max_char) {
|
||||||
max_char = ch;
|
max_char = ch;
|
||||||
|
if (max_char >= kind_limit)
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
result = PyUnicode_New(slicelength, max_char);
|
result = PyUnicode_New(slicelength, max_char);
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue