Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd9e44d

Browse files
committed
Issue #16286: optimize PyUnicode_RichCompare() for identical strings (same
pointer) for any operator, not only Py_EQ and Py_NE. Code of bytes_richcompare() and PyUnicode_RichCompare() is now closer.
1 parent c8bc537 commit fd9e44d

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

Objects/bytesobject.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -842,12 +842,20 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
842842
}
843843
else if (a == b) {
844844
switch (op) {
845-
case Py_EQ:case Py_LE:case Py_GE:
845+
case Py_EQ:
846+
case Py_LE:
847+
case Py_GE:
848+
/* a string is equal to itself */
846849
result = Py_True;
847850
break;
848-
case Py_NE:case Py_LT:case Py_GT:
851+
case Py_NE:
852+
case Py_LT:
853+
case Py_GT:
849854
result = Py_False;
850855
break;
856+
default:
857+
PyErr_BadArgument();
858+
return NULL;
851859
}
852860
}
853861
else if (op == Py_EQ || op == Py_NE) {
@@ -856,11 +864,12 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
856864
result = eq ? Py_True : Py_False;
857865
}
858866
else {
859-
len_a = Py_SIZE(a); len_b = Py_SIZE(b);
860-
min_len = (len_a < len_b) ? len_a : len_b;
867+
len_a = Py_SIZE(a);
868+
len_b = Py_SIZE(b);
869+
min_len = Py_MIN(len_a, len_b);
861870
if (min_len > 0) {
862871
c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
863-
if (c==0)
872+
if (c == 0)
864873
c = memcmp(a->ob_sval, b->ob_sval, min_len);
865874
}
866875
else
@@ -873,8 +882,8 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
873882
case Py_GT: c = c > 0; break;
874883
case Py_GE: c = c >= 0; break;
875884
default:
876-
assert(op != Py_EQ && op != Py_NE);
877-
Py_RETURN_NOTIMPLEMENTED;
885+
PyErr_BadArgument();
886+
return NULL;
878887
}
879888
result = c ? Py_True : Py_False;
880889
}

Objects/unicodeobject.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10534,10 +10534,6 @@ unicode_compare_eq(PyObject *str1, PyObject *str2)
1053410534
Py_ssize_t len;
1053510535
int cmp;
1053610536

10537-
/* a string is equal to itself */
10538-
if (str1 == str2)
10539-
return 1;
10540-
1054110537
len = PyUnicode_GET_LENGTH(str1);
1054210538
if (PyUnicode_GET_LENGTH(str2) != len)
1054310539
return 0;
@@ -10628,7 +10624,25 @@ PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
1062810624
PyUnicode_READY(right) == -1)
1062910625
return NULL;
1063010626

10631-
if (op == Py_EQ || op == Py_NE) {
10627+
if (left == right) {
10628+
switch (op) {
10629+
case Py_EQ:
10630+
case Py_LE:
10631+
case Py_GE:
10632+
/* a string is equal to itself */
10633+
v = Py_True;
10634+
break;
10635+
case Py_NE:
10636+
case Py_LT:
10637+
case Py_GT:
10638+
v = Py_False;
10639+
break;
10640+
default:
10641+
PyErr_BadArgument();
10642+
return NULL;
10643+
}
10644+
}
10645+
else if (op == Py_EQ || op == Py_NE) {
1063210646
result = unicode_compare_eq(left, right);
1063310647
result ^= (op == Py_NE);
1063410648
v = TEST_COND(result);

0 commit comments

Comments
 (0)