|
48 | 48 | import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary.ForEachNode;
|
49 | 49 | import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary.HashingStorageIterable;
|
50 | 50 | import com.oracle.graal.python.builtins.objects.function.PArguments.ThreadState;
|
| 51 | +import com.oracle.graal.python.builtins.objects.ints.PInt; |
51 | 52 | import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
|
52 | 53 | import com.oracle.graal.python.nodes.PGuards;
|
53 | 54 | import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
|
@@ -103,6 +104,56 @@ static boolean isSupportedKey(Object obj, IsBuiltinClassProfile isBuiltinClassPr
|
103 | 104 | return PGuards.isBuiltinString(obj, isBuiltinClassProfile);
|
104 | 105 | }
|
105 | 106 |
|
| 107 | + private static final class CustomKey { |
| 108 | + private final Object value; |
| 109 | + private final int hash; |
| 110 | + private PythonObjectLibrary lib; |
| 111 | + private final ThreadState state; |
| 112 | + |
| 113 | + private CustomKey(Object value, int hash, ThreadState state) { |
| 114 | + this.value = value; |
| 115 | + this.hash = hash; |
| 116 | + this.state = state; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public boolean equals(Object other) { |
| 121 | + if (this == other) { |
| 122 | + return true; |
| 123 | + } |
| 124 | + if (other == null) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + Object otherValue = other; |
| 128 | + PythonObjectLibrary otherLib = PythonObjectLibrary.getUncached(); |
| 129 | + if (other instanceof CustomKey) { |
| 130 | + otherValue = ((CustomKey) other).value; |
| 131 | + otherLib = ((CustomKey) other).getPythonObjLib(); |
| 132 | + if (hash != ((CustomKey) other).hash) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + } else if (hash != other.hashCode()) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + // Hopefully it will be uncommon that the object we search for will have the same hash |
| 139 | + // as some of the items in the storage (it may even equal to some of those items), so |
| 140 | + // the uncached equals call does not hurt that much here |
| 141 | + return getPythonObjLib().equalsWithState(value, otherValue, otherLib, state); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public int hashCode() { |
| 146 | + return hash; |
| 147 | + } |
| 148 | + |
| 149 | + PythonObjectLibrary getPythonObjLib() { |
| 150 | + if (lib == null) { |
| 151 | + lib = PythonObjectLibrary.getFactory().getUncached(value); |
| 152 | + } |
| 153 | + return lib; |
| 154 | + } |
| 155 | + } |
| 156 | + |
106 | 157 | @Override
|
107 | 158 | @ExportMessage
|
108 | 159 | public int length() {
|
@@ -138,8 +189,15 @@ static Object getItemNotSupportedKey(@SuppressWarnings("unused") HashMapStorage
|
138 | 189 | @SuppressWarnings("unused") @Cached IsBuiltinClassProfile profile,
|
139 | 190 | @CachedLibrary("key") PythonObjectLibrary lib,
|
140 | 191 | @Exclusive @Cached("createBinaryProfile()") ConditionProfile gotState) {
|
141 |
| - // must call __hash__ for potential side-effect |
142 |
| - getHashWithState(key, lib, state, gotState); |
| 192 | + // we must still search the map for items that may have the same hash and that may |
| 193 | + // return true from key.__eq__, we use artificial object with overridden Java level |
| 194 | + // equals and hashCode methods to perform this search |
| 195 | + long hash = getHashWithState(key, lib, state, gotState); |
| 196 | + if (PInt.isIntRange(hash)) { |
| 197 | + CustomKey keyObj = new CustomKey(key, (int) hash, state); |
| 198 | + return get(self.values, keyObj); |
| 199 | + } |
| 200 | + // else the hashes cannot possibly match |
143 | 201 | return null;
|
144 | 202 | }
|
145 | 203 | }
|
@@ -198,9 +256,20 @@ private static void remove(LinkedHashMap<Object, Object> values, Object key) {
|
198 | 256 | values.remove(key);
|
199 | 257 | }
|
200 | 258 |
|
201 |
| - @Specialization(guards = "!isSupportedKey(key, profile)") |
| 259 | + @Specialization(guards = "!isSupportedKey(key, profile)", limit = "3") |
202 | 260 | static HashingStorage delItemNonSupportedKey(HashMapStorage self, @SuppressWarnings("unused") Object key, @SuppressWarnings("unused") ThreadState state,
|
203 |
| - @SuppressWarnings("unused") @Cached IsBuiltinClassProfile profile) { |
| 261 | + @SuppressWarnings("unused") @Cached IsBuiltinClassProfile profile, |
| 262 | + @CachedLibrary("key") PythonObjectLibrary lib, |
| 263 | + @Exclusive @Cached("createBinaryProfile()") ConditionProfile gotState) { |
| 264 | + // we must still search the map for items that may have the same hash and that may |
| 265 | + // return true from key.__eq__, we use artificial object with overridden Java level |
| 266 | + // equals and hashCode methods to perform this search |
| 267 | + long hash = getHashWithState(key, lib, state, gotState); |
| 268 | + if (PInt.isIntRange(hash)) { |
| 269 | + CustomKey keyObj = new CustomKey(key, (int) hash, state); |
| 270 | + remove(self.values, keyObj); |
| 271 | + } |
| 272 | + // else the hashes cannot possibly match |
204 | 273 | return self;
|
205 | 274 | }
|
206 | 275 | }
|
|
0 commit comments