diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-11-13-21-02.gh-issue-155555.CYo1PR.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-11-13-21-02.gh-issue-155555.CYo1PR.rst new file mode 100644 index 00000000000000..e215edca19b084 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-11-13-21-02.gh-issue-155555.CYo1PR.rst @@ -0,0 +1 @@ +String search is now up to to 20x faster when string and pattern are of the same size. diff --git a/Objects/stringlib/fastsearch.h b/Objects/stringlib/fastsearch.h index 26bb0555ca9b6c..9dd514e452796e 100644 --- a/Objects/stringlib/fastsearch.h +++ b/Objects/stringlib/fastsearch.h @@ -795,6 +795,15 @@ FASTSEARCH(const STRINGLIB_CHAR* s, Py_ssize_t n, return STRINGLIB(count_char)(s, n, p[0], maxcount); } } + else if (n == m) { + /* use special case when both strings are of equal length */ + int res = memcmp(s, p, m * sizeof(STRINGLIB_CHAR)); + if (mode == FAST_COUNT){ + return res == 0 ? 1 : 0; + } else { + return res == 0 ? 0 : -1; + } + } if (mode != FAST_RSEARCH) { if (n < 2500 || (m < 100 && n < 30000) || m < 6) {