Skip to content

Commit a7c5099

Browse files
eendebakptclaudeblurb-it[bot]
authored
gh-159037: Improve performance of str.join pre-pass (#159038)
* gh-159037: Speed up the pre-pass of str.join() Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 436d791 commit a7c5099

3 files changed

Lines changed: 39 additions & 18 deletions

File tree

‎Lib/test/test_str.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,18 @@ def __str__(self): return self.sval
590590
self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
591591
self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd'))
592592
self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz'))
593+
# mixed kinds of the separator and the items
594+
self.checkequalnofix('a\xe9b', '\xe9', 'join', ['a', 'b'])
595+
self.checkequalnofix('a\u20acb', '\u20ac', 'join', ['a', 'b'])
596+
self.checkequalnofix('a\xe9', '', 'join', ['a', '\xe9'])
597+
self.checkequalnofix('\u20ac\xe9', '', 'join', ['\u20ac', '\xe9'])
598+
self.checkequalnofix('\xe9-\u20ac-\U0001f600', '-', 'join',
599+
['\xe9', '\u20ac', '\U0001f600'])
600+
self.checkequalnofix('\U0001f600-a', '-', 'join', ['\U0001f600', 'a'])
601+
# a single item which is a str subclass is copied
602+
class S(str): pass
603+
for s in 'a', '\xe9', '\u20ac', '\U0001f600':
604+
self.checkequalnofix(s, '-', 'join', [S(s)])
593605
self.checkraises(TypeError, ' ', 'join', ['1', '2', MyWrapper('foo')])
594606
self.checkraises(TypeError, ' ', 'join', ['1', '2', '3', bytes()])
595607
self.checkraises(TypeError, ' ', 'join', [1, 2, 3])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve performance of :meth:`str.join`.

‎Objects/unicodeobject.c‎

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10454,10 +10454,10 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq
1045410454
PyObject *item;
1045510455
Py_ssize_t sz, i, res_offset;
1045610456
Py_UCS4 maxchar;
10457-
Py_UCS4 item_maxchar;
10457+
/* kinds is the bitwise OR of the PyUnicode_KIND() values (1, 2, 4). */
10458+
unsigned int kinds = 0, all_ascii = 1;
1045810459
int use_memcpy;
1045910460
unsigned char *res_data = NULL, *sep_data = NULL;
10460-
PyObject *last_obj;
1046110461
int kind = 0;
1046210462

1046310463
/* If empty sequence, return u"". */
@@ -10466,14 +10466,12 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq
1046610466
}
1046710467

1046810468
/* If singleton sequence with an exact Unicode, return that. */
10469-
last_obj = NULL;
1047010469
if (seqlen == 1) {
1047110470
if (PyUnicode_CheckExact(items[0])) {
1047210471
res = items[0];
1047310472
return Py_NewRef(res);
1047410473
}
1047510474
seplen = 0;
10476-
maxchar = 0;
1047710475
}
1047810476
else {
1047910477
/* Set up sep and seplen */
@@ -10483,7 +10481,6 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq
1048310481
if (!sep)
1048410482
goto onError;
1048510483
seplen = 1;
10486-
maxchar = 32;
1048710484
}
1048810485
else {
1048910486
if (!PyUnicode_Check(separator)) {
@@ -10495,12 +10492,12 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq
1049510492
}
1049610493
sep = separator;
1049710494
seplen = PyUnicode_GET_LENGTH(separator);
10498-
maxchar = PyUnicode_MAX_CHAR_VALUE(separator);
1049910495
/* inc refcount to keep this code path symmetric with the
1050010496
above case of a blank separator */
1050110497
Py_INCREF(sep);
1050210498
}
10503-
last_obj = sep;
10499+
kinds = PyUnicode_KIND(sep);
10500+
all_ascii = PyUnicode_IS_ASCII(sep);
1050410501
}
1050510502

1050610503
/* There are at least two things to join, or else we have a subclass
@@ -10524,23 +10521,34 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq
1052410521
i, Py_TYPE(item)->tp_name);
1052510522
goto onError;
1052610523
}
10527-
add_sz = PyUnicode_GET_LENGTH(item);
10528-
item_maxchar = PyUnicode_MAX_CHAR_VALUE(item);
10529-
maxchar = Py_MAX(maxchar, item_maxchar);
10530-
if (i != 0) {
10531-
add_sz += seplen;
10532-
}
10524+
/* Count one separator per item; the extra one is removed below. */
10525+
add_sz = (size_t)PyUnicode_GET_LENGTH(item) + (size_t)seplen;
10526+
kinds |= PyUnicode_KIND(item);
10527+
all_ascii &= PyUnicode_IS_ASCII(item);
1053310528
if (add_sz > (size_t)(PY_SSIZE_T_MAX - sz)) {
1053410529
PyErr_SetString(PyExc_OverflowError,
1053510530
"join() result is too long for a Python string");
1053610531
goto onError;
1053710532
}
1053810533
sz += add_sz;
10539-
if (use_memcpy && last_obj != NULL) {
10540-
if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item))
10541-
use_memcpy = 0;
10542-
}
10543-
last_obj = item;
10534+
}
10535+
sz -= seplen;
10536+
/* memcpy() can be used if the separator and all items have the same
10537+
kind: the result then has that kind too. */
10538+
if (kinds & (kinds - 1)) {
10539+
use_memcpy = 0;
10540+
}
10541+
if (all_ascii) {
10542+
maxchar = 0x7f;
10543+
}
10544+
else if (kinds & PyUnicode_4BYTE_KIND) {
10545+
maxchar = MAX_UNICODE;
10546+
}
10547+
else if (kinds & PyUnicode_2BYTE_KIND) {
10548+
maxchar = 0xffff;
10549+
}
10550+
else {
10551+
maxchar = 0xff;
1054410552
}
1054510553

1054610554
res = PyUnicode_New(sz, maxchar);

0 commit comments

Comments
 (0)