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

Skip to content

gh-91146: Reduce allocation size of list from str.split()/rsplit() #95473

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 2 commits into from
Jul 31, 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
@@ -0,0 +1,2 @@
Reduce allocation size of :class:`list` from :meth:`str.split`
and :meth:`str.rsplit`. Patch by Dong-hee Na.
39 changes: 20 additions & 19 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -9696,40 +9696,40 @@ split(PyObject *self,
const void *buf1, *buf2;
Py_ssize_t len1, len2;
PyObject* out;

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

if (substring == NULL)
switch (PyUnicode_KIND(self)) {
switch (kind1) {
case PyUnicode_1BYTE_KIND:
if (PyUnicode_IS_ASCII(self))
return asciilib_split_whitespace(
self, PyUnicode_1BYTE_DATA(self),
PyUnicode_GET_LENGTH(self), maxcount
len1, maxcount
);
else
return ucs1lib_split_whitespace(
self, PyUnicode_1BYTE_DATA(self),
PyUnicode_GET_LENGTH(self), maxcount
len1, maxcount
);
case PyUnicode_2BYTE_KIND:
return ucs2lib_split_whitespace(
self, PyUnicode_2BYTE_DATA(self),
PyUnicode_GET_LENGTH(self), maxcount
len1, maxcount
);
case PyUnicode_4BYTE_KIND:
return ucs4lib_split_whitespace(
self, PyUnicode_4BYTE_DATA(self),
PyUnicode_GET_LENGTH(self), maxcount
len1, maxcount
);
default:
Py_UNREACHABLE();
}

kind1 = PyUnicode_KIND(self);
kind2 = PyUnicode_KIND(substring);
len1 = PyUnicode_GET_LENGTH(self);
len2 = PyUnicode_GET_LENGTH(substring);
if (kind1 < kind2 || len1 < len2) {
out = PyList_New(1);
Expand Down Expand Up @@ -9783,39 +9783,40 @@ rsplit(PyObject *self,
Py_ssize_t len1, len2;
PyObject* out;

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

if (substring == NULL)
switch (PyUnicode_KIND(self)) {
switch (kind1) {
case PyUnicode_1BYTE_KIND:
if (PyUnicode_IS_ASCII(self))
return asciilib_rsplit_whitespace(
self, PyUnicode_1BYTE_DATA(self),
PyUnicode_GET_LENGTH(self), maxcount
len1, maxcount
);
else
return ucs1lib_rsplit_whitespace(
self, PyUnicode_1BYTE_DATA(self),
PyUnicode_GET_LENGTH(self), maxcount
len1, maxcount
);
case PyUnicode_2BYTE_KIND:
return ucs2lib_rsplit_whitespace(
self, PyUnicode_2BYTE_DATA(self),
PyUnicode_GET_LENGTH(self), maxcount
len1, maxcount
);
case PyUnicode_4BYTE_KIND:
return ucs4lib_rsplit_whitespace(
self, PyUnicode_4BYTE_DATA(self),
PyUnicode_GET_LENGTH(self), maxcount
len1, maxcount
);
default:
Py_UNREACHABLE();
}

kind1 = PyUnicode_KIND(self);
kind2 = PyUnicode_KIND(substring);
len1 = PyUnicode_GET_LENGTH(self);
len2 = PyUnicode_GET_LENGTH(substring);
if (kind1 < kind2 || len1 < len2) {
out = PyList_New(1);
Expand Down
0