From f67a3a5a365640229845634909dd3a7b135947e5 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 2 Apr 2024 21:03:13 +0200 Subject: [PATCH] Optimize str.startswith for one and two character arguments --- Objects/unicodeobject.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index e412af5f797e7a..624e3b4466344d 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -9347,12 +9347,9 @@ tailmatch(PyObject *self, PyUnicode_READ(kind_self, data_self, offset + end_sub) == PyUnicode_READ(kind_sub, data_sub, end_sub)) { /* If both are of the same kind, memcmp is sufficient */ - if (kind_self == kind_sub) { - return ! memcmp((char *)data_self + - (offset * PyUnicode_KIND(substring)), - data_sub, - PyUnicode_GET_LENGTH(substring) * - PyUnicode_KIND(substring)); + if (kind_self == kind_sub && end_sub > 1) { + return ! memcmp((char *)data_self + (offset * kind_sub), + data_sub, end_sub * kind_sub); } /* otherwise we have to compare each character by first accessing it */ else {