]> granicus.if.org Git - python/commitdiff
Issue #16286: optimize PyUnicode_RichCompare() for identical strings (same
authorVictor Stinner <victor.stinner@gmail.com>
Mon, 4 Nov 2013 10:23:05 +0000 (11:23 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Mon, 4 Nov 2013 10:23:05 +0000 (11:23 +0100)
pointer) for any operator, not only Py_EQ and Py_NE.

Code of bytes_richcompare() and PyUnicode_RichCompare() is now closer.

Objects/bytesobject.c
Objects/unicodeobject.c

index 9aa3ee286b1cecd2473f5b97300e4ded81a28451..0a9d04d6db7765c73383afe76d40d0759c7d302b 100644 (file)
@@ -842,12 +842,20 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
     }
     else if (a == b) {
         switch (op) {
-        case Py_EQ:case Py_LE:case Py_GE:
+        case Py_EQ:
+        case Py_LE:
+        case Py_GE:
+            /* a string is equal to itself */
             result = Py_True;
             break;
-        case Py_NE:case Py_LT:case Py_GT:
+        case Py_NE:
+        case Py_LT:
+        case Py_GT:
             result = Py_False;
             break;
+        default:
+            PyErr_BadArgument();
+            return NULL;
         }
     }
     else if (op == Py_EQ || op == Py_NE) {
@@ -856,11 +864,12 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
         result = eq ? Py_True : Py_False;
     }
     else {
-        len_a = Py_SIZE(a); len_b = Py_SIZE(b);
-        min_len = (len_a < len_b) ? len_a : len_b;
+        len_a = Py_SIZE(a);
+        len_b = Py_SIZE(b);
+        min_len = Py_MIN(len_a, len_b);
         if (min_len > 0) {
             c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
-            if (c==0)
+            if (c == 0)
                 c = memcmp(a->ob_sval, b->ob_sval, min_len);
         }
         else
@@ -873,8 +882,8 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
         case Py_GT: c = c >  0; break;
         case Py_GE: c = c >= 0; break;
         default:
-            assert(op != Py_EQ && op != Py_NE);
-            Py_RETURN_NOTIMPLEMENTED;
+            PyErr_BadArgument();
+            return NULL;
         }
         result = c ? Py_True : Py_False;
     }
index 17ae481aec9c5b6afa01a863af56a123ed739b04..f0aff5f16ae5ba68ca6188bd924b211a32568ba6 100644 (file)
@@ -10534,10 +10534,6 @@ unicode_compare_eq(PyObject *str1, PyObject *str2)
     Py_ssize_t len;
     int cmp;
 
-    /* a string is equal to itself */
-    if (str1 == str2)
-        return 1;
-
     len = PyUnicode_GET_LENGTH(str1);
     if (PyUnicode_GET_LENGTH(str2) != len)
         return 0;
@@ -10628,7 +10624,25 @@ PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
         PyUnicode_READY(right) == -1)
         return NULL;
 
-    if (op == Py_EQ || op == Py_NE) {
+    if (left == right) {
+        switch (op) {
+        case Py_EQ:
+        case Py_LE:
+        case Py_GE:
+            /* a string is equal to itself */
+            v = Py_True;
+            break;
+        case Py_NE:
+        case Py_LT:
+        case Py_GT:
+            v = Py_False;
+            break;
+        default:
+            PyErr_BadArgument();
+            return NULL;
+        }
+    }
+    else if (op == Py_EQ || op == Py_NE) {
         result = unicode_compare_eq(left, right);
         result ^= (op == Py_NE);
         v = TEST_COND(result);