Skip to content

gh-117431: Adapt str.{start,end}swith to use the METH_FASTCALL calling convention #117466

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
Apr 3, 2024
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
4 changes: 2 additions & 2 deletions Lib/test/string_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,9 +1513,9 @@ def test_find_etc_raise_correct_error_messages(self):
x, None, None, None)
self.assertRaisesRegex(TypeError, r'^count\(', s.count,
x, None, None, None)
self.assertRaisesRegex(TypeError, r'^startswith\(', s.startswith,
self.assertRaisesRegex(TypeError, r'^startswith\b', s.startswith,
x, None, None, None)
self.assertRaisesRegex(TypeError, r'^endswith\(', s.endswith,
self.assertRaisesRegex(TypeError, r'^endswith\b', s.endswith,
x, None, None, None)

# issue #15534
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve the performance of :meth:`str.startswith` and :meth:`str.endswith`
by adapting them to the :c:macro:`METH_FASTCALL` calling convention.
104 changes: 103 additions & 1 deletion Objects/clinic/unicodeobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 42 additions & 46 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -13021,40 +13021,41 @@ unicode_zfill_impl(PyObject *self, Py_ssize_t width)
return u;
}

PyDoc_STRVAR(startswith__doc__,
"S.startswith(prefix[, start[, end]]) -> bool\n\
\n\
Return True if S starts with the specified prefix, False otherwise.\n\
With optional start, test S beginning at that position.\n\
With optional end, stop comparing S at that position.\n\
prefix can also be a tuple of strings to try.");
/*[clinic input]
@text_signature "($self, prefix[, start[, end]], /)"
str.startswith as unicode_startswith

prefix as subobj: object
A string or a tuple of strings to try.
start: slice_index(accept={int, NoneType}, c_default='0') = None
Optional start position. Default: start of the string.
end: slice_index(accept={int, NoneType}, c_default='PY_SSIZE_T_MAX') = None
Optional stop position. Default: end of the string.
/

Return True if the string starts with the specified prefix, False otherwise.
[clinic start generated code]*/

static PyObject *
unicode_startswith(PyObject *self,
PyObject *args)
unicode_startswith_impl(PyObject *self, PyObject *subobj, Py_ssize_t start,
Py_ssize_t end)
/*[clinic end generated code: output=4bd7cfd0803051d4 input=5f918b5f5f89d856]*/
{
PyObject *subobj;
PyObject *substring;
Py_ssize_t start = 0;
Py_ssize_t end = PY_SSIZE_T_MAX;
int result;

if (!asciilib_parse_args_finds("startswith", args, &subobj, &start, &end))
return NULL;
if (PyTuple_Check(subobj)) {
Py_ssize_t i;
for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
substring = PyTuple_GET_ITEM(subobj, i);
PyObject *substring = PyTuple_GET_ITEM(subobj, i);
if (!PyUnicode_Check(substring)) {
PyErr_Format(PyExc_TypeError,
"tuple for startswith must only contain str, "
"not %.100s",
Py_TYPE(substring)->tp_name);
return NULL;
}
result = tailmatch(self, substring, start, end, -1);
if (result == -1)
int result = tailmatch(self, substring, start, end, -1);
if (result < 0) {
return NULL;
}
if (result) {
Py_RETURN_TRUE;
}
Expand All @@ -13068,47 +13069,41 @@ unicode_startswith(PyObject *self,
"a tuple of str, not %.100s", Py_TYPE(subobj)->tp_name);
return NULL;
}
result = tailmatch(self, subobj, start, end, -1);
if (result == -1)
int result = tailmatch(self, subobj, start, end, -1);
if (result < 0) {
return NULL;
}
return PyBool_FromLong(result);
}


PyDoc_STRVAR(endswith__doc__,
"S.endswith(suffix[, start[, end]]) -> bool\n\
\n\
Return True if S ends with the specified suffix, False otherwise.\n\
With optional start, test S beginning at that position.\n\
With optional end, stop comparing S at that position.\n\
suffix can also be a tuple of strings to try.");
/*[clinic input]
@text_signature "($self, prefix[, start[, end]], /)"
str.endswith as unicode_endswith = str.startswith

Return True if the string ends with the specified prefix, False otherwise.
[clinic start generated code]*/

static PyObject *
unicode_endswith(PyObject *self,
PyObject *args)
unicode_endswith_impl(PyObject *self, PyObject *subobj, Py_ssize_t start,
Py_ssize_t end)
/*[clinic end generated code: output=cce6f8ceb0102ca9 input=82cd5ce9e7623646]*/
{
PyObject *subobj;
PyObject *substring;
Py_ssize_t start = 0;
Py_ssize_t end = PY_SSIZE_T_MAX;
int result;

if (!asciilib_parse_args_finds("endswith", args, &subobj, &start, &end))
return NULL;
if (PyTuple_Check(subobj)) {
Py_ssize_t i;
for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
substring = PyTuple_GET_ITEM(subobj, i);
PyObject *substring = PyTuple_GET_ITEM(subobj, i);
if (!PyUnicode_Check(substring)) {
PyErr_Format(PyExc_TypeError,
"tuple for endswith must only contain str, "
"not %.100s",
Py_TYPE(substring)->tp_name);
return NULL;
}
result = tailmatch(self, substring, start, end, +1);
if (result == -1)
int result = tailmatch(self, substring, start, end, +1);
if (result < 0) {
return NULL;
}
if (result) {
Py_RETURN_TRUE;
}
Expand All @@ -13121,9 +13116,10 @@ unicode_endswith(PyObject *self,
"a tuple of str, not %.100s", Py_TYPE(subobj)->tp_name);
return NULL;
}
result = tailmatch(self, subobj, start, end, +1);
if (result == -1)
int result = tailmatch(self, subobj, start, end, +1);
if (result < 0) {
return NULL;
}
return PyBool_FromLong(result);
}

Expand Down Expand Up @@ -13576,8 +13572,8 @@ static PyMethodDef unicode_methods[] = {
UNICODE_SWAPCASE_METHODDEF
UNICODE_TRANSLATE_METHODDEF
UNICODE_UPPER_METHODDEF
{"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__},
{"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__},
UNICODE_STARTSWITH_METHODDEF
UNICODE_ENDSWITH_METHODDEF
UNICODE_REMOVEPREFIX_METHODDEF
UNICODE_REMOVESUFFIX_METHODDEF
UNICODE_ISASCII_METHODDEF
Expand Down