Skip to content

Commit c6cef98

Browse files
committed
GR-10801: new HashingNodes.KeysEqualsNode used for det equality comparison
1 parent 983c9e4 commit c6cef98

File tree

4 files changed

+85
-13
lines changed

4 files changed

+85
-13
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_dict.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def test_init():
119119

120120
assert_raises(TypeError, dict.fromkeys, 10)
121121

122+
122123
def test_init1():
123124
try:
124125
dict([("a", 1), ("b", 2)], [("c", 3), ("d", 4)])
@@ -150,17 +151,22 @@ def test_init4():
150151
d = dict(pairs)
151152
assert len(d) == 100000, "invalid length, expected 100.000 but was %d".format(len(d))
152153

154+
153155
def test_init5():
154156
key_set = { 'a', 'b', 'c', 'd' }
157+
155158
class CustomMappingObject:
156159
def __init__(self, keys):
157160
self.__keys = keys
161+
158162
def keys(self):
159163
return self.__keys
164+
160165
def __getitem__(self, key):
161166
if key in self.__keys:
162167
return ord(key)
163168
raise KeyError(key)
169+
164170
def __len__(self):
165171
return len(self.keys)
166172
d = dict(CustomMappingObject(key_set))
@@ -173,8 +179,10 @@ def test_custom_key_object0():
173179
class CollisionKey:
174180
def __init__(self, val):
175181
self.val = val
182+
176183
def __hash__(self):
177184
return 1234
185+
178186
def __eq__(self, other):
179187
if type(other) == type(self):
180188
return self.val == other.val
@@ -240,6 +248,7 @@ def modifying(**kwargs):
240248
kwargs["a"] = 10
241249
kwargs["b"] = 20
242250
return kwargs
251+
243252
def reading(**kwargs):
244253
assert kwargs["a"] == 1
245254
assert kwargs["b"] == 2
@@ -258,20 +267,20 @@ class Foo:
258267
obj = Foo()
259268
d = obj.__dict__
260269
for i in range(200):
261-
attrName = "method" + str(i)
262-
d[attrName] = lambda: attrName
270+
attr_name = "method" + str(i)
271+
d[attr_name] = lambda: attr_name
263272

264273
for i in range(200):
265-
attrName = "method" + str(i)
266-
method_to_call = getattr(obj, attrName)
267-
assert method_to_call() == attrName
274+
attr_name = "method" + str(i)
275+
method_to_call = getattr(obj, attr_name)
276+
assert method_to_call() == attr_name
268277

269278

270279
def test_get_default():
271280
d = {"a": 1}
272281
assert d.get("a") == 1
273282
assert d.get("a", 2) == 1
274-
assert d.get("b") == None
283+
assert d.get("b") is None
275284
assert d.get("b", 2) == 2
276285

277286

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodesFactory.EqualsNodeGen;
6060
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodesFactory.GetItemNodeGen;
6161
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodesFactory.InitNodeGen;
62+
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodesFactory.KeysEqualsNodeGen;
6263
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodesFactory.SetItemNodeGen;
6364
import com.oracle.graal.python.builtins.objects.dict.PDict;
6465
import com.oracle.graal.python.builtins.objects.function.PKeyword;
@@ -1105,6 +1106,68 @@ public static EqualsNode create() {
11051106
}
11061107
}
11071108

1109+
public abstract static class KeysEqualsNode extends DictStorageBaseNode {
1110+
@Child private GetItemNode getRightItemNode;
1111+
1112+
public abstract boolean execute(HashingStorage selfStorage, HashingStorage other);
1113+
1114+
private GetItemNode getRightItemNode() {
1115+
if (getRightItemNode == null) {
1116+
CompilerDirectives.transferToInterpreterAndInvalidate();
1117+
getRightItemNode = insert(GetItemNode.create());
1118+
}
1119+
return getRightItemNode;
1120+
}
1121+
1122+
@Specialization(guards = "selfStorage.length() == other.length()")
1123+
boolean doKeywordsString(LocalsStorage selfStorage, LocalsStorage other) {
1124+
if (selfStorage.getFrame().getFrameDescriptor() == other.getFrame().getFrameDescriptor()) {
1125+
return doKeywordsString(selfStorage, other);
1126+
}
1127+
return false;
1128+
}
1129+
1130+
@Specialization(guards = "selfStorage.length() == other.length()")
1131+
boolean doKeywordsString(DynamicObjectStorage selfStorage, DynamicObjectStorage other) {
1132+
if (selfStorage.length() == other.length()) {
1133+
Iterable<Object> keys = selfStorage.keys();
1134+
for (Object key : keys) {
1135+
Object rightItem = getRightItemNode().execute(other, key);
1136+
if (rightItem == null) {
1137+
return false;
1138+
}
1139+
}
1140+
return true;
1141+
}
1142+
return false;
1143+
}
1144+
1145+
@Specialization(guards = "selfStorage.length() == other.length()")
1146+
boolean doKeywordsString(HashingStorage selfStorage, HashingStorage other) {
1147+
if (selfStorage.length() == other.length()) {
1148+
Iterable<Object> keys = selfStorage.keys();
1149+
for (Object key : keys) {
1150+
Object rightItem = getRightItemNode().execute(other, key);
1151+
if (rightItem == null) {
1152+
return false;
1153+
}
1154+
}
1155+
return true;
1156+
}
1157+
return false;
1158+
}
1159+
1160+
@SuppressWarnings("unused")
1161+
@Fallback
1162+
boolean doGeneric(HashingStorage selfStorage, HashingStorage other) {
1163+
return false;
1164+
}
1165+
1166+
public static KeysEqualsNode create() {
1167+
return KeysEqualsNodeGen.create();
1168+
}
1169+
}
1170+
11081171
public abstract static class DelItemNode extends DictStorageBaseNode {
11091172

11101173
public abstract boolean execute(PHashingCollection dict, HashingStorage dictStorage, Object key);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict/DictViewBuiltins.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ public abstract static class EqNode extends PythonBuiltinNode {
128128

129129
@Specialization
130130
boolean doKeysView(PDictKeysView self, PDictKeysView other,
131-
@Cached("create()") HashingStorageNodes.EqualsNode equalsNode) {
131+
@Cached("create()") HashingStorageNodes.KeysEqualsNode equalsNode) {
132132
return equalsNode.execute(self.getDict().getDictStorage(), other.getDict().getDictStorage());
133133
}
134134

135135
@Specialization
136-
boolean doItemsView(PDictItemsView self, PDictItemsView other,
137-
@Cached("create()") HashingStorageNodes.EqualsNode equalsNode) {
138-
return equalsNode.execute(self.getDict().getDictStorage(), other.getDict().getDictStorage());
136+
boolean doItemsView(PDictKeysView self, PBaseSet other,
137+
@Cached("create()") HashingStorageNodes.KeysEqualsNode equalsNode) {
138+
return equalsNode.execute(self.getDict().getDictStorage(), other.getDictStorage());
139139
}
140140

141141
@Fallback
@@ -202,13 +202,13 @@ PBaseSet doKeysView(PDictKeysView self, PDictKeysView other,
202202
public abstract static class XorNode extends PythonBuiltinNode {
203203
@Specialization
204204
PBaseSet doItemsView(PDictItemsView self, PDictItemsView other,
205-
@Cached("create()") HashingStorageNodes.ExclusiveOrNode xorNode) {
205+
@Cached("create()") HashingStorageNodes.ExclusiveOrNode xorNode) {
206206
return factory().createSet(xorNode.execute(self.getDict().getDictStorage(), other.getDict().getDictStorage()));
207207
}
208208

209209
@Specialization
210210
PBaseSet doKeysView(PDictKeysView self, PDictKeysView other,
211-
@Cached("create()") HashingStorageNodes.ExclusiveOrNode xorNode) {
211+
@Cached("create()") HashingStorageNodes.ExclusiveOrNode xorNode) {
212212
return factory().createSet(xorNode.execute(self.getDict().getDictStorage(), other.getDict().getDictStorage()));
213213
}
214214
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/set/FrozenSetBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public int len(PBaseSet self) {
8181
abstract static class EqNode extends PythonBinaryBuiltinNode {
8282
@Specialization
8383
boolean doSetSameType(PBaseSet self, PBaseSet other,
84-
@Cached("create()") HashingStorageNodes.EqualsNode equalsNode) {
84+
@Cached("create()") HashingStorageNodes.KeysEqualsNode equalsNode) {
8585
return equalsNode.execute(self.getDictStorage(), other.getDictStorage());
8686
}
8787

0 commit comments

Comments
 (0)