|
53 | 53 | import com.oracle.graal.python.builtins.CoreFunctions;
|
54 | 54 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
55 | 55 | import com.oracle.graal.python.builtins.PythonBuiltins;
|
56 |
| -import com.oracle.graal.python.builtins.modules.BuiltinFunctions.IsInstanceNode; |
57 | 56 | 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; |
58 | 59 | import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
|
59 |
| -import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode; |
60 | 60 | import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
|
61 | 61 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
|
62 | 62 | import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
|
63 | 63 | import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
|
64 |
| -import com.oracle.graal.python.nodes.util.CoerceToJavaLongNode; |
65 | 64 | import com.oracle.graal.python.runtime.exception.PythonErrorType;
|
66 | 65 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
67 | 66 | import com.oracle.truffle.api.dsl.Cached;
|
|
70 | 69 | import com.oracle.truffle.api.dsl.NodeFactory;
|
71 | 70 | import com.oracle.truffle.api.dsl.Specialization;
|
72 | 71 | import com.oracle.truffle.api.frame.VirtualFrame;
|
| 72 | +import com.oracle.truffle.api.library.CachedLibrary; |
| 73 | +import com.oracle.truffle.api.profiles.ConditionProfile; |
73 | 74 |
|
74 | 75 | @CoreFunctions(extendClasses = PythonBuiltinClassType.PReferenceType)
|
75 | 76 | public class ReferenceTypeBuiltins extends PythonBuiltins {
|
@@ -109,29 +110,19 @@ long getHash(PReferenceType self) {
|
109 | 110 | return self.getHash();
|
110 | 111 | }
|
111 | 112 |
|
112 |
| - @Specialization(guards = { |
113 |
| - "self.getObject() != null", |
114 |
| - "self.getHash() == HASH_UNSET" |
115 |
| - }) |
| 113 | + @Specialization(guards = "self.getHash() == HASH_UNSET") |
116 | 114 | 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) { |
120 | 118 | 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"); |
124 | 125 | }
|
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"); |
135 | 126 | }
|
136 | 127 |
|
137 | 128 | @Fallback
|
|
0 commit comments