Skip to content

Commit 4b44b28

Browse files
committed
Improve array initializer for byte-like objects.
1 parent d413808 commit 4b44b28

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.oracle.graal.python.builtins.objects.PNone;
3939
import com.oracle.graal.python.builtins.objects.array.PArray;
4040
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
41+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
4142
import com.oracle.graal.python.builtins.objects.range.PRange;
4243
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
4344
import com.oracle.graal.python.nodes.control.GetIteratorExpressionNode.GetIteratorNode;
@@ -152,26 +153,10 @@ PArray arrayByteInitializer(VirtualFrame frame, LazyPythonClass cls, @SuppressWa
152153

153154
@Specialization(guards = "isCharArray(typeCode)")
154155
PArray arrayCharInitializer(VirtualFrame frame, LazyPythonClass cls, @SuppressWarnings("unused") String typeCode, PSequence initializer,
155-
@Cached("createCast()") CastToByteNode castToByteNode,
156-
@Cached("create()") GetIteratorNode getIterator,
157-
@Cached("create()") GetNextNode next,
158-
@Cached("create()") IsBuiltinClassProfile errorProfile,
159-
@Cached("create()") SequenceNodes.LenNode lenNode) {
160-
Object iter = getIterator.executeWith(frame, initializer);
161-
int i = 0;
162-
byte[] byteArray = new byte[lenNode.execute(initializer)];
163-
164-
while (true) {
165-
Object nextValue;
166-
try {
167-
nextValue = next.execute(frame, iter);
168-
} catch (PException e) {
169-
e.expectStopIteration(errorProfile);
170-
break;
171-
}
172-
byteArray[i++] = castToByteNode.execute(nextValue);
173-
}
156+
@Cached SequenceNodes.GetSequenceStorageNode getSequenceStorageNode,
157+
@Cached SequenceStorageNodes.ToByteArrayNode toByteArrayNode) {
174158

159+
byte[] byteArray = toByteArrayNode.execute(getSequenceStorageNode.execute(initializer));
175160
return factory().createArray(cls, byteArray);
176161
}
177162

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
import com.oracle.truffle.api.interop.UnsupportedMessageException;
162162
import com.oracle.truffle.api.interop.UnsupportedTypeException;
163163
import com.oracle.truffle.api.library.CachedLibrary;
164+
import com.oracle.truffle.api.nodes.ExplodeLoop;
164165
import com.oracle.truffle.api.nodes.Node;
165166
import com.oracle.truffle.api.profiles.BranchProfile;
166167
import com.oracle.truffle.api.profiles.ConditionProfile;
@@ -2009,15 +2010,28 @@ byte[] doNativeByte(NativeSequenceStorage s) {
20092010
return barr;
20102011
}
20112012

2012-
@Specialization
2013+
@Specialization(guards = { "len(lenNode, s) == cachedLen", "cachedLen <= 32"})
2014+
@ExplodeLoop
2015+
byte[] doGenericLenCached(SequenceStorage s,
2016+
@Cached CastToByteNode castToByteNode,
2017+
@Cached @SuppressWarnings("unused") LenNode lenNode,
2018+
@Cached("len(lenNode, s)") int cachedLen) {
2019+
byte[] barr = new byte[cachedLen];
2020+
for (int i = 0; i < cachedLen; i++) {
2021+
barr[i] = castToByteNode.execute(getGetItemNode().execute(s, i));
2022+
}
2023+
return barr;
2024+
}
2025+
2026+
@Specialization(replaces = "doGenericLenCached")
20132027
byte[] doGeneric(SequenceStorage s,
2014-
@Cached PRaiseNode raiseNode) {
2015-
if (s instanceof ByteSequenceStorage) {
2016-
return doByteSequenceStorage((ByteSequenceStorage) s);
2017-
} else if (s instanceof NativeSequenceStorage && isByteStorage((NativeSequenceStorage) s)) {
2018-
return doNativeByte((NativeSequenceStorage) s);
2028+
@Cached CastToByteNode castToByteNode,
2029+
@Cached LenNode lenNode) {
2030+
byte[] barr = new byte[lenNode.execute(s)];
2031+
for (int i = 0; i < barr.length; i++) {
2032+
barr[i] = castToByteNode.execute(getGetItemNode().execute(s, i));
20192033
}
2020-
throw raiseNode.raise(TypeError, "expected a bytes-like object");
2034+
return barr;
20212035
}
20222036

20232037
private static byte[] exactCopy(byte[] barr, int len) {
@@ -2032,6 +2046,10 @@ protected GetItemScalarNode getGetItemNode() {
20322046
return getItemNode;
20332047
}
20342048

2049+
protected static int len(LenNode lenNode, SequenceStorage s) {
2050+
return lenNode.execute(s);
2051+
}
2052+
20352053
public static ToByteArrayNode create() {
20362054
return ToByteArrayNodeGen.create(true);
20372055
}

0 commit comments

Comments
 (0)