diff options
author | Victor Stinner <vstinner@python.org> | 2024-10-08 14:25:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-08 14:25:24 (GMT) |
commit | c203955f3b433e06118d00a2fe7215546a0b7fe6 (patch) | |
tree | 800381f8e139dbaab246440b0a9904e8f5a64d91 /Objects | |
parent | e99f159be4f70cf9e40865d638e79fa426968827 (diff) | |
download | cpython-c203955f3b433e06118d00a2fe7215546a0b7fe6.zip cpython-c203955f3b433e06118d00a2fe7215546a0b7fe6.tar.gz cpython-c203955f3b433e06118d00a2fe7215546a0b7fe6.tar.bz2 |
gh-124502: Optimize unicode_eq() (#125105)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/stringlib/eq.h | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/Objects/stringlib/eq.h b/Objects/stringlib/eq.h index 2eac4ba..821b692 100644 --- a/Objects/stringlib/eq.h +++ b/Objects/stringlib/eq.h @@ -4,14 +4,19 @@ * unicode_eq() is called when the hash of two unicode objects is equal. */ Py_LOCAL_INLINE(int) -unicode_eq(PyObject *a, PyObject *b) +unicode_eq(PyObject *str1, PyObject *str2) { - if (PyUnicode_GET_LENGTH(a) != PyUnicode_GET_LENGTH(b)) + Py_ssize_t len = PyUnicode_GET_LENGTH(str1); + if (PyUnicode_GET_LENGTH(str2) != len) { return 0; - if (PyUnicode_GET_LENGTH(a) == 0) - return 1; - if (PyUnicode_KIND(a) != PyUnicode_KIND(b)) + } + + int kind = PyUnicode_KIND(str1); + if (PyUnicode_KIND(str2) != kind) { return 0; - return memcmp(PyUnicode_1BYTE_DATA(a), PyUnicode_1BYTE_DATA(b), - PyUnicode_GET_LENGTH(a) * PyUnicode_KIND(a)) == 0; + } + + const void *data1 = PyUnicode_DATA(str1); + const void *data2 = PyUnicode_DATA(str2); + return (memcmp(data1, data2, len * kind) == 0); } |