|
40 | 40 | */
|
41 | 41 | package com.oracle.graal.python.builtins.objects.bytes;
|
42 | 42 |
|
| 43 | +import com.oracle.graal.python.builtins.objects.bytes.BytesNodes.FromSequenceNode; |
43 | 44 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
|
44 | 45 |
|
45 | 46 | import java.util.ArrayList;
|
|
49 | 50 | import com.oracle.graal.python.builtins.objects.bytes.BytesNodesFactory.ToBytesNodeGen;
|
50 | 51 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
|
51 | 52 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.NormalizeIndexNode;
|
52 |
| -import com.oracle.graal.python.builtins.objects.list.PList; |
53 | 53 | import com.oracle.graal.python.builtins.objects.memoryview.PMemoryView;
|
54 |
| -import com.oracle.graal.python.builtins.objects.tuple.PTuple; |
55 | 54 | import com.oracle.graal.python.nodes.PGuards;
|
56 | 55 | import com.oracle.graal.python.nodes.PNodeWithContext;
|
57 | 56 | import com.oracle.graal.python.nodes.SpecialMethodNames;
|
|
60 | 59 | import com.oracle.graal.python.nodes.control.GetNextNode;
|
61 | 60 | import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
|
62 | 61 | 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; |
63 | 64 | import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
|
64 | 65 | import com.oracle.truffle.api.CompilerDirectives;
|
65 | 66 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
@@ -283,16 +284,13 @@ public static FindNode create() {
|
283 | 284 | }
|
284 | 285 | }
|
285 | 286 |
|
286 |
| - public abstract static class FromSequenceStorageNode extends PNodeWithContext { |
| 287 | + public static class FromSequenceStorageNode extends Node { |
287 | 288 |
|
288 | 289 | @Node.Child private SequenceStorageNodes.GetItemNode getItemNode;
|
289 | 290 | @Node.Child private SequenceStorageNodes.CastToByteNode castToByteNode;
|
290 | 291 | @Node.Child private SequenceStorageNodes.LenNode lenNode;
|
291 | 292 |
|
292 |
| - public abstract byte[] execute(SequenceStorage storage); |
293 |
| - |
294 |
| - @Specialization |
295 |
| - public byte[] doIt(SequenceStorage storage) { |
| 293 | + public byte[] execute(SequenceStorage storage) { |
296 | 294 | int len = getLenNode().execute(storage);
|
297 | 295 | byte[] bytes = new byte[len];
|
298 | 296 | for (int i = 0; i < len; i++) {
|
@@ -326,79 +324,54 @@ private SequenceStorageNodes.LenNode getLenNode() {
|
326 | 324 | return lenNode;
|
327 | 325 | }
|
328 | 326 |
|
329 |
| - public static FromSequenceStorageNode createNode() { |
330 |
| - return BytesNodesFactory.FromSequenceStorageNodeGen.create(); |
| 327 | + public static FromSequenceStorageNode create() { |
| 328 | + return new FromSequenceStorageNode(); |
331 | 329 | }
|
332 | 330 | }
|
333 | 331 |
|
334 |
| - public abstract static class FromListNode extends FromSequenceStorageNode { |
335 |
| - |
336 |
| - public abstract byte[] execute(PList iterator); |
| 332 | + public static class FromSequenceNode extends Node { |
337 | 333 |
|
338 |
| - @Specialization |
339 |
| - public byte[] doIt(PList list) { |
340 |
| - return doIt(list.getSequenceStorage()); |
341 |
| - } |
| 334 | + @Child private FromSequenceStorageNode fromSequenceStorageNode; |
342 | 335 |
|
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 | + } |
351 | 341 |
|
352 |
| - @Specialization |
353 |
| - public byte[] doIt(PTuple tuple) { |
354 |
| - return doIt(tuple.getSequenceStorage()); |
| 342 | + return fromSequenceStorageNode.execute(sequence.getSequenceStorage()); |
355 | 343 | }
|
356 | 344 |
|
357 |
| - public static FromTupleNode create() { |
358 |
| - return BytesNodesFactory.FromTupleNodeGen.create(); |
| 345 | + public static FromSequenceNode create() { |
| 346 | + return new FromSequenceNode(); |
359 | 347 | }
|
360 | 348 | }
|
361 | 349 |
|
362 | 350 | public abstract static class FromIteratorNode extends PNodeWithContext {
|
363 | 351 |
|
| 352 | + @Child private SequenceStorageNodes.AppendNode appendByteNode; |
| 353 | + |
364 | 354 | public abstract byte[] execute(Object iterator);
|
365 | 355 |
|
| 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 | + |
366 | 364 | @Specialization
|
367 | 365 | public byte[] execute(Object iterObject,
|
368 |
| - @Cached("create()") SequenceStorageNodes.CastToByteNode castToByteNode, |
369 | 366 | @Cached("create()") GetNextNode getNextNode,
|
370 | 367 | @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); |
377 | 369 | 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 |
| - } |
387 | 370 | try {
|
388 |
| - currentArray[currentOffset] = castToByteNode.execute(getNextNode.execute(iterObject)); |
389 |
| - currentOffset++; |
390 |
| - size++; |
| 371 | + getAppendByteNode().execute(bss, getNextNode.execute(iterObject)); |
391 | 372 | } catch (PException e) {
|
392 | 373 | 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(); |
402 | 375 | }
|
403 | 376 | }
|
404 | 377 | }
|
|
0 commit comments