Skip to content

Commit ef73bd5

Browse files
committed
removed marker field from PGroupBy and PGrouper
1 parent ab0b376 commit ef73bd5

File tree

5 files changed

+71
-123
lines changed

5 files changed

+71
-123
lines changed

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,6 @@ protected PGroupBy construct(VirtualFrame frame, Object cls, Object iterable, Ob
412412
@Cached PyObjectGetIter getIter,
413413
@SuppressWarnings("unused") @Cached IsTypeNode isTypeNode) {
414414
PGroupBy self = factory().createGroupBy(cls);
415-
Object marker = factory().createPythonObject(PythonBuiltinClassType.PythonObject);
416-
self.setMarker(marker);
417-
self.setTgtKey(marker);
418-
self.setCurrKey(marker);
419-
self.setCurrValue(marker);
420415
self.setKeyFunc(key);
421416
self.setIt(getIter.execute(frame, iterable));
422417
return self;
@@ -437,21 +432,14 @@ public abstract static class GrouperNode extends PythonTernaryBuiltinNode {
437432
@Specialization(guards = "isTypeNode.execute(cls)")
438433
protected PGrouper construct(Object cls, PGroupBy parent, Object tgtKey,
439434
@Cached IsTypeNode isTypeNode) {
440-
PGrouper self = factory().createGrouper(cls);
441-
parent.setCurrGrouper(self);
442-
self.setParent(parent);
443-
self.setTgtKey(tgtKey);
444-
self.setMarker(parent.getMarker());
445-
return self;
435+
return factory().createGrouper(parent, tgtKey);
446436
}
447437

448438
@SuppressWarnings("unused")
449439
@Specialization(guards = {"isTypeNode.execute(cls)", "!isGroupBy(parent)"})
450440
Object construct(Object cls, Object parent, Object tgtky,
451441
@Cached IsTypeNode isTypeNode) {
452-
// TODO
453-
// throw raise(TypeError, "incorrect usage of internal _grouper " + parent + " " + tgtky);
454-
return factory().createGrouper(cls);
442+
throw raise(TypeError, "incorrect usage of internal _grouper ");
455443
}
456444

457445
protected boolean isGroupBy(Object obj) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/itertools/GroupByBuiltins.java

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
4444
import static com.oracle.graal.python.nodes.ErrorMessages.IS_NOT_A;
45-
import static com.oracle.graal.python.nodes.ErrorMessages.STATE_ARGUMENT_D_MUST_BE_A_S;
4645
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ITER__;
4746
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEXT__;
4847
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REDUCE__;
@@ -97,53 +96,79 @@ Object next(VirtualFrame frame, PGroupBy self,
9796
@Cached BuiltinFunctions.NextNode nextNode,
9897
@Cached CallNode callNode,
9998
@Cached PyObjectRichCompareBool.EqNode eqNode,
99+
@Cached BranchProfile eqProfile,
100100
@Cached ConditionProfile hasFuncProfile,
101101
@Cached LoopConditionProfile loopConditionProfile) {
102102
self.setCurrGrouper(null);
103-
Object marker = self.getMarker();
104-
while (loopConditionProfile.profile(!(self.getCurrKey() != marker && (self.getTgtKey() == marker || !eqNode.execute(frame, self.getTgtKey(), self.getCurrKey()))))) {
103+
while (loopConditionProfile.profile(doGroupByStep(frame, self, eqProfile, eqNode))) {
105104
self.groupByStep(frame, nextNode, callNode, hasFuncProfile);
106105
}
107106
self.setTgtKey(self.getCurrKey());
108107
PGrouper grouper = factory().createGrouper(self, self.getTgtKey());
109108
return factory().createTuple(new Object[]{self.getCurrKey(), grouper});
110109
}
110+
111+
protected boolean doGroupByStep(VirtualFrame frame, PGroupBy self, BranchProfile eqProfile, PyObjectRichCompareBool.EqNode eqNode) {
112+
if (self.getCurrKey() == null) {
113+
return true;
114+
} else if (self.getTgtKey() == null) {
115+
return false;
116+
} else {
117+
eqProfile.enter();
118+
if (!eqNode.execute(frame, self.getTgtKey(), self.getCurrKey())) {
119+
return false;
120+
}
121+
}
122+
return true;
123+
}
111124
}
112125

113126
@Builtin(name = __REDUCE__, minNumOfPositionalArgs = 1)
114127
@GenerateNodeFactory
115128
public abstract static class ReduceNode extends PythonUnaryBuiltinNode {
116-
@Specialization(guards = "isMarkerStillSet(self)")
129+
@Specialization(guards = {"!valuesSet(self)", "isNull(self.getKeyFunc())"})
117130
Object reduce(PGroupBy self,
118131
@Cached GetClassNode getClassNode) {
132+
return reduce(self, PNone.NONE, getClassNode);
133+
}
119134

120-
Object type = getClassNode.execute(self);
121-
Object keyFunc = self.getKeyFunc() == null ? PNone.NONE : self.getKeyFunc();
135+
@Specialization(guards = {"!valuesSet(self)", "!isNull(self.getKeyFunc())"})
136+
Object reduceNoFunc(PGroupBy self,
137+
@Cached GetClassNode getClassNode) {
138+
return reduce(self, self.getKeyFunc(), getClassNode);
139+
}
122140

141+
private Object reduce(PGroupBy self, Object keyFunc, GetClassNode getClassNode) {
142+
Object type = getClassNode.execute(self);
123143
PTuple tuple = factory().createTuple(new Object[]{self.getIt(), keyFunc});
124144
return factory().createTuple(new Object[]{type, tuple});
125145
}
126146

127-
@Specialization(guards = "!isMarkerStillSet(self)")
147+
@Specialization(guards = {"valuesSet(self)", "isNull(self.getKeyFunc())"})
128148
Object reduceMarkerNotSet(PGroupBy self,
129149
@Cached GetClassNode getClassNode) {
150+
return reduceOther(self, PNone.NONE, getClassNode);
151+
}
152+
153+
@Specialization(guards = {"valuesSet(self)", "!isNull(self.getKeyFunc())"})
154+
Object reduceMarkerNotSetNoFunc(PGroupBy self,
155+
@Cached GetClassNode getClassNode) {
156+
return reduceOther(self, self.getKeyFunc(), getClassNode);
157+
}
130158

159+
private Object reduceOther(PGroupBy self, Object keyFunc, GetClassNode getClassNode) {
131160
Object type = getClassNode.execute(self);
132-
Object keyFunc = self.getKeyFunc() == null ? PNone.NONE : self.getKeyFunc();
133-
134-
Object currValue = self.getCurrValue();
135-
Object tgtKey = self.getTgtKey();
136-
Object currKey = self.getCurrKey();
137-
Object currGrouper = self.getCurrGrouper() == null ? PNone.NONE : self.getCurrGrouper();
138-
Object marker = self.getMarker();
161+
PTuple tuple1 = factory().createTuple(new Object[]{self.getIt(), keyFunc});
162+
PTuple tuple2 = factory().createTuple(new Object[]{self.getCurrValue(), self.getTgtKey(), self.getCurrKey()});
163+
return factory().createTuple(new Object[]{type, tuple1, tuple2});
164+
}
139165

140-
PTuple tuple = factory().createTuple(new Object[]{self.getIt(), keyFunc, currValue, tgtKey, currKey, currGrouper, marker});
141-
PTuple emptyTuple = factory().createTuple(new Object[]{factory().createEmptyTuple()});
142-
return factory().createTuple(new Object[]{type, emptyTuple, tuple});
166+
protected boolean valuesSet(PGroupBy self) {
167+
return self.getTgtKey() != null && self.getCurrKey() != null && self.getCurrValue() != null;
143168
}
144169

145-
protected boolean isMarkerStillSet(PGroupBy self) {
146-
return self.getCurrValue() == self.getMarker() || self.getTgtKey() == self.getMarker() || self.getCurrKey() == self.getMarker();
170+
protected boolean isNull(Object obj) {
171+
return obj == null;
147172
}
148173

149174
}
@@ -155,41 +180,21 @@ public abstract static class SetStateNode extends PythonBinaryBuiltinNode {
155180
Object setState(VirtualFrame frame, PGroupBy self, Object state,
156181
@Cached TupleBuiltins.LenNode lenNode,
157182
@Cached TupleBuiltins.GetItemNode getItemNode,
158-
@Cached BranchProfile isNotTupleProfile,
159-
@Cached BranchProfile isNotGroupByProfile) {
160-
if (!(state instanceof PTuple) || (int) lenNode.execute(frame, state) != 7) {
183+
@Cached BranchProfile isNotTupleProfile) {
184+
if (!(state instanceof PTuple) || (int) lenNode.execute(frame, state) != 3) {
161185
isNotTupleProfile.enter();
162-
throw raise(TypeError, IS_NOT_A, "state", "7-tuple");
186+
throw raise(TypeError, IS_NOT_A, "state", "3-tuple");
163187
}
164-
Object iterable = getItemNode.execute(frame, state, 0);
165-
self.setIt(iterable);
166-
167-
Object keyFunc = getItemNode.execute(frame, state, 1);
168-
self.setKeyFunc(keyFunc instanceof PNone ? null : keyFunc);
169188

170-
Object currValue = getItemNode.execute(frame, state, 2);
189+
Object currValue = getItemNode.execute(frame, state, 0);
171190
self.setCurrValue(currValue);
172191

173-
Object tgtKey = getItemNode.execute(frame, state, 3);
192+
Object tgtKey = getItemNode.execute(frame, state, 1);
174193
self.setTgtKey(tgtKey);
175194

176-
Object currKey = getItemNode.execute(frame, state, 4);
195+
Object currKey = getItemNode.execute(frame, state, 2);
177196
self.setCurrKey(currKey);
178197

179-
Object currGrouper = getItemNode.execute(frame, state, 5);
180-
if (currGrouper instanceof PNone) {
181-
self.setCurrGrouper(null);
182-
} else {
183-
if (!(currGrouper instanceof PGrouper)) {
184-
isNotGroupByProfile.enter();
185-
throw raise(TypeError, STATE_ARGUMENT_D_MUST_BE_A_S, 6, "PGrouper");
186-
}
187-
self.setCurrGrouper((PGrouper) currGrouper);
188-
}
189-
190-
Object marker = getItemNode.execute(frame, state, 6);
191-
self.setMarker(marker);
192-
193198
return PNone.NONE;
194199
}
195200
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/itertools/GrouperBuiltins.java

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@
4141
package com.oracle.graal.python.builtins.objects.itertools;
4242

4343
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.StopIteration;
44-
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
45-
import static com.oracle.graal.python.nodes.ErrorMessages.IS_NOT_A;
46-
import static com.oracle.graal.python.nodes.ErrorMessages.STATE_ARGUMENT_D_MUST_BE_A_S;
47-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETSTATE__;
44+
import static com.oracle.graal.python.nodes.BuiltinNames.ITER;
4845
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ITER__;
4946
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEXT__;
5047
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REDUCE__;
@@ -54,13 +51,13 @@
5451
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5552
import com.oracle.graal.python.builtins.PythonBuiltins;
5653
import com.oracle.graal.python.builtins.modules.BuiltinFunctions;
57-
import com.oracle.graal.python.builtins.objects.PNone;
54+
import com.oracle.graal.python.builtins.objects.module.PythonModule;
5855
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
59-
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins;
56+
import com.oracle.graal.python.lib.PyObjectGetAttr;
6057
import com.oracle.graal.python.lib.PyObjectRichCompareBool;
58+
import com.oracle.graal.python.nodes.BuiltinNames;
6159
import com.oracle.graal.python.nodes.call.CallNode;
6260
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
63-
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
6461
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
6562
import com.oracle.graal.python.nodes.object.GetClassNode;
6663
import com.oracle.truffle.api.dsl.Cached;
@@ -106,7 +103,7 @@ Object next(VirtualFrame frame, PGrouper self,
106103
currGrouperProfile.enter();
107104
throw raise(StopIteration);
108105
}
109-
if (gbo.getCurrValue() == self.getMarker()) {
106+
if (gbo.getCurrValue() == null) {
110107
currValueMarkerProfile.enter();
111108
gbo.groupByStep(frame, nextNode, callNode, hasFuncProfile);
112109
}
@@ -115,53 +112,33 @@ Object next(VirtualFrame frame, PGrouper self,
115112
throw raise(StopIteration);
116113
}
117114
Object r = gbo.getCurrValue();
118-
gbo.setCurrValue(self.getMarker());
115+
gbo.setCurrValue(null);
119116
return r;
120117
}
121118
}
122119

123120
@Builtin(name = __REDUCE__, minNumOfPositionalArgs = 1)
124121
@GenerateNodeFactory
125122
public abstract static class ReduceNode extends PythonUnaryBuiltinNode {
126-
@Specialization
123+
@Specialization(guards = "currValueIsSelf(self)")
127124
Object reduce(PGrouper self,
128125
@Cached GetClassNode getClassNode) {
129126
Object type = getClassNode.execute(self);
130-
PTuple tuple = factory().createTuple(new Object[]{self.getParent(), self.getTgtKey(), self.getMarker()});
131-
PTuple emptyTuple = factory().createTuple(new Object[]{factory().createEmptyTuple()});
132-
// TODO
133-
return factory().createTuple(new Object[]{type, emptyTuple, tuple});
127+
PTuple tuple = factory().createTuple(new Object[]{self.getParent(), self.getTgtKey()});
128+
return factory().createTuple(new Object[]{type, tuple});
134129
}
135-
}
136-
137-
@Builtin(name = __SETSTATE__, minNumOfPositionalArgs = 2)
138-
@GenerateNodeFactory
139-
public abstract static class SetStateNode extends PythonBinaryBuiltinNode {
140-
@Specialization
141-
Object setState(VirtualFrame frame, PGrouper self, Object state,
142-
@Cached TupleBuiltins.LenNode lenNode,
143-
@Cached TupleBuiltins.GetItemNode getItemNode,
144-
@Cached BranchProfile isNotTupleProfile,
145-
@Cached BranchProfile isNotGrouperProfile) {
146130

147-
if (!(state instanceof PTuple) || (int) lenNode.execute(frame, state) != 3) {
148-
isNotTupleProfile.enter();
149-
throw raise(TypeError, IS_NOT_A, "state", "3-tuple");
150-
}
151-
Object parent = getItemNode.execute(frame, state, 0);
152-
if (!(parent instanceof PGroupBy)) {
153-
isNotGrouperProfile.enter();
154-
throw raise(TypeError, STATE_ARGUMENT_D_MUST_BE_A_S, 1, "PGroupBy");
155-
}
156-
self.setParent((PGroupBy) parent);
157-
158-
Object tgtKey = getItemNode.execute(frame, state, 1);
159-
self.setTgtKey(tgtKey);
160-
161-
Object marker = getItemNode.execute(frame, state, 2);
162-
self.setMarker(marker);
131+
@Specialization(guards = "!currValueIsSelf(self)")
132+
Object reduceCurrNotSelf(VirtualFrame frame, @SuppressWarnings("unused") PGrouper self,
133+
@Cached PyObjectGetAttr getAttrNode) {
134+
PythonModule builtins = getContext().getCore().lookupBuiltinModule(BuiltinNames.BUILTINS);
135+
Object iterCallable = getAttrNode.execute(frame, builtins, ITER);
136+
// return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
137+
return factory().createTuple(new Object[]{iterCallable, factory().createTuple(new Object[]{factory().createEmptyTuple()})});
138+
}
163139

164-
return PNone.NONE;
140+
protected boolean currValueIsSelf(PGrouper self) {
141+
return self.getParent().getCurrGrouper() == self;
165142
}
166143
}
167144
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/itertools/PGroupBy.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@
5050

5151
public final class PGroupBy extends PythonBuiltinObject {
5252

53-
static final Object MARKER = new Object();
54-
55-
private Object marker;
5653
private Object tgtKey;
5754
private PGrouper currGrouper;
5855
private Object currValue;
@@ -72,14 +69,6 @@ public void setCurrGrouper(PGrouper currGrouper) {
7269
this.currGrouper = currGrouper;
7370
}
7471

75-
public Object getMarker() {
76-
return marker;
77-
}
78-
79-
public void setMarker(Object marker) {
80-
this.marker = marker;
81-
}
82-
8372
public Object getTgtKey() {
8473
return tgtKey;
8574
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/itertools/PGrouper.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public final class PGrouper extends PythonBuiltinObject {
4747

4848
private PGroupBy parent;
4949
private Object tgtKey;
50-
private Object marker;
5150

5251
public PGrouper(Object cls, Shape instanceShape) {
5352
super(cls, instanceShape);
@@ -58,7 +57,6 @@ public PGrouper(PGroupBy parent, Object tgtKey, Object cls, Shape instanceShape)
5857
this.parent = parent;
5958
this.parent.setCurrGrouper(this);
6059
this.tgtKey = tgtKey;
61-
this.marker = parent.getMarker();
6260
}
6361

6462
public PGroupBy getParent() {
@@ -76,13 +74,4 @@ public Object getTgtKey() {
7674
public void setTgtKey(Object tgtKey) {
7775
this.tgtKey = tgtKey;
7876
}
79-
80-
public Object getMarker() {
81-
return marker;
82-
}
83-
84-
public void setMarker(Object marker) {
85-
this.marker = marker;
86-
}
87-
8877
}

0 commit comments

Comments
 (0)