Skip to content

Commit 035b3e2

Browse files
committed
Improve performance of startswith by eliminating double work in tailmatch
1 parent 39d381f commit 035b3e2

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

Objects/unicodeobject.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9272,24 +9272,23 @@ tailmatch(PyObject *self,
92729272
else
92739273
offset = start;
92749274

9275-
if (PyUnicode_READ(kind_self, data_self, offset) ==
9276-
PyUnicode_READ(kind_sub, data_sub, 0) &&
9277-
PyUnicode_READ(kind_self, data_self, offset + end_sub) ==
9278-
PyUnicode_READ(kind_sub, data_sub, end_sub)) {
9275+
int last_character_matches = PyUnicode_READ(kind_self, data_self, offset + end_sub) ==
9276+
PyUnicode_READ(kind_sub, data_sub, end_sub);
9277+
9278+
if (last_character_matches) {
9279+
if (end_sub==0)
9280+
return 1;
92799281
/* If both are of the same kind, memcmp is sufficient */
92809282
if (kind_self == kind_sub) {
9281-
return ! memcmp((char *)data_self +
9282-
(offset * PyUnicode_KIND(substring)),
9283-
data_sub,
9284-
PyUnicode_GET_LENGTH(substring) *
9285-
PyUnicode_KIND(substring));
9283+
return ! memcmp((char *)data_self + (offset * kind_sub),
9284+
data_sub, end_sub * kind_sub);
92869285
}
92879286
/* otherwise we have to compare each character by first accessing it */
92889287
else {
92899288
/* We do not need to compare 0 and len(substring)-1 because
92909289
the if statement above ensured already that they are equal
92919290
when we end up here. */
9292-
for (i = 1; i < end_sub; ++i) {
9291+
for (i = 0; i < end_sub; ++i) {
92939292
if (PyUnicode_READ(kind_self, data_self, offset + i) !=
92949293
PyUnicode_READ(kind_sub, data_sub, i))
92959294
return 0;

0 commit comments

Comments
 (0)