Skip to content

Commit 4484bb9

Browse files
committed
replace weakref.__hash__ hashfunction call and coercion with object library
1 parent f1a6724 commit 4484bb9

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/referencetype/ReferenceTypeBuiltins.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,14 @@
5353
import com.oracle.graal.python.builtins.CoreFunctions;
5454
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5555
import com.oracle.graal.python.builtins.PythonBuiltins;
56-
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.IsInstanceNode;
5756
import com.oracle.graal.python.builtins.objects.PNone;
57+
import com.oracle.graal.python.builtins.objects.function.PArguments;
58+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
5859
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
59-
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
6060
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
6161
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6262
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
6363
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
64-
import com.oracle.graal.python.nodes.util.CoerceToJavaLongNode;
6564
import com.oracle.graal.python.runtime.exception.PythonErrorType;
6665
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6766
import com.oracle.truffle.api.dsl.Cached;
@@ -70,6 +69,8 @@
7069
import com.oracle.truffle.api.dsl.NodeFactory;
7170
import com.oracle.truffle.api.dsl.Specialization;
7271
import com.oracle.truffle.api.frame.VirtualFrame;
72+
import com.oracle.truffle.api.library.CachedLibrary;
73+
import com.oracle.truffle.api.profiles.ConditionProfile;
7374

7475
@CoreFunctions(extendClasses = PythonBuiltinClassType.PReferenceType)
7576
public class ReferenceTypeBuiltins extends PythonBuiltins {
@@ -109,29 +110,19 @@ long getHash(PReferenceType self) {
109110
return self.getHash();
110111
}
111112

112-
@Specialization(guards = {
113-
"self.getObject() != null",
114-
"self.getHash() == HASH_UNSET"
115-
})
113+
@Specialization(guards = "self.getHash() == HASH_UNSET")
116114
long computeHash(VirtualFrame frame, PReferenceType self,
117-
@Cached("create(__HASH__)") LookupAndCallUnaryNode dispatchHash,
118-
@Cached IsInstanceNode isInstanceNode,
119-
@Cached("createLossy()") CoerceToJavaLongNode castToLongNode) {
115+
@Cached("createBinaryProfile()") ConditionProfile referentProfile,
116+
// n.b.: we cannot directly specialize on lib.getObject() here, because it might go away in the meantime!
117+
@CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary lib) {
120118
Object referent = self.getObject();
121-
Object hashValue = dispatchHash.executeObject(frame, referent);
122-
if (!isInstanceNode.executeWith(frame, hashValue, getBuiltinPythonClass(PythonBuiltinClassType.PInt))) {
123-
throw raise(PythonErrorType.TypeError, "__hash__ method should return an integer");
119+
if (referentProfile.profile(referent != null)) {
120+
long hash = lib.hashWithState(referent, PArguments.getThreadState(frame));
121+
self.setHash(hash);
122+
return hash;
123+
} else {
124+
throw raise(PythonErrorType.TypeError, "weak object has gone away");
124125
}
125-
126-
long hash = castToLongNode.execute(hashValue);
127-
self.setHash(hash);
128-
return hash;
129-
}
130-
131-
@Specialization(guards = {"self.getObject() == null", "self.getHash() == HASH_UNSET"})
132-
@SuppressWarnings("unused")
133-
int hashGone(VirtualFrame frame, PReferenceType self) {
134-
throw raise(PythonErrorType.TypeError, "weak object has gone away");
135126
}
136127

137128
@Fallback

0 commit comments

Comments
 (0)