Skip to content

Commit cbe9027

Browse files
committed
fix WriteGlobalNode to properly update the dict storage, introduce a HashingCollectionNodes.SetItemNode
1 parent d41cf88 commit cbe9027

File tree

7 files changed

+50
-30
lines changed

7 files changed

+50
-30
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import com.oracle.graal.python.builtins.objects.cext.CExtNodes;
7474
import com.oracle.graal.python.builtins.objects.cext.PythonNativeClass;
7575
import com.oracle.graal.python.builtins.objects.code.PCode;
76+
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
7677
import com.oracle.graal.python.builtins.objects.common.HashingStorage.DictEntry;
7778
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
7879
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
@@ -727,7 +728,7 @@ public Object floatFromObject(@SuppressWarnings("unused") Object cls, Object arg
727728
@GenerateNodeFactory
728729
public abstract static class FrozenSetNode extends PythonBuiltinNode {
729730

730-
@Child private HashingStorageNodes.SetItemNode setItemNode;
731+
@Child private HashingCollectionNodes.SetItemNode setItemNode;
731732

732733
@Specialization(guards = "isNoValue(arg)")
733734
public PFrozenSet frozensetEmpty(PythonClass cls, @SuppressWarnings("unused") PNone arg) {
@@ -739,7 +740,7 @@ public PFrozenSet frozensetEmpty(PythonClass cls, @SuppressWarnings("unused") PN
739740
public PFrozenSet frozenset(PythonClass cls, String arg) {
740741
PFrozenSet frozenSet = factory().createFrozenSet(cls);
741742
for (int i = 0; i < arg.length(); i++) {
742-
frozenSet.setDictStorage(getSetItemNode().execute(frozenSet.getDictStorage(), String.valueOf(arg.charAt(i)), PNone.NO_VALUE));
743+
getSetItemNode().execute(frozenSet, String.valueOf(arg.charAt(i)), PNone.NO_VALUE);
743744
}
744745
return frozenSet;
745746
}
@@ -755,18 +756,18 @@ public PFrozenSet frozensetIterable(PythonClass cls, Object iterable,
755756
PFrozenSet frozenSet = factory().createFrozenSet(cls);
756757
while (true) {
757758
try {
758-
frozenSet.setDictStorage(getSetItemNode().execute(frozenSet.getDictStorage(), next.execute(iterator), PNone.NO_VALUE));
759+
getSetItemNode().execute(frozenSet, next.execute(iterator), PNone.NO_VALUE);
759760
} catch (PException e) {
760761
e.expectStopIteration(getCore(), errorProfile);
761762
return frozenSet;
762763
}
763764
}
764765
}
765766

766-
private HashingStorageNodes.SetItemNode getSetItemNode() {
767+
private HashingCollectionNodes.SetItemNode getSetItemNode() {
767768
if (setItemNode == null) {
768769
CompilerDirectives.transferToInterpreterAndInvalidate();
769-
setItemNode = insert(HashingStorageNodes.SetItemNode.create());
770+
setItemNode = insert(HashingCollectionNodes.SetItemNode.create());
770771
}
771772
return setItemNode;
772773
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ImpModuleBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
import com.oracle.graal.python.builtins.modules.TruffleCextBuiltins.CheckFunctionResultNode;
5858
import com.oracle.graal.python.builtins.objects.PNone;
5959
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.AsPythonObjectNode;
60+
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes.SetItemNode;
6061
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
61-
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.SetItemNode;
6262
import com.oracle.graal.python.builtins.objects.dict.PDict;
6363
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
6464
import com.oracle.graal.python.builtins.objects.function.PArguments;
@@ -213,7 +213,7 @@ private Object loadDynamicModuleWithSpec(String name, String path, Node readNode
213213
((PythonObject) result).setAttribute(__FILE__, path);
214214
// TODO: _PyImport_FixupExtensionObject(result, name, path, sys.modules)
215215
PDict sysModules = getContext().getSysModules();
216-
sysModules.setDictStorage(getSetItemNode().execute(sysModules.getDictStorage(), name, result));
216+
getSetItemNode().execute(sysModules, name, result);
217217
return result;
218218
}
219219
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
package com.oracle.graal.python.builtins.objects.common;
4242

4343
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodesFactory.LenNodeGen;
44+
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodesFactory.SetItemNodeGen;
4445
import com.oracle.graal.python.nodes.PGuards;
4546
import com.oracle.graal.python.nodes.PNodeWithContext;
4647
import com.oracle.truffle.api.dsl.Cached;
@@ -65,4 +66,21 @@ public static LenNode create() {
6566
return LenNodeGen.create();
6667
}
6768
}
69+
70+
@ImportStatic(PGuards.class)
71+
public abstract static class SetItemNode extends PNodeWithContext {
72+
73+
public abstract void execute(PHashingCollection c, Object key, Object value);
74+
75+
@Specialization(limit = "4", guards = {"c.getClass() == cachedClass"})
76+
void doWithStorage(PHashingCollection c, Object key, Object value,
77+
@Cached("c.getClass()") Class<? extends PHashingCollection> cachedClass,
78+
@Cached("create()") HashingStorageNodes.SetItemNode setNode) {
79+
cachedClass.cast(c).setDictStorage(setNode.execute(cachedClass.cast(c).getDictStorage(), key, value));
80+
}
81+
82+
public static SetItemNode create() {
83+
return SetItemNodeGen.create();
84+
}
85+
}
6886
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.oracle.graal.python.builtins.PythonBuiltins;
5353
import com.oracle.graal.python.builtins.objects.PNone;
5454
import com.oracle.graal.python.builtins.objects.PNotImplemented;
55+
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
5556
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
5657
import com.oracle.graal.python.builtins.objects.common.HashingStorage.DictEntry;
5758
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
@@ -137,13 +138,13 @@ public Object setDefault(PDict dict, Object key, @SuppressWarnings("unused") Obj
137138

138139
@Specialization(guards = "!containsKey(dict.getDictStorage(), key)")
139140
public Object setDefault(PDict dict, Object key, Object defaultValue,
140-
@Cached("create()") HashingStorageNodes.SetItemNode setItemNode,
141+
@Cached("create()") HashingCollectionNodes.SetItemNode setItemNode,
141142
@Cached("createBinaryProfile()") ConditionProfile defaultValProfile) {
142143
Object value = defaultValue;
143144
if (defaultValProfile.profile(defaultValue == PNone.NO_VALUE)) {
144145
value = PNone.NONE;
145146
}
146-
dict.setDictStorage(setItemNode.execute(dict.getDictStorage(), key, value));
147+
setItemNode.execute(dict, key, value);
147148
return value;
148149
}
149150
}
@@ -303,8 +304,8 @@ Object run(Object self, Object key,
303304
public abstract static class SetItemNode extends PythonTernaryBuiltinNode {
304305
@Specialization
305306
Object run(PDict self, Object key, Object value,
306-
@Cached("create()") HashingStorageNodes.SetItemNode setItemNode) {
307-
self.setDictStorage(setItemNode.execute(self.getDictStorage(), key, value));
307+
@Cached("create()") HashingCollectionNodes.SetItemNode setItemNode) {
308+
setItemNode.execute(self, key, value);
308309
return PNone.NONE;
309310
}
310311
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3737
import com.oracle.graal.python.builtins.PythonBuiltins;
3838
import com.oracle.graal.python.builtins.objects.PNone;
39+
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
3940
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
4041
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
4142
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
@@ -74,8 +75,8 @@ public abstract static class AddNode extends PythonBinaryBuiltinNode {
7475

7576
@Specialization
7677
public Object add(PSet self, Object o,
77-
@Cached("create()") HashingStorageNodes.SetItemNode setItemNode) {
78-
self.setDictStorage(setItemNode.execute(self.getDictStorage(), o, PNone.NO_VALUE));
78+
@Cached("create()") HashingCollectionNodes.SetItemNode setItemNode) {
79+
setItemNode.execute(self, o, PNone.NO_VALUE);
7980
return PNone.NONE;
8081
}
8182
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4646
import com.oracle.graal.python.builtins.objects.PNone;
47-
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.SetItemNode;
47+
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes.SetItemNode;
4848
import com.oracle.graal.python.builtins.objects.type.PythonClass;
4949
import com.oracle.graal.python.nodes.PNodeWithContext;
5050
import com.oracle.graal.python.nodes.PGuards;
@@ -79,7 +79,7 @@ public final PSet executeWith(Object value) {
7979
public PSet setString(PythonClass cls, String arg) {
8080
PSet set = factory().createSet(cls);
8181
for (int i = 0; i < arg.length(); i++) {
82-
set.setDictStorage(getSetItemNode().execute(set.getDictStorage(), String.valueOf(arg.charAt(i)), PNone.NO_VALUE));
82+
getSetItemNode().execute(set, String.valueOf(arg.charAt(i)), PNone.NO_VALUE);
8383
}
8484
return set;
8585
}
@@ -100,7 +100,7 @@ public PSet setIterable(PythonClass cls, Object iterable,
100100
Object iterator = getIterator.executeWith(iterable);
101101
while (true) {
102102
try {
103-
set.setDictStorage(getSetItemNode().execute(set.getDictStorage(), next.execute(iterator), PNone.NO_VALUE));
103+
getSetItemNode().execute(set, next.execute(iterator), PNone.NO_VALUE);
104104
} catch (PException e) {
105105
e.expectStopIteration(getCore(), errorProfile);
106106
return set;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/WriteGlobalNode.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
*/
4141
package com.oracle.graal.python.nodes.frame;
4242

43-
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
44-
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
43+
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
4544
import com.oracle.graal.python.builtins.objects.dict.PDict;
4645
import com.oracle.graal.python.builtins.objects.function.PArguments;
4746
import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
@@ -100,38 +99,38 @@ public void doWrite(VirtualFrame frame, Object value) {
10099

101100
public abstract void executeWithValue(VirtualFrame frame, Object value);
102101

103-
private static HashingStorage getDictStorage(VirtualFrame frame) {
104-
return ((PDict) PArguments.getGlobals(frame)).getDictStorage();
102+
private static PDict getGlobalsDict(VirtualFrame frame) {
103+
return (PDict) PArguments.getGlobals(frame);
105104
}
106105

107106
@Specialization(guards = "isInBuiltinDict(frame)")
108107
void writeDictBoolean(VirtualFrame frame, boolean value,
109-
@Cached("create()") HashingStorageNodes.SetItemNode storeNode) {
110-
storeNode.execute(getDictStorage(frame), attributeId, value);
108+
@Cached("create()") HashingCollectionNodes.SetItemNode storeNode) {
109+
storeNode.execute(getGlobalsDict(frame), attributeId, value);
111110
}
112111

113112
@Specialization(guards = "isInBuiltinDict(frame)")
114113
void writeDictInt(VirtualFrame frame, int value,
115-
@Cached("create()") HashingStorageNodes.SetItemNode storeNode) {
116-
storeNode.execute(getDictStorage(frame), attributeId, value);
114+
@Cached("create()") HashingCollectionNodes.SetItemNode storeNode) {
115+
storeNode.execute(getGlobalsDict(frame), attributeId, value);
117116
}
118117

119118
@Specialization(guards = "isInBuiltinDict(frame)")
120119
void writeDictLong(VirtualFrame frame, long value,
121-
@Cached("create()") HashingStorageNodes.SetItemNode storeNode) {
122-
storeNode.execute(getDictStorage(frame), attributeId, value);
120+
@Cached("create()") HashingCollectionNodes.SetItemNode storeNode) {
121+
storeNode.execute(getGlobalsDict(frame), attributeId, value);
123122
}
124123

125124
@Specialization(guards = "isInBuiltinDict(frame)")
126125
void writeDictDouble(VirtualFrame frame, double value,
127-
@Cached("create()") HashingStorageNodes.SetItemNode storeNode) {
128-
storeNode.execute(getDictStorage(frame), attributeId, value);
126+
@Cached("create()") HashingCollectionNodes.SetItemNode storeNode) {
127+
storeNode.execute(getGlobalsDict(frame), attributeId, value);
129128
}
130129

131130
@Specialization(replaces = {"writeDictBoolean", "writeDictInt", "writeDictLong", "writeDictDouble"}, guards = "isInBuiltinDict(frame)")
132131
void writeDictObject(VirtualFrame frame, Object value,
133-
@Cached("create()") HashingStorageNodes.SetItemNode storeNode) {
134-
storeNode.execute(getDictStorage(frame), attributeId, value);
132+
@Cached("create()") HashingCollectionNodes.SetItemNode storeNode) {
133+
storeNode.execute(getGlobalsDict(frame), attributeId, value);
135134
}
136135

137136
@Specialization(replaces = {"writeDictBoolean", "writeDictInt", "writeDictLong", "writeDictDouble", "writeDictObject"}, guards = "isInDict(frame)")

0 commit comments

Comments
 (0)