Commit ce14184b authored by Stefan Behnel's avatar Stefan Behnel

optimise str == unicode / unicode == str in Py2

parent 9ef2b067
...@@ -138,10 +138,32 @@ static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int ...@@ -138,10 +138,32 @@ static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int
#if CYTHON_COMPILING_IN_PYPY #if CYTHON_COMPILING_IN_PYPY
return PyObject_RichCompareBool(s1, s2, equals); return PyObject_RichCompareBool(s1, s2, equals);
#else #else
#if PY_MAJOR_VERSION < 3
PyObject* owned_ref = NULL;
#endif
int s1_is_unicode, s2_is_unicode;
if (s1 == s2) { if (s1 == s2) {
/* as done by PyObject_RichCompareBool(); also catches the (interned) empty string */ /* as done by PyObject_RichCompareBool(); also catches the (interned) empty string */
return (equals == Py_EQ); goto return_eq;
} else if (PyUnicode_CheckExact(s1) & PyUnicode_CheckExact(s2)) { }
s1_is_unicode = PyUnicode_CheckExact(s1);
s2_is_unicode = PyUnicode_CheckExact(s2);
#if PY_MAJOR_VERSION < 3
if ((s1_is_unicode & (!s2_is_unicode)) && PyString_CheckExact(s2)) {
owned_ref = PyUnicode_FromObject(s2);
if (unlikely(!owned_ref))
return -1;
s2 = owned_ref;
s2_is_unicode = 1;
} else if ((s2_is_unicode & (!s1_is_unicode)) && PyString_CheckExact(s1)) {
owned_ref = PyUnicode_FromObject(s1);
if (unlikely(!owned_ref))
return -1;
s1 = owned_ref;
s1_is_unicode = 1;
}
#endif
if (s1_is_unicode & s2_is_unicode) {
Py_ssize_t length; Py_ssize_t length;
int kind; int kind;
void *data1, *data2; void *data1, *data2;
...@@ -150,26 +172,31 @@ static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int ...@@ -150,26 +172,31 @@ static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int
return -1; return -1;
#endif #endif
length = __Pyx_PyUnicode_GET_LENGTH(s1); length = __Pyx_PyUnicode_GET_LENGTH(s1);
if (length != __Pyx_PyUnicode_GET_LENGTH(s2)) if (length != __Pyx_PyUnicode_GET_LENGTH(s2)) {
return (equals == Py_NE); goto return_ne;
}
// len(s1) == len(s2) >= 1 (empty string is interned, and "s1 is not s2") // len(s1) == len(s2) >= 1 (empty string is interned, and "s1 is not s2")
kind = __Pyx_PyUnicode_KIND(s1); kind = __Pyx_PyUnicode_KIND(s1);
if (kind != __Pyx_PyUnicode_KIND(s2)) if (kind != __Pyx_PyUnicode_KIND(s2)) {
return (equals == Py_NE); goto return_ne;
}
data1 = __Pyx_PyUnicode_DATA(s1); data1 = __Pyx_PyUnicode_DATA(s1);
data2 = __Pyx_PyUnicode_DATA(s2); data2 = __Pyx_PyUnicode_DATA(s2);
if (__Pyx_PyUnicode_READ(kind, data1, 0) != __Pyx_PyUnicode_READ(kind, data2, 0)) { if (__Pyx_PyUnicode_READ(kind, data1, 0) != __Pyx_PyUnicode_READ(kind, data2, 0)) {
return (equals == Py_NE); goto return_ne;
} else if (length == 1) { } else if (length == 1) {
return (equals == Py_EQ); goto return_eq;
} else { } else {
int result = memcmp(data1, data2, length * kind); int result = memcmp(data1, data2, length * kind);
#if PY_MAJOR_VERSION < 3
Py_XDECREF(owned_ref);
#endif
return (equals == Py_EQ) ? (result == 0) : (result != 0); return (equals == Py_EQ) ? (result == 0) : (result != 0);
} }
} else if ((s1 == Py_None) & PyUnicode_CheckExact(s2)) { } else if ((s1 == Py_None) & s2_is_unicode) {
return (equals == Py_NE); goto return_ne;
} else if ((s2 == Py_None) & PyUnicode_CheckExact(s1)) { } else if ((s2 == Py_None) & s1_is_unicode) {
return (equals == Py_NE); goto return_ne;
} else { } else {
int result; int result;
PyObject* py_result = PyObject_RichCompare(s1, s2, equals); PyObject* py_result = PyObject_RichCompare(s1, s2, equals);
...@@ -179,6 +206,16 @@ static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int ...@@ -179,6 +206,16 @@ static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int
Py_DECREF(py_result); Py_DECREF(py_result);
return result; return result;
} }
return_eq:
#if PY_MAJOR_VERSION < 3
Py_XDECREF(owned_ref);
#endif
return (equals == Py_EQ);
return_ne:
#if PY_MAJOR_VERSION < 3
Py_XDECREF(owned_ref);
#endif
return (equals == Py_NE);
#endif #endif
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment