mirror of https://github.com/python/cpython
gh-91146: Reduce allocation size of list from str.split()/rsplit() (gh-95473)
This commit is contained in:
parent
53357b3ee5
commit
50b2261bda
|
@ -0,0 +1,2 @@
|
||||||
|
Reduce allocation size of :class:`list` from :meth:`str.split`
|
||||||
|
and :meth:`str.rsplit`. Patch by Dong-hee Na.
|
|
@ -9696,40 +9696,40 @@ split(PyObject *self,
|
||||||
const void *buf1, *buf2;
|
const void *buf1, *buf2;
|
||||||
Py_ssize_t len1, len2;
|
Py_ssize_t len1, len2;
|
||||||
PyObject* out;
|
PyObject* out;
|
||||||
|
len1 = PyUnicode_GET_LENGTH(self);
|
||||||
if (maxcount < 0)
|
kind1 = PyUnicode_KIND(self);
|
||||||
maxcount = PY_SSIZE_T_MAX;
|
if (maxcount < 0) {
|
||||||
|
maxcount = len1;
|
||||||
|
}
|
||||||
|
|
||||||
if (substring == NULL)
|
if (substring == NULL)
|
||||||
switch (PyUnicode_KIND(self)) {
|
switch (kind1) {
|
||||||
case PyUnicode_1BYTE_KIND:
|
case PyUnicode_1BYTE_KIND:
|
||||||
if (PyUnicode_IS_ASCII(self))
|
if (PyUnicode_IS_ASCII(self))
|
||||||
return asciilib_split_whitespace(
|
return asciilib_split_whitespace(
|
||||||
self, PyUnicode_1BYTE_DATA(self),
|
self, PyUnicode_1BYTE_DATA(self),
|
||||||
PyUnicode_GET_LENGTH(self), maxcount
|
len1, maxcount
|
||||||
);
|
);
|
||||||
else
|
else
|
||||||
return ucs1lib_split_whitespace(
|
return ucs1lib_split_whitespace(
|
||||||
self, PyUnicode_1BYTE_DATA(self),
|
self, PyUnicode_1BYTE_DATA(self),
|
||||||
PyUnicode_GET_LENGTH(self), maxcount
|
len1, maxcount
|
||||||
);
|
);
|
||||||
case PyUnicode_2BYTE_KIND:
|
case PyUnicode_2BYTE_KIND:
|
||||||
return ucs2lib_split_whitespace(
|
return ucs2lib_split_whitespace(
|
||||||
self, PyUnicode_2BYTE_DATA(self),
|
self, PyUnicode_2BYTE_DATA(self),
|
||||||
PyUnicode_GET_LENGTH(self), maxcount
|
len1, maxcount
|
||||||
);
|
);
|
||||||
case PyUnicode_4BYTE_KIND:
|
case PyUnicode_4BYTE_KIND:
|
||||||
return ucs4lib_split_whitespace(
|
return ucs4lib_split_whitespace(
|
||||||
self, PyUnicode_4BYTE_DATA(self),
|
self, PyUnicode_4BYTE_DATA(self),
|
||||||
PyUnicode_GET_LENGTH(self), maxcount
|
len1, maxcount
|
||||||
);
|
);
|
||||||
default:
|
default:
|
||||||
Py_UNREACHABLE();
|
Py_UNREACHABLE();
|
||||||
}
|
}
|
||||||
|
|
||||||
kind1 = PyUnicode_KIND(self);
|
|
||||||
kind2 = PyUnicode_KIND(substring);
|
kind2 = PyUnicode_KIND(substring);
|
||||||
len1 = PyUnicode_GET_LENGTH(self);
|
|
||||||
len2 = PyUnicode_GET_LENGTH(substring);
|
len2 = PyUnicode_GET_LENGTH(substring);
|
||||||
if (kind1 < kind2 || len1 < len2) {
|
if (kind1 < kind2 || len1 < len2) {
|
||||||
out = PyList_New(1);
|
out = PyList_New(1);
|
||||||
|
@ -9783,39 +9783,40 @@ rsplit(PyObject *self,
|
||||||
Py_ssize_t len1, len2;
|
Py_ssize_t len1, len2;
|
||||||
PyObject* out;
|
PyObject* out;
|
||||||
|
|
||||||
if (maxcount < 0)
|
len1 = PyUnicode_GET_LENGTH(self);
|
||||||
maxcount = PY_SSIZE_T_MAX;
|
kind1 = PyUnicode_KIND(self);
|
||||||
|
if (maxcount < 0) {
|
||||||
|
maxcount = len1;
|
||||||
|
}
|
||||||
|
|
||||||
if (substring == NULL)
|
if (substring == NULL)
|
||||||
switch (PyUnicode_KIND(self)) {
|
switch (kind1) {
|
||||||
case PyUnicode_1BYTE_KIND:
|
case PyUnicode_1BYTE_KIND:
|
||||||
if (PyUnicode_IS_ASCII(self))
|
if (PyUnicode_IS_ASCII(self))
|
||||||
return asciilib_rsplit_whitespace(
|
return asciilib_rsplit_whitespace(
|
||||||
self, PyUnicode_1BYTE_DATA(self),
|
self, PyUnicode_1BYTE_DATA(self),
|
||||||
PyUnicode_GET_LENGTH(self), maxcount
|
len1, maxcount
|
||||||
);
|
);
|
||||||
else
|
else
|
||||||
return ucs1lib_rsplit_whitespace(
|
return ucs1lib_rsplit_whitespace(
|
||||||
self, PyUnicode_1BYTE_DATA(self),
|
self, PyUnicode_1BYTE_DATA(self),
|
||||||
PyUnicode_GET_LENGTH(self), maxcount
|
len1, maxcount
|
||||||
);
|
);
|
||||||
case PyUnicode_2BYTE_KIND:
|
case PyUnicode_2BYTE_KIND:
|
||||||
return ucs2lib_rsplit_whitespace(
|
return ucs2lib_rsplit_whitespace(
|
||||||
self, PyUnicode_2BYTE_DATA(self),
|
self, PyUnicode_2BYTE_DATA(self),
|
||||||
PyUnicode_GET_LENGTH(self), maxcount
|
len1, maxcount
|
||||||
);
|
);
|
||||||
case PyUnicode_4BYTE_KIND:
|
case PyUnicode_4BYTE_KIND:
|
||||||
return ucs4lib_rsplit_whitespace(
|
return ucs4lib_rsplit_whitespace(
|
||||||
self, PyUnicode_4BYTE_DATA(self),
|
self, PyUnicode_4BYTE_DATA(self),
|
||||||
PyUnicode_GET_LENGTH(self), maxcount
|
len1, maxcount
|
||||||
);
|
);
|
||||||
default:
|
default:
|
||||||
Py_UNREACHABLE();
|
Py_UNREACHABLE();
|
||||||
}
|
}
|
||||||
|
|
||||||
kind1 = PyUnicode_KIND(self);
|
|
||||||
kind2 = PyUnicode_KIND(substring);
|
kind2 = PyUnicode_KIND(substring);
|
||||||
len1 = PyUnicode_GET_LENGTH(self);
|
|
||||||
len2 = PyUnicode_GET_LENGTH(substring);
|
len2 = PyUnicode_GET_LENGTH(substring);
|
||||||
if (kind1 < kind2 || len1 < len2) {
|
if (kind1 < kind2 || len1 < len2) {
|
||||||
out = PyList_New(1);
|
out = PyList_New(1);
|
||||||
|
|
Loading…
Reference in New Issue