Skip to content

Commit 48ad552

Browse files
corona10methane
andcommitted
gh-91146: More reduce allocation size of list from str.split/rsplit
Co-authored-by: Inada Naoki <songofacandy@gmail.com>
1 parent 50b2261 commit 48ad552

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

Objects/unicodeobject.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9698,11 +9698,11 @@ split(PyObject *self,
96989698
PyObject* out;
96999699
len1 = PyUnicode_GET_LENGTH(self);
97009700
kind1 = PyUnicode_KIND(self);
9701-
if (maxcount < 0) {
9702-
maxcount = len1;
9703-
}
97049701

9705-
if (substring == NULL)
9702+
if (substring == NULL) {
9703+
if (maxcount < 0) {
9704+
maxcount = (len1 + 1) / 2;
9705+
}
97069706
switch (kind1) {
97079707
case PyUnicode_1BYTE_KIND:
97089708
if (PyUnicode_IS_ASCII(self))
@@ -9728,9 +9728,13 @@ split(PyObject *self,
97289728
default:
97299729
Py_UNREACHABLE();
97309730
}
9731+
}
97319732

97329733
kind2 = PyUnicode_KIND(substring);
97339734
len2 = PyUnicode_GET_LENGTH(substring);
9735+
if (maxcount < 0) {
9736+
maxcount = len1 / len2 + 1;
9737+
}
97349738
if (kind1 < kind2 || len1 < len2) {
97359739
out = PyList_New(1);
97369740
if (out == NULL)
@@ -9785,11 +9789,11 @@ rsplit(PyObject *self,
97859789

97869790
len1 = PyUnicode_GET_LENGTH(self);
97879791
kind1 = PyUnicode_KIND(self);
9788-
if (maxcount < 0) {
9789-
maxcount = len1;
9790-
}
97919792

9792-
if (substring == NULL)
9793+
if (substring == NULL) {
9794+
if (maxcount < 0) {
9795+
maxcount = (len1 + 1) / 2;
9796+
}
97939797
switch (kind1) {
97949798
case PyUnicode_1BYTE_KIND:
97959799
if (PyUnicode_IS_ASCII(self))
@@ -9815,9 +9819,12 @@ rsplit(PyObject *self,
98159819
default:
98169820
Py_UNREACHABLE();
98179821
}
9818-
9822+
}
98199823
kind2 = PyUnicode_KIND(substring);
98209824
len2 = PyUnicode_GET_LENGTH(substring);
9825+
if (maxcount < 0) {
9826+
maxcount = len1 / len2 + 1;
9827+
}
98219828
if (kind1 < kind2 || len1 < len2) {
98229829
out = PyList_New(1);
98239830
if (out == NULL)

0 commit comments

Comments
 (0)