Skip to content

Commit c203955

Browse files
authored
gh-124502: Optimize unicode_eq() (#125105)
1 parent e99f159 commit c203955

File tree

1 file changed

+12
-7
lines changed
  • Objects/stringlib

1 file changed

+12
-7
lines changed

Objects/stringlib/eq.h

+12-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
* unicode_eq() is called when the hash of two unicode objects is equal.
55
*/
66
Py_LOCAL_INLINE(int)
7-
unicode_eq(PyObject *a, PyObject *b)
7+
unicode_eq(PyObject *str1, PyObject *str2)
88
{
9-
if (PyUnicode_GET_LENGTH(a) != PyUnicode_GET_LENGTH(b))
9+
Py_ssize_t len = PyUnicode_GET_LENGTH(str1);
10+
if (PyUnicode_GET_LENGTH(str2) != len) {
1011
return 0;
11-
if (PyUnicode_GET_LENGTH(a) == 0)
12-
return 1;
13-
if (PyUnicode_KIND(a) != PyUnicode_KIND(b))
12+
}
13+
14+
int kind = PyUnicode_KIND(str1);
15+
if (PyUnicode_KIND(str2) != kind) {
1416
return 0;
15-
return memcmp(PyUnicode_1BYTE_DATA(a), PyUnicode_1BYTE_DATA(b),
16-
PyUnicode_GET_LENGTH(a) * PyUnicode_KIND(a)) == 0;
17+
}
18+
19+
const void *data1 = PyUnicode_DATA(str1);
20+
const void *data2 = PyUnicode_DATA(str2);
21+
return (memcmp(data1, data2, len * kind) == 0);
1722
}

0 commit comments

Comments
 (0)