Skip to content

Commit e4be725

Browse files
committed
moved IterableToArray from CombinationBuiltins to IteratorNodes
1 parent 18d603c commit e4be725

File tree

3 files changed

+79
-74
lines changed

3 files changed

+79
-74
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,34 @@
2525
*/
2626
package com.oracle.graal.python.builtins.modules;
2727

28+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.OverflowError;
29+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
30+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError;
2831
import static com.oracle.graal.python.nodes.ErrorMessages.S_MUST_BE_S;
2932
import static com.oracle.graal.python.nodes.ErrorMessages.S_EXPECTED_GOT_P;
3033
import static com.oracle.graal.python.nodes.SpecialMethodNames.__COPY__;
34+
import static com.oracle.graal.python.nodes.ErrorMessages.ARG_CANNOT_BE_NEGATIVE;
35+
import static com.oracle.graal.python.nodes.ErrorMessages.EXPECTED_INT_AS_R;
36+
import static com.oracle.graal.python.nodes.ErrorMessages.ISLICE_WRONG_ARGS;
37+
import static com.oracle.graal.python.nodes.ErrorMessages.MUST_BE_NON_NEGATIVE;
38+
import static com.oracle.graal.python.nodes.ErrorMessages.NUMBER_IS_REQUIRED;
39+
import static com.oracle.graal.python.nodes.ErrorMessages.STEP_FOR_ISLICE_MUST_BE;
40+
import static com.oracle.graal.python.nodes.ErrorMessages.S_FOR_ISLICE_MUST_BE;
41+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__FLOAT__;
42+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INDEX__;
43+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INT__;
3144

3245
import com.oracle.graal.python.annotations.ArgumentClinic;
3346
import com.oracle.graal.python.builtins.Builtin;
3447
import java.util.List;
3548

3649
import com.oracle.graal.python.builtins.CoreFunctions;
3750
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
38-
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.OverflowError;
39-
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
40-
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError;
4151
import com.oracle.graal.python.builtins.PythonBuiltins;
4252
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.IterNode;
4353
import com.oracle.graal.python.builtins.objects.PNone;
4454
import com.oracle.graal.python.builtins.objects.function.PKeyword;
45-
import com.oracle.graal.python.builtins.objects.itertools.CombinationsBuiltins;
55+
import com.oracle.graal.python.builtins.objects.iterator.IteratorNodes.ToArrayNode;
4656
import com.oracle.graal.python.builtins.objects.itertools.PAccumulate;
4757
import com.oracle.graal.python.builtins.objects.itertools.PChain;
4858
import com.oracle.graal.python.builtins.objects.itertools.PCombinations;
@@ -70,16 +80,6 @@
7080
import com.oracle.graal.python.lib.PyObjectSizeNode;
7181
import com.oracle.graal.python.lib.PyObjectTypeCheck;
7282
import com.oracle.graal.python.nodes.ErrorMessages;
73-
import static com.oracle.graal.python.nodes.ErrorMessages.ARG_CANNOT_BE_NEGATIVE;
74-
import static com.oracle.graal.python.nodes.ErrorMessages.EXPECTED_INT_AS_R;
75-
import static com.oracle.graal.python.nodes.ErrorMessages.ISLICE_WRONG_ARGS;
76-
import static com.oracle.graal.python.nodes.ErrorMessages.MUST_BE_NON_NEGATIVE;
77-
import static com.oracle.graal.python.nodes.ErrorMessages.NUMBER_IS_REQUIRED;
78-
import static com.oracle.graal.python.nodes.ErrorMessages.STEP_FOR_ISLICE_MUST_BE;
79-
import static com.oracle.graal.python.nodes.ErrorMessages.S_FOR_ISLICE_MUST_BE;
80-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__FLOAT__;
81-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INDEX__;
82-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INT__;
8383
import com.oracle.graal.python.nodes.builtins.ListNodes.FastConstructListNode;
8484
import com.oracle.graal.python.nodes.call.special.CallVarargsMethodNode;
8585
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
@@ -180,7 +180,7 @@ protected ArgumentClinicProvider getArgumentClinic() {
180180
@Specialization(guards = {"isTypeNode.execute(cls)", "r >= 0"})
181181
Object construct(VirtualFrame frame, Object cls, Object iterable, int r,
182182
@SuppressWarnings("unused") @Cached IsTypeNode isTypeNode,
183-
@Cached CombinationsBuiltins.IterableToArrayNode toArrayNode,
183+
@Cached ToArrayNode toArrayNode,
184184
@Cached LoopConditionProfile indicesLoopProfile) {
185185
PCombinations self = factory().createCombinations(cls);
186186
self.setPool(toArrayNode.execute(frame, iterable));
@@ -230,7 +230,7 @@ protected ArgumentClinicProvider getArgumentClinic() {
230230
@Specialization(guards = {"isTypeNode.execute(cls)", "r >= 0"})
231231
Object construct(VirtualFrame frame, Object cls, Object iterable, int r,
232232
@SuppressWarnings("unused") @Cached IsTypeNode isTypeNode,
233-
@Cached CombinationsBuiltins.IterableToArrayNode toArrayNode) {
233+
@Cached ToArrayNode toArrayNode) {
234234
PCombinationsWithReplacement self = factory().createCombinationsWithReplacement(cls);
235235
self.setPool(toArrayNode.execute(frame, iterable));
236236
self.setR(r);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/iterator/IteratorNodes.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,33 @@
4646

4747
import com.oracle.graal.python.builtins.objects.PNone;
4848
import com.oracle.graal.python.builtins.objects.PNotImplemented;
49+
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetSequenceStorageNode;
50+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
51+
import com.oracle.graal.python.builtins.objects.str.PString;
4952
import com.oracle.graal.python.builtins.objects.type.SpecialMethodSlot;
5053
import com.oracle.graal.python.lib.PyIndexCheckNode;
5154
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
55+
import com.oracle.graal.python.lib.PyObjectGetIter;
5256
import com.oracle.graal.python.lib.PyObjectSizeNode;
5357
import com.oracle.graal.python.nodes.ErrorMessages;
5458
import com.oracle.graal.python.nodes.PGuards;
5559
import com.oracle.graal.python.nodes.PNodeWithContext;
60+
import com.oracle.graal.python.nodes.PNodeWithRaise;
5661
import com.oracle.graal.python.nodes.PRaiseNode;
5762
import com.oracle.graal.python.nodes.SpecialMethodNames;
5863
import com.oracle.graal.python.nodes.attributes.LookupCallableSlotInMRONode;
5964
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
6065
import com.oracle.graal.python.nodes.call.special.CallUnaryMethodNode;
6166
import com.oracle.graal.python.nodes.call.special.LookupSpecialMethodSlotNode;
67+
import com.oracle.graal.python.nodes.control.GetNextNode;
6268
import com.oracle.graal.python.nodes.object.GetClassNode;
6369
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
6470
import com.oracle.graal.python.runtime.GilNode;
6571
import com.oracle.graal.python.runtime.exception.PException;
72+
import com.oracle.graal.python.runtime.sequence.PSequence;
73+
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
6674
import com.oracle.truffle.api.CompilerDirectives;
75+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6776
import com.oracle.truffle.api.dsl.Cached;
6877
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6978
import com.oracle.truffle.api.dsl.GenerateUncached;
@@ -75,6 +84,9 @@
7584
import com.oracle.truffle.api.library.CachedLibrary;
7685
import com.oracle.truffle.api.nodes.Node;
7786
import com.oracle.truffle.api.profiles.ConditionProfile;
87+
import com.oracle.truffle.api.profiles.LoopConditionProfile;
88+
import java.util.ArrayList;
89+
import java.util.List;
7890

7991
public abstract class IteratorNodes {
8092

@@ -186,4 +198,55 @@ static boolean doGeneric(Object it,
186198
return lookupAttributeNode.execute(it, SpecialMethodNames.__NEXT__) != PNone.NO_VALUE;
187199
}
188200
}
201+
202+
public abstract static class ToArrayNode extends PNodeWithRaise {
203+
public abstract Object[] execute(VirtualFrame frame, Object iterable);
204+
205+
@Specialization
206+
public static Object[] doIt(String iterable,
207+
@Cached LoopConditionProfile loopProfile) {
208+
Object[] result = new Object[iterable.length()];
209+
loopProfile.profileCounted(result.length);
210+
for (int i = 0; loopProfile.inject(i < result.length); i++) {
211+
result[i] = Character.toString(iterable.charAt(i));
212+
}
213+
return result;
214+
}
215+
216+
@Specialization
217+
public static Object[] doIt(PString iterable,
218+
@Cached LoopConditionProfile loopProfile) {
219+
return doIt(iterable.getValue(), loopProfile);
220+
}
221+
222+
@Specialization
223+
public static Object[] doIt(PSequence iterable,
224+
@Cached GetSequenceStorageNode getStorageNode,
225+
@Cached SequenceStorageNodes.ToArrayNode toArrayNode) {
226+
SequenceStorage storage = getStorageNode.execute(iterable);
227+
return toArrayNode.execute(storage);
228+
}
229+
230+
@Specialization(guards = {"!isPSequence(iterable)", "!isString(iterable)"})
231+
public static Object[] doIt(VirtualFrame frame, Object iterable,
232+
@Cached GetNextNode getNextNode,
233+
@Cached IsBuiltinClassProfile stopIterationProfile,
234+
@Cached PyObjectGetIter getIter) {
235+
Object it = getIter.execute(frame, iterable);
236+
List<Object> result = createlist();
237+
while (true) {
238+
try {
239+
result.add(getNextNode.execute(frame, it));
240+
} catch (PException e) {
241+
e.expectStopIteration(stopIterationProfile);
242+
return result.toArray(new Object[result.size()]);
243+
}
244+
}
245+
}
246+
247+
@TruffleBoundary
248+
private static List<Object> createlist() {
249+
return new ArrayList<>();
250+
}
251+
}
189252
}

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

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,18 @@
5353
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5454
import com.oracle.graal.python.builtins.PythonBuiltins;
5555
import com.oracle.graal.python.builtins.objects.PNone;
56-
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetSequenceStorageNode;
5756
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.ToArrayNode;
5857
import com.oracle.graal.python.builtins.objects.list.PList;
59-
import com.oracle.graal.python.builtins.objects.str.PString;
6058
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6159
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins;
6260
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins.GetItemNode;
63-
import com.oracle.graal.python.lib.PyObjectGetIter;
64-
import com.oracle.graal.python.lib.PyObjectSizeNode;
65-
import com.oracle.graal.python.nodes.PNodeWithRaise;
66-
import com.oracle.graal.python.nodes.control.GetNextNode;
6761
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6862
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
6963
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
7064
import com.oracle.graal.python.nodes.object.GetClassNode;
71-
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
7265
import com.oracle.graal.python.nodes.util.CastToJavaIntLossyNode;
7366
import com.oracle.graal.python.runtime.exception.PException;
7467
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
75-
import com.oracle.graal.python.runtime.sequence.PSequence;
76-
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
7768
import com.oracle.graal.python.util.PythonUtils;
7869
import com.oracle.truffle.api.CompilerAsserts;
7970
import com.oracle.truffle.api.dsl.Cached;
@@ -94,55 +85,6 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
9485
return CombinationsBuiltinsFactory.getFactories();
9586
}
9687

97-
public abstract static class IterableToArrayNode extends PNodeWithRaise {
98-
public abstract Object[] execute(VirtualFrame frame, Object iterable);
99-
100-
@Specialization
101-
public static Object[] doIt(String iterable,
102-
@Cached LoopConditionProfile loopProfile) {
103-
Object[] result = new Object[iterable.length()];
104-
loopProfile.profileCounted(result.length);
105-
for (int i = 0; loopProfile.inject(i < result.length); i++) {
106-
result[i] = Character.toString(iterable.charAt(i));
107-
}
108-
return result;
109-
}
110-
111-
@Specialization
112-
public static Object[] doIt(PString iterable,
113-
@Cached LoopConditionProfile loopProfile) {
114-
return doIt(iterable.getValue(), loopProfile);
115-
}
116-
117-
@Specialization
118-
public static Object[] doIt(PSequence iterable,
119-
@Cached GetSequenceStorageNode getStorageNode,
120-
@Cached ToArrayNode toArrayNode) {
121-
SequenceStorage storage = getStorageNode.execute(iterable);
122-
return toArrayNode.execute(storage);
123-
}
124-
125-
@Specialization(guards = {"!isPSequence(iterable)", "!isString(iterable)"})
126-
public static Object[] doIt(VirtualFrame frame, Object iterable,
127-
@Cached PyObjectSizeNode lenNode,
128-
@Cached GetNextNode getNextNode,
129-
@Cached IsBuiltinClassProfile stopIterationProfile,
130-
@Cached PyObjectGetIter getIter) {
131-
Object it = getIter.execute(frame, iterable);
132-
int len = lenNode.execute(frame, iterable);
133-
Object[] result = new Object[len];
134-
int i = 0;
135-
while (true) {
136-
try {
137-
result[i++] = getNextNode.execute(frame, it);
138-
} catch (PException e) {
139-
e.expectStopIteration(stopIterationProfile);
140-
return result;
141-
}
142-
}
143-
}
144-
}
145-
14688
@Builtin(name = __ITER__, minNumOfPositionalArgs = 1)
14789
@GenerateNodeFactory
14890
public abstract static class IterNode extends PythonUnaryBuiltinNode {

0 commit comments

Comments
 (0)