Skip to content

Commit cffcd0f

Browse files
committed
Use specialized SequenceStorage when constructing tuple from iterable
1 parent 89c6fca commit cffcd0f

File tree

1 file changed

+7
-37
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/builtins

1 file changed

+7
-37
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/builtins/TupleNodes.java

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -40,23 +40,18 @@
4040
*/
4141
package com.oracle.graal.python.nodes.builtins;
4242

43-
import java.util.ArrayList;
44-
4543
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4644
import com.oracle.graal.python.builtins.objects.PNone;
4745
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
4846
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
4947
import com.oracle.graal.python.nodes.PGuards;
5048
import com.oracle.graal.python.nodes.PNodeWithContext;
5149
import com.oracle.graal.python.nodes.SpecialMethodNames;
50+
import com.oracle.graal.python.nodes.builtins.ListNodes.CreateStorageFromIteratorNode;
5251
import com.oracle.graal.python.nodes.control.GetIteratorExpressionNode.GetIteratorNode;
53-
import com.oracle.graal.python.nodes.control.GetNextNode;
5452
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
55-
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
56-
import com.oracle.graal.python.runtime.exception.PException;
5753
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5854
import com.oracle.truffle.api.CompilerDirectives;
59-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6055
import com.oracle.truffle.api.dsl.Cached;
6156
import com.oracle.truffle.api.dsl.Fallback;
6257
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -107,41 +102,16 @@ PTuple tuple(@SuppressWarnings("unused") LazyPythonClass cls, PTuple iterable) {
107102

108103
@Specialization(guards = {"!isNoValue(iterable)", "createNewTuple(cls, iterable)"})
109104
PTuple tuple(VirtualFrame frame, LazyPythonClass cls, Object iterable,
110-
@Cached("create()") GetIteratorNode getIterator,
111-
@Cached("create()") GetNextNode next,
112-
@Cached("create()") IsBuiltinClassProfile errorProfile) {
113-
114-
Object iterator = getIterator.executeWith(frame, iterable);
115-
ArrayList<Object> internalStorage = makeList();
116-
while (true) {
117-
try {
118-
addToList(internalStorage, next.execute(frame, iterator));
119-
} catch (PException e) {
120-
e.expectStopIteration(errorProfile);
121-
return factory.createTuple(cls, listToArray(internalStorage));
122-
}
123-
}
124-
}
125-
126-
@TruffleBoundary
127-
private static ArrayList<Object> makeList() {
128-
return new ArrayList<>();
129-
}
130-
131-
@TruffleBoundary
132-
private static void addToList(ArrayList<Object> list, Object obj) {
133-
list.add(obj);
134-
}
135-
136-
@TruffleBoundary
137-
private static Object[] listToArray(ArrayList<Object> list) {
138-
return list.toArray();
105+
@Cached("create()") GetIteratorNode getIteratorNode,
106+
@Cached("create()") CreateStorageFromIteratorNode storageNode) {
107+
Object iterObj = getIteratorNode.executeWith(frame, iterable);
108+
return factory.createTuple(cls, storageNode.execute(frame, iterObj));
139109
}
140110

141111
@Fallback
142112
public PTuple tuple(@SuppressWarnings("unused") LazyPythonClass cls, Object value) {
143113
CompilerDirectives.transferToInterpreter();
144-
throw new RuntimeException("list does not support iterable object " + value);
114+
throw new RuntimeException("tuple does not support iterable object " + value);
145115
}
146116

147117
protected boolean createNewTuple(LazyPythonClass cls, Object iterable) {

0 commit comments

Comments
 (0)