Skip to content

Commit cc7acd4

Browse files
committed
Refactoring of BytesNodes.FromSequnceStorageNode.
1 parent 26cf71b commit cc7acd4

File tree

3 files changed

+39
-76
lines changed

3 files changed

+39
-76
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_int.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,6 @@ def __new__(cls, value):
505505
self.assertIs(type(i), myint2)
506506
if (sys.version_info.major >= 3 and sys.version_info.minor >= 6):
507507
# It doesn't pass on old CPython
508-
print(i)
509508
self.assertEqual(i, 2)
510509

511510
class myint3(int):

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesNodes.java

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.bytes;
4242

43+
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes.FromSequenceNode;
4344
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
4445

4546
import java.util.ArrayList;
@@ -49,9 +50,7 @@
4950
import com.oracle.graal.python.builtins.objects.bytes.BytesNodesFactory.ToBytesNodeGen;
5051
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
5152
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.NormalizeIndexNode;
52-
import com.oracle.graal.python.builtins.objects.list.PList;
5353
import com.oracle.graal.python.builtins.objects.memoryview.PMemoryView;
54-
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
5554
import com.oracle.graal.python.nodes.PGuards;
5655
import com.oracle.graal.python.nodes.PNodeWithContext;
5756
import com.oracle.graal.python.nodes.SpecialMethodNames;
@@ -60,6 +59,8 @@
6059
import com.oracle.graal.python.nodes.control.GetNextNode;
6160
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
6261
import com.oracle.graal.python.runtime.exception.PException;
62+
import com.oracle.graal.python.runtime.sequence.PSequence;
63+
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
6364
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
6465
import com.oracle.truffle.api.CompilerDirectives;
6566
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -283,16 +284,13 @@ public static FindNode create() {
283284
}
284285
}
285286

286-
public abstract static class FromSequenceStorageNode extends PNodeWithContext {
287+
public static class FromSequenceStorageNode extends Node {
287288

288289
@Node.Child private SequenceStorageNodes.GetItemNode getItemNode;
289290
@Node.Child private SequenceStorageNodes.CastToByteNode castToByteNode;
290291
@Node.Child private SequenceStorageNodes.LenNode lenNode;
291292

292-
public abstract byte[] execute(SequenceStorage storage);
293-
294-
@Specialization
295-
public byte[] doIt(SequenceStorage storage) {
293+
public byte[] execute(SequenceStorage storage) {
296294
int len = getLenNode().execute(storage);
297295
byte[] bytes = new byte[len];
298296
for (int i = 0; i < len; i++) {
@@ -326,79 +324,54 @@ private SequenceStorageNodes.LenNode getLenNode() {
326324
return lenNode;
327325
}
328326

329-
public static FromSequenceStorageNode createNode() {
330-
return BytesNodesFactory.FromSequenceStorageNodeGen.create();
327+
public static FromSequenceStorageNode create() {
328+
return new FromSequenceStorageNode();
331329
}
332330
}
333331

334-
public abstract static class FromListNode extends FromSequenceStorageNode {
335-
336-
public abstract byte[] execute(PList iterator);
332+
public static class FromSequenceNode extends Node {
337333

338-
@Specialization
339-
public byte[] doIt(PList list) {
340-
return doIt(list.getSequenceStorage());
341-
}
334+
@Child private FromSequenceStorageNode fromSequenceStorageNode;
342335

343-
public static FromListNode create() {
344-
return BytesNodesFactory.FromListNodeGen.create();
345-
}
346-
}
347-
348-
public abstract static class FromTupleNode extends FromSequenceStorageNode {
349-
350-
public abstract byte[] execute(PTuple tuple);
336+
public byte[] execute(PSequence sequence) {
337+
if (fromSequenceStorageNode == null) {
338+
CompilerDirectives.transferToInterpreterAndInvalidate();
339+
fromSequenceStorageNode = insert(FromSequenceStorageNode.create());
340+
}
351341

352-
@Specialization
353-
public byte[] doIt(PTuple tuple) {
354-
return doIt(tuple.getSequenceStorage());
342+
return fromSequenceStorageNode.execute(sequence.getSequenceStorage());
355343
}
356344

357-
public static FromTupleNode create() {
358-
return BytesNodesFactory.FromTupleNodeGen.create();
345+
public static FromSequenceNode create() {
346+
return new FromSequenceNode();
359347
}
360348
}
361349

362350
public abstract static class FromIteratorNode extends PNodeWithContext {
363351

352+
@Child private SequenceStorageNodes.AppendNode appendByteNode;
353+
364354
public abstract byte[] execute(Object iterator);
365355

356+
public SequenceStorageNodes.AppendNode getAppendByteNode() {
357+
if (appendByteNode == null) {
358+
CompilerDirectives.transferToInterpreterAndInvalidate();
359+
appendByteNode = insert(SequenceStorageNodes.AppendNode.create(() -> SequenceStorageNodes.NoGeneralizationNode.create("byte must be in range(0, 256)")));
360+
}
361+
return appendByteNode;
362+
}
363+
366364
@Specialization
367365
public byte[] execute(Object iterObject,
368-
@Cached("create()") SequenceStorageNodes.CastToByteNode castToByteNode,
369366
@Cached("create()") GetNextNode getNextNode,
370367
@Cached("create()") IsBuiltinClassProfile errorProfile) {
371-
ArrayList<byte[]> parts = new ArrayList<>();
372-
int currentLenght = 16;
373-
int currentOffset = 0;
374-
byte[] currentArray = new byte[currentLenght];
375-
parts.add(currentArray);
376-
int size = 0;
368+
ByteSequenceStorage bss = new ByteSequenceStorage(16);
377369
while (true) {
378-
if (currentOffset >= currentArray.length) {
379-
// we need to create new array
380-
if (currentLenght < 65536) {
381-
currentLenght = currentLenght * 2;
382-
}
383-
currentArray = new byte[currentLenght];
384-
parts.add(currentArray);
385-
currentOffset = 0;
386-
}
387370
try {
388-
currentArray[currentOffset] = castToByteNode.execute(getNextNode.execute(iterObject));
389-
currentOffset++;
390-
size++;
371+
getAppendByteNode().execute(bss, getNextNode.execute(iterObject));
391372
} catch (PException e) {
392373
e.expectStopIteration(errorProfile);
393-
// convert parts into result array
394-
byte[] bytes = new byte[size];
395-
396-
int offset = 0;
397-
for (byte[] part : parts) {
398-
System.arraycopy(part, 0, bytes, offset, Math.min(part.length, size - offset));
399-
offset += part.length;
400-
}
401-
return bytes;
374+
return bss.getInternalByteArray();
402375
}
403376
}
404377
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,8 +1636,7 @@ public abstract static class FromBytesNode extends PythonBuiltinNode {
16361636
@Child private GetLazyClassNode getClassNode;
16371637

16381638
@Child private LookupAndCallVarargsNode constructNode;
1639-
@Child private BytesNodes.FromListNode fromListNode;
1640-
@Child private BytesNodes.FromTupleNode fromTupleNode;
1639+
@Child private BytesNodes.FromSequenceNode fromSequenceNode;
16411640
@Child private BytesNodes.FromIteratorNode fromIteratorNode;
16421641
@Child private GetIteratorNode getIteratorNode;
16431642

@@ -1653,20 +1652,12 @@ protected LazyPythonClass getClass(Object value) {
16531652
return getClassNode.execute(value);
16541653
}
16551654

1656-
protected BytesNodes.FromListNode getFromListNode() {
1657-
if (fromListNode == null) {
1655+
protected BytesNodes.FromSequenceNode getFromSequenceNode() {
1656+
if (fromSequenceNode == null) {
16581657
CompilerDirectives.transferToInterpreterAndInvalidate();
1659-
fromListNode = insert(BytesNodes.FromListNode.create());
1658+
fromSequenceNode = insert(BytesNodes.FromSequenceNode.create());
16601659
}
1661-
return fromListNode;
1662-
}
1663-
1664-
protected BytesNodes.FromTupleNode getFromTupleNode() {
1665-
if (fromTupleNode == null) {
1666-
CompilerDirectives.transferToInterpreterAndInvalidate();
1667-
fromTupleNode = insert(BytesNodes.FromTupleNode.create());
1668-
}
1669-
return fromTupleNode;
1660+
return fromSequenceNode;
16701661
}
16711662

16721663
protected BytesNodes.FromIteratorNode getFromIteratorNode() {
@@ -1784,13 +1775,13 @@ public Object fromPByteArray(PythonClass cl, PByteArray bytes, String byteorder,
17841775
// from PArray
17851776
@Specialization
17861777
public Object fromPArray(PythonClass cl, PArray array, String byteorder, Object[] args, boolean signed,
1787-
@Cached("createNode()") BytesNodes.FromSequenceStorageNode fromSequenceStorageNode) {
1778+
@Cached("create()") BytesNodes.FromSequenceStorageNode fromSequenceStorageNode) {
17881779
return compute(cl, fromSequenceStorageNode.execute(array.getSequenceStorage()), byteorder, signed);
17891780
}
17901781

17911782
@Specialization
17921783
public Object fromPArray(PythonClass cl, PArray array, String byteorder, Object[] args, PNone signed,
1793-
@Cached("createNode()") BytesNodes.FromSequenceStorageNode fromSequenceStorageNode) {
1784+
@Cached("create()") BytesNodes.FromSequenceStorageNode fromSequenceStorageNode) {
17941785
return fromPArray(cl, array, byteorder, args, false, fromSequenceStorageNode);
17951786
}
17961787

@@ -1808,7 +1799,7 @@ public Object fromPMemoryView(PythonClass cl, PMemoryView view, String byteorder
18081799
// from PList, only if it is not extended
18091800
@Specialization(guards = "cannotBeOverridden(getClass(list))")
18101801
public Object fromPList(PythonClass cl, PList list, String byteorder, Object[] args, boolean signed) {
1811-
return compute(cl, getFromListNode().execute(list), byteorder, signed);
1802+
return compute(cl, getFromSequenceNode().execute(list), byteorder, signed);
18121803
}
18131804

18141805
@Specialization(guards = "cannotBeOverridden(getClass(list))")
@@ -1819,7 +1810,7 @@ public Object fromPList(PythonClass cl, PList list, String byteorder, Object[] a
18191810
// from PTuple, only if it is not extended
18201811
@Specialization(guards = "cannotBeOverridden(getClass(tuple))")
18211812
public Object fromPTuple(PythonClass cl, PTuple tuple, String byteorder, Object[] args, boolean signed) {
1822-
return compute(cl, getFromTupleNode().execute(tuple), byteorder, signed);
1813+
return compute(cl, getFromSequenceNode().execute(tuple), byteorder, signed);
18231814
}
18241815

18251816
@Specialization(guards = "cannotBeOverridden(getClass(tuple))")

0 commit comments

Comments
 (0)