8000 gh-91146: More reduce allocation size of list from str.split/rsplit by corona10 · Pull Request #95493 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-91146: More reduce allocation size of list from str.split/rsplit #95493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Reduce allocation size of :class:`list` from :meth:`str.split`
and :meth:`str.rsplit`. Patch by Dong-hee Na.
and :meth:`str.rsplit`. Patch by Dong-hee Na and Inada Naoki.
31 changes: 22 additions & 9 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -9698,11 +9698,11 @@ split(PyObject *self,
PyObject* out;
len1 = PyUnicode_GET_LENGTH(self);
kind1 = PyUnicode_KIND(self);
if (maxcount < 0) {
maxcount = len1;
}

if (substring == NULL)
if (substring == NULL) {
if (maxcount < 0) {
maxcount = (len1 - 1) / 2 + 1;
}
switch (kind1) {
case PyUnicode_1BYTE_KIND:
if (PyUnicode_IS_ASCII(self))
Expand All @@ -9728,9 +9728,16 @@ split(PyObject *self,
default:
Py_UNREACHABLE();
}
}

kind2 = PyUnicode_KIND(substring);
len2 = PyUnicode_GET_LENGTH(substring);
if (maxcount < 0) {
// if len2 == 0, it will raise ValueError.
maxcount = len2 == 0 ? 0 : (len1 / len2) + 1;
// handle expected overflow case: (Py_SSIZE_T_MAX / 1) + 1
maxcount = maxcount < 0 ? len1 : maxcount;
}
if (kind1 < kind2 || len1 < len2) {
out = PyList_New(1);
if (out == NULL)
Expand Down Expand Up @@ -9785,11 +9792,11 @@ rsplit(PyObject *self,

len1 = PyUnicode_GET_LENGTH(self);
kind1 = PyUnicode_KIND(self);
if (maxcount < 0) {
maxcount = len1;
}

if (substring == NULL)
if (substring == NULL) {
if (maxcount < 0) {
maxcount = (len1 - 1) / 2 + 1;
}
switch (kind1) {
case PyUnicode_1BYTE_KIND:
if (PyUnicode_IS_ASCII(self))
Expand All @@ -9815,9 +9822,15 @@ rsplit(PyObject *self,
default:
Py_UNREACHABLE();
}

}
kind2 = PyUnicode_KIND(substring);
len2 = PyUnicode_GET_LENGTH(substring);
if (maxcount < 0) {
// if len2 == 0, it will raise ValueError.
maxcount = len2 == 0 ? 0 : (len1 / len2) + 1;
// handle expected overflow case: (Py_SSIZE_T_MAX / 1) + 1
maxcount = maxcount < 0 ? len1 : maxcount;
}
if (kind1 < kind2 || len1 < len2) {
out = PyList_New(1);
if (out == NULL)
Expand Down
0