Skip to content

Commit 6b59d12

Browse files
committed
Use PythonObjectLibrary.equals for deque richcompare
1 parent 508d187 commit 6b59d12

File tree

1 file changed

+57
-16
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/deque

1 file changed

+57
-16
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/deque/DequeBuiltins.java

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797
import com.oracle.graal.python.nodes.SpecialMethodNames;
9898
import com.oracle.graal.python.nodes.control.GetNextNode;
9999
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
100-
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode.EqNode;
101100
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode.GeNode;
102101
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode.GtNode;
103102
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode.LeNode;
@@ -112,7 +111,6 @@
112111
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
113112
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
114113
import com.oracle.graal.python.nodes.util.CannotCastException;
115-
import com.oracle.graal.python.nodes.util.CastToJavaBooleanNode;
116114
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
117115
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
118116
import com.oracle.graal.python.runtime.PythonContext;
@@ -904,6 +902,9 @@ Object doGeneric(PDeque self,
904902

905903
public abstract static class DequeCompareNode extends PythonBinaryBuiltinNode {
906904

905+
@Child private PythonObjectLibrary selfItemLib;
906+
@Child private PythonObjectLibrary otherItemLib;
907+
907908
@Specialization(guards = "shortcutIdentityCheck(self, other)")
908909
@SuppressWarnings("unused")
909910
static boolean doSame(PDeque self, PDeque other) {
@@ -922,10 +923,7 @@ Object doGeneric(VirtualFrame frame, PDeque self, Object other,
922923
@CachedLibrary("other") PythonObjectLibrary otherLib,
923924
@Cached GetNextNode selfItNextNode,
924925
@Cached GetNextNode otherItNextNode,
925-
@Cached EqNode eqNode,
926-
@Cached("createCmp()") BinaryComparisonNode comparisonNode,
927-
@Cached IsBuiltinClassProfile profile,
928-
@Cached CastToJavaBooleanNode castToJavaBooleanNode) {
926+
@Cached IsBuiltinClassProfile profile) {
929927
if (!isPDeque(other)) {
930928
return PNotImplemented.NOT_IMPLEMENTED;
931929
}
@@ -945,12 +943,12 @@ Object doGeneric(VirtualFrame frame, PDeque self, Object other,
945943
try {
946944
Object selfItem = selfItNextNode.execute(frame, ait);
947945
Object otherItem = otherItNextNode.execute(frame, bit);
948-
if (!castToJavaBooleanNode.execute(eqNode.executeWith(frame, selfItem, otherItem))) {
949-
return castToJavaBooleanNode.execute(comparisonNode.executeWith(frame, selfItem, otherItem));
946+
if (!compareEq(frame, selfItem, otherItem)) {
947+
return compare(frame, selfItem, otherItem);
950948
}
951949
} catch (PException e) {
952950
e.expect(StopIteration, profile);
953-
return comparisonNode.cmp(self.getSize(), otherDeque.getSize());
951+
return compare(frame, self.getSize(), otherDeque.getSize());
954952
}
955953
}
956954
}
@@ -969,7 +967,27 @@ boolean shortcutLengthCheck(PDeque self, PDeque other) {
969967
return true;
970968
}
971969

972-
BinaryComparisonNode createCmp() {
970+
/**
971+
* Compares two items using
972+
* {@link PythonObjectLibrary#equals(Object, Object, PythonObjectLibrary)}. Unfortunately,
973+
* we cannot use
974+
* {@link com.oracle.graal.python.nodes.expression.BinaryComparisonNode.EqNode} because
975+
* CPython uses {@code PyObject_RichCompareBool} which has a special case for identity.
976+
*/
977+
final boolean compareEq(VirtualFrame frame, Object selfItem, Object otherItem) {
978+
if (selfItemLib == null) {
979+
CompilerDirectives.transferToInterpreterAndInvalidate();
980+
selfItemLib = insert(PythonObjectLibrary.getFactory().createDispatched(3));
981+
}
982+
if (otherItemLib == null) {
983+
CompilerDirectives.transferToInterpreterAndInvalidate();
984+
otherItemLib = insert(PythonObjectLibrary.getFactory().createDispatched(3));
985+
}
986+
return selfItemLib.equalsWithFrame(selfItem, otherItem, otherItemLib, frame);
987+
}
988+
989+
@SuppressWarnings("unused")
990+
boolean compare(VirtualFrame frame, Object selfItem, Object otherItem) {
973991
throw CompilerDirectives.shouldNotReachHere();
974992
}
975993
}
@@ -979,7 +997,7 @@ BinaryComparisonNode createCmp() {
979997
public abstract static class DequeEqNode extends DequeCompareNode {
980998
@Specialization(guards = "!shortcutLengthCheck(self, other)", insertBefore = "doGeneric")
981999
@SuppressWarnings("unused")
982-
static boolean doSame(PDeque self, PDeque other) {
1000+
static boolean doDifferentLength(PDeque self, PDeque other) {
9831001
return false;
9841002
}
9851003

@@ -994,14 +1012,37 @@ boolean shortcutLengthCheck(PDeque self, PDeque other) {
9941012
}
9951013

9961014
@Override
1015+
boolean compare(VirtualFrame frame, Object selfItem, Object otherItem) {
1016+
return compareEq(frame, selfItem, otherItem);
1017+
}
1018+
}
1019+
1020+
public abstract static class DequeRelCompareNode extends DequeCompareNode {
1021+
1022+
@Child private BinaryComparisonNode comparisonNode;
1023+
@Child private PythonObjectLibrary lib;
1024+
1025+
@Override
1026+
boolean compare(VirtualFrame frame, Object selfItem, Object otherItem) {
1027+
if (comparisonNode == null) {
1028+
CompilerDirectives.transferToInterpreterAndInvalidate();
1029+
comparisonNode = insert(createCmp());
1030+
}
1031+
if (lib == null) {
1032+
CompilerDirectives.transferToInterpreterAndInvalidate();
1033+
lib = insert(PythonObjectLibrary.getFactory().createDispatched(3));
1034+
}
1035+
return lib.isTrue(comparisonNode.executeWith(frame, selfItem, otherItem), frame);
1036+
}
1037+
9971038
BinaryComparisonNode createCmp() {
998-
return EqNode.create();
1039+
throw CompilerDirectives.shouldNotReachHere();
9991040
}
10001041
}
10011042

10021043
@Builtin(name = __LE__, minNumOfPositionalArgs = 2)
10031044
@GenerateNodeFactory
1004-
public abstract static class DequeLeNode extends DequeCompareNode {
1045+
public abstract static class DequeLeNode extends DequeRelCompareNode {
10051046
@Override
10061047
boolean shortcutIdentityCheck(Object self, Object other) {
10071048
return self == other;
@@ -1015,7 +1056,7 @@ BinaryComparisonNode createCmp() {
10151056

10161057
@Builtin(name = __LT__, minNumOfPositionalArgs = 2)
10171058
@GenerateNodeFactory
1018-
public abstract static class DequeLtNode extends DequeCompareNode {
1059+
public abstract static class DequeLtNode extends DequeRelCompareNode {
10191060
@Override
10201061
BinaryComparisonNode createCmp() {
10211062
return LtNode.create();
@@ -1024,7 +1065,7 @@ BinaryComparisonNode createCmp() {
10241065

10251066
@Builtin(name = __GE__, minNumOfPositionalArgs = 2)
10261067
@GenerateNodeFactory
1027-
public abstract static class DequeGeNode extends DequeCompareNode {
1068+
public abstract static class DequeGeNode extends DequeRelCompareNode {
10281069
@Override
10291070
boolean shortcutIdentityCheck(Object self, Object other) {
10301071
return self == other;
@@ -1038,7 +1079,7 @@ BinaryComparisonNode createCmp() {
10381079

10391080
@Builtin(name = __GT__, minNumOfPositionalArgs = 2)
10401081
@GenerateNodeFactory
1041-
public abstract static class DequeGtNode extends DequeCompareNode {
1082+
public abstract static class DequeGtNode extends DequeRelCompareNode {
10421083
@Override
10431084
BinaryComparisonNode createCmp() {
10441085
return GtNode.create();

0 commit comments

Comments
 (0)