Skip to content

Commit 833eae6

Browse files
committed
implement EconomicMapStorage Equivalence hash call with the library
1 parent 0085e7d commit 833eae6

File tree

2 files changed

+7
-64
lines changed

2 files changed

+7
-64
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/HashingStorage.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@
4242

4343
import static com.oracle.graal.python.nodes.SpecialMethodNames.__EQ__;
4444
import static com.oracle.graal.python.nodes.SpecialMethodNames.__HASH__;
45-
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
4645

4746
import java.util.Iterator;
4847

4948
import com.oracle.graal.python.PythonLanguage;
49+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
5050
import com.oracle.graal.python.nodes.PNodeWithContext;
51-
import com.oracle.graal.python.nodes.PRaiseNode;
5251
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
5352
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
5453
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -163,20 +162,12 @@ public boolean isCloningAllowed() {
163162
}
164163

165164
public static class SlowPathEquivalence extends Equivalence {
166-
167-
private final HashRootNode hashRootNode = new HashRootNode();
168165
private final EqualsRootNode eqRootNode = new EqualsRootNode();
169166

170167
@Override
171168
public int hashCode(Object o) {
172-
Object result = hashRootNode.getCallTarget().call(o);
173-
if (result instanceof Integer) {
174-
return (int) result;
175-
} else if (result instanceof Long) {
176-
return ((Long) result).intValue();
177-
} else {
178-
throw PRaiseNode.getUncached().raise(TypeError, "__hash__ method should return an integer");
179-
}
169+
long hash = PythonObjectLibrary.getUncached().hash(o);
170+
return Long.hashCode(hash);
180171
}
181172

182173
@Override

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/HashingStorageNodes.java

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodesFactory.UnionNodeGen;
7575
import com.oracle.graal.python.builtins.objects.dict.PDict;
7676
import com.oracle.graal.python.builtins.objects.function.PKeyword;
77-
import com.oracle.graal.python.builtins.objects.ints.PInt;
7877
import com.oracle.graal.python.builtins.objects.object.PythonObject;
7978
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
8079
import com.oracle.graal.python.builtins.objects.set.PSet;
@@ -102,7 +101,6 @@
102101
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
103102
import com.oracle.graal.python.runtime.PythonContext;
104103
import com.oracle.graal.python.runtime.exception.PException;
105-
import com.oracle.graal.python.runtime.exception.PythonErrorType;
106104
import com.oracle.graal.python.runtime.sequence.PSequence;
107105
import com.oracle.truffle.api.Assumption;
108106
import com.oracle.truffle.api.CompilerAsserts;
@@ -125,7 +123,6 @@
125123
import com.oracle.truffle.api.nodes.Node;
126124
import com.oracle.truffle.api.nodes.NodeCost;
127125
import com.oracle.truffle.api.nodes.NodeInfo;
128-
import com.oracle.truffle.api.nodes.UnexpectedResultException;
129126
import com.oracle.truffle.api.object.DynamicObject;
130127
import com.oracle.truffle.api.object.FinalLocationException;
131128
import com.oracle.truffle.api.object.IncompatibleLocationException;
@@ -141,60 +138,15 @@ public abstract class HashingStorageNodes {
141138
private static final int MAX_STORAGES = 8;
142139

143140
public static class PythonEquivalence extends Equivalence {
144-
@Child private PRaiseNode raise;
145-
@Child private LookupAndCallUnaryNode callHashNode = LookupAndCallUnaryNode.create(__HASH__);
141+
@Child private PythonObjectLibrary library = PythonObjectLibrary.getFactory().createDispatched(3);
146142
@Child private BinaryComparisonNode callEqNode = BinaryComparisonNode.create(__EQ__, __EQ__, "==", null, null);
147143
@Child private CastToBooleanNode castToBoolean = CastToBooleanNode.createIfTrueNode();
148-
@CompilationFinal private int state = 0;
149144

150145
@Override
151146
public int hashCode(Object o) {
152-
try {
153-
if (state == 0) { // int hash
154-
return callHashNode.executeInt(null, o);
155-
} else if (state == 1) { // long hash
156-
return (int) (callHashNode.executeLong(null, o));
157-
} else if (state == 2) { // object hash
158-
Object hash = callHashNode.executeObject(null, o);
159-
if (hash instanceof Integer) {
160-
return (int) hash;
161-
} else if (hash instanceof Long) {
162-
return (int) ((long) hash);
163-
} else if (hash instanceof PInt) {
164-
return ((PInt) hash).intValue();
165-
} else {
166-
throw hashCodeTypeError();
167-
}
168-
} else {
169-
throw new IllegalStateException();
170-
}
171-
} catch (UnexpectedResultException ex) {
172-
CompilerDirectives.transferToInterpreterAndInvalidate();
173-
if (ex.getResult() instanceof Integer) {
174-
// the default state is executeInt, so when we get here, we already
175-
// switched to executeLong and now got an int again, so we cannot
176-
// specialize on either primitive type and have to switch to
177-
// the executeObject state.
178-
state = 2;
179-
return (int) ex.getResult();
180-
} else if (ex.getResult() instanceof Long) {
181-
state = 1;
182-
return (int) ((long) ex.getResult());
183-
} else if (ex.getResult() instanceof PInt) {
184-
state = 2;
185-
return ((PInt) ex.getResult()).intValue();
186-
} else {
187-
throw hashCodeTypeError();
188-
}
189-
}
190-
}
191-
192-
private PException hashCodeTypeError() {
193-
if (raise == null) {
194-
CompilerDirectives.transferToInterpreterAndInvalidate();
195-
raise = insert(PRaiseNode.create());
196-
}
197-
return raise.raise(PythonErrorType.TypeError, "__hash__ method should return an integer");
147+
long hash = library.hash(o);
148+
// Use a sufficiently bit-mixing narrowing conversion...
149+
return Long.hashCode(hash);
198150
}
199151

200152
@Override

0 commit comments

Comments
 (0)