Skip to content

Commit 55dde5d

Browse files
committed
Improve dynamic type MR for sequence wrappers.
1 parent f0fff4d commit 55dde5d

File tree

1 file changed

+36
-52
lines changed

1 file changed

+36
-52
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PySequenceArrayWrapperMR.java

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import com.oracle.graal.python.builtins.objects.cext.PySequenceArrayWrapperMRFactory.ToNativeArrayNodeGen;
5454
import com.oracle.graal.python.builtins.objects.cext.PySequenceArrayWrapperMRFactory.ToNativeStorageNodeGen;
5555
import com.oracle.graal.python.builtins.objects.cext.PySequenceArrayWrapperMRFactory.WriteArrayItemNodeGen;
56-
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
5756
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
5857
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.ListGeneralizationNode;
5958
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.NormalizeIndexNode;
@@ -68,7 +67,6 @@
6867
import com.oracle.graal.python.nodes.SpecialMethodNames;
6968
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
7069
import com.oracle.graal.python.nodes.call.special.LookupAndCallTernaryNode;
71-
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
7270
import com.oracle.graal.python.nodes.truffle.PythonTypes;
7371
import com.oracle.graal.python.runtime.sequence.PSequence;
7472
import com.oracle.graal.python.runtime.sequence.storage.EmptySequenceStorage;
@@ -411,82 +409,68 @@ long access(PySequenceArrayWrapper obj) {
411409
abstract static class GetTypeIDNode extends CExtBaseNode {
412410

413411
@Child private PCallNativeNode callUnaryNode = PCallNativeNode.create();
414-
@Child private SequenceNodes.LenNode lenNode;
415412

416-
@CompilationFinal TruffleObject funGetByteArrayTypeID;
417-
@CompilationFinal TruffleObject funGetPtrArrayTypeID;
413+
@CompilationFinal private TruffleObject funGetByteArrayTypeID;
414+
@CompilationFinal private TruffleObject funGetPtrArrayTypeID;
418415

419416
public abstract Object execute(Object delegate);
420417

421-
private Object callGetByteArrayTypeID(long len) {
418+
protected Object callGetByteArrayTypeID() {
419+
return callGetArrayTypeID(importCAPISymbol(NativeCAPISymbols.FUN_GET_BYTE_ARRAY_TYPE_ID));
420+
}
421+
422+
protected Object callGetPtrArrayTypeID() {
423+
return callGetArrayTypeID(importCAPISymbol(NativeCAPISymbols.FUN_GET_PTR_ARRAY_TYPE_ID));
424+
}
425+
426+
private Object callGetByteArrayTypeIDCached() {
422427
if (funGetByteArrayTypeID == null) {
423428
CompilerDirectives.transferToInterpreterAndInvalidate();
424429
funGetByteArrayTypeID = importCAPISymbol(NativeCAPISymbols.FUN_GET_BYTE_ARRAY_TYPE_ID);
425430
}
426-
return callUnaryNode.execute(funGetByteArrayTypeID, new Object[]{len});
431+
return callGetArrayTypeID(funGetByteArrayTypeID);
427432
}
428433

429-
private Object callGetPtrArrayTypeID(long len) {
434+
private Object callGetPtrArrayTypeIDCached() {
430435
if (funGetPtrArrayTypeID == null) {
431436
CompilerDirectives.transferToInterpreterAndInvalidate();
432437
funGetPtrArrayTypeID = importCAPISymbol(NativeCAPISymbols.FUN_GET_PTR_ARRAY_TYPE_ID);
433438
}
434-
return callUnaryNode.execute(funGetPtrArrayTypeID, new Object[]{len});
435-
}
436-
437-
@Specialization
438-
Object doTuple(PTuple tuple) {
439-
return callGetPtrArrayTypeID(getLength(tuple));
440-
}
441-
442-
@Specialization
443-
Object doList(PList list) {
444-
return callGetPtrArrayTypeID(getLength(list));
445-
}
446-
447-
@Specialization
448-
Object doBytes(PBytes bytes) {
449-
return callGetByteArrayTypeID(getLength(bytes));
450-
}
451-
452-
@Specialization
453-
Object doByteArray(PByteArray bytes) {
454-
return callGetByteArrayTypeID(getLength(bytes));
439+
return callGetArrayTypeID(funGetPtrArrayTypeID);
455440
}
456441

457-
@Specialization(guards = {"!isTuple(object)", "!isList(object)"})
458-
Object doGeneric(Object object,
459-
@Cached("create(__LEN__)") LookupAndCallUnaryNode getLenNode) {
460-
try {
461-
return callGetPtrArrayTypeID(getLenNode.executeInt(object));
462-
} catch (UnexpectedResultException e) {
463-
// TODO do something useful
464-
throw new AssertionError();
465-
}
442+
private Object callGetArrayTypeID(TruffleObject fun) {
443+
// We use length=0 indicating an unknown length. This allows us to reuse the type but
444+
// Sulong will report the wrong length via interop for a pointer to this object.
445+
return callUnaryNode.execute(fun, new Object[]{0});
466446
}
467447

468-
protected static ListBuiltins.GetItemNode createListGetItem() {
469-
return ListBuiltinsFactory.GetItemNodeFactory.create();
448+
@Specialization(assumptions = "singleContextAssumption()", guards = "hasByteArrayContent(object)")
449+
Object doByteArray(@SuppressWarnings("unused") PSequence object,
450+
@Cached("callGetByteArrayTypeID()") Object nativeType) {
451+
// TODO(fa): use weak reference ?
452+
return nativeType;
470453
}
471454

472-
protected static TupleBuiltins.GetItemNode createTupleGetItem() {
473-
return TupleBuiltinsFactory.GetItemNodeFactory.create();
455+
@Specialization(guards = "hasByteArrayContent(object)", replaces = "doByteArray")
456+
Object doByteArrayMultiCtx(@SuppressWarnings("unused") PSequence object) {
457+
return callGetByteArrayTypeIDCached();
474458
}
475459

476-
protected boolean isTuple(Object object) {
477-
return object instanceof PTuple;
460+
@Specialization(assumptions = "singleContextAssumption()", guards = "!hasByteArrayContent(object)")
461+
Object doPtrArray(@SuppressWarnings("unused") PSequence object,
462+
@Cached("callGetPtrArrayTypeID()") Object nativeType) {
463+
// TODO(fa): use weak reference ?
464+
return nativeType;
478465
}
479466

480-
protected boolean isList(Object object) {
481-
return object instanceof PList;
467+
@Specialization(guards = "!hasByteArrayContent(object)", replaces = "doPtrArray")
468+
Object doPtrArrayMultiCtx(@SuppressWarnings("unused") PSequence object) {
469+
return callGetPtrArrayTypeIDCached();
482470
}
483471

484-
private int getLength(PSequence s) {
485-
if (lenNode == null) {
486-
CompilerDirectives.transferToInterpreterAndInvalidate();
487-
lenNode = insert(SequenceNodes.LenNode.create());
488-
}
489-
return lenNode.execute(s);
472+
protected static boolean hasByteArrayContent(Object object) {
473+
return object instanceof PBytes || object instanceof PByteArray;
490474
}
491475

492476
public static GetTypeIDNode create() {

0 commit comments

Comments
 (0)