]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-124502: Optimize unicode_eq() (#125105)
authorVictor Stinner <vstinner@python.org>
Tue, 8 Oct 2024 14:25:24 +0000 (16:25 +0200)
committerGitHub <noreply@github.com>
Tue, 8 Oct 2024 14:25:24 +0000 (16:25 +0200)
Objects/stringlib/eq.h

index 2eac4baf5ca9ceaad50b998bc651b0f44e98e97f..821b692f26b8308c7540bdc9563002fd3a0e3964 100644 (file)
@@ -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);
 }