Skip to content

Commit 6393980

Browse files
committed
Use typed sequence storage when constructing tuples with star expressions
1 parent d00439b commit 6393980

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/literal/TupleLiteralNode.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,20 @@
2727

2828
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
2929
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.ListGeneralizationNode;
30-
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.NoGeneralizationNode;
3130
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
3231
import com.oracle.graal.python.nodes.PNode;
3332
import com.oracle.graal.python.nodes.expression.ExpressionNode;
3433
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
34+
import com.oracle.graal.python.runtime.sequence.storage.BoolSequenceStorage;
35+
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
36+
import com.oracle.graal.python.runtime.sequence.storage.DoubleSequenceStorage;
37+
import com.oracle.graal.python.runtime.sequence.storage.EmptySequenceStorage;
38+
import com.oracle.graal.python.runtime.sequence.storage.IntSequenceStorage;
39+
import com.oracle.graal.python.runtime.sequence.storage.ListSequenceStorage;
40+
import com.oracle.graal.python.runtime.sequence.storage.LongSequenceStorage;
3541
import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
3642
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
43+
import com.oracle.graal.python.runtime.sequence.storage.TupleSequenceStorage;
3744
import com.oracle.truffle.api.CompilerDirectives;
3845
import com.oracle.truffle.api.frame.VirtualFrame;
3946
import com.oracle.truffle.api.nodes.ExplodeLoop;
@@ -71,18 +78,51 @@ public Object execute(VirtualFrame frame) {
7178

7279
@ExplodeLoop
7380
private PTuple expandingTuple(VirtualFrame frame) {
81+
SequenceStorage storage;
7482
// we will usually have more than 'values.length' elements
75-
SequenceStorage storage = new ObjectSequenceStorage(values.length);
83+
switch (type) {
84+
case Uninitialized:
85+
case Empty:
86+
storage = EmptySequenceStorage.INSTANCE;
87+
break;
88+
case Boolean:
89+
storage = new BoolSequenceStorage(values.length);
90+
break;
91+
case Byte:
92+
storage = new ByteSequenceStorage(values.length);
93+
break;
94+
case Double:
95+
storage = new DoubleSequenceStorage(values.length);
96+
break;
97+
case List:
98+
storage = new ListSequenceStorage(values.length);
99+
break;
100+
case Tuple:
101+
storage = new TupleSequenceStorage(values.length);
102+
break;
103+
case Int:
104+
storage = new IntSequenceStorage(values.length);
105+
break;
106+
case Long:
107+
storage = new LongSequenceStorage(values.length);
108+
break;
109+
default:
110+
storage = new ObjectSequenceStorage(values.length);
111+
break;
112+
}
76113
for (ExpressionNode n : values) {
77114
if (n instanceof StarredExpressionNode) {
78115
SequenceStorage addElements = ((StarredExpressionNode) n).getStorage(frame);
79116
storage = ensureConcatStoragesNode().execute(storage, addElements);
80117
} else {
81118
Object element = n.execute(frame);
82-
storage = ensureAppendNode().execute(storage, element, NoGeneralizationNode.DEFAULT);
119+
storage = ensureAppendNode().execute(storage, element, ListGeneralizationNode.SUPPLIER);
83120
}
84121
}
85-
type = storage.getElementType();
122+
if (type != storage.getElementType()) {
123+
CompilerDirectives.transferToInterpreterAndInvalidate();
124+
type = storage.getElementType();
125+
}
86126
return factory.createTuple(storage);
87127
}
88128

0 commit comments

Comments
 (0)