Skip to content
Snippets Groups Projects
Commit 0494c211 authored by stepan's avatar stepan
Browse files

VectorIterator: reduce instance-of checks, make sure interface calls are inlined

parent 0512825a
No related branches found
No related tags found
No related merge requests found
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
*/ */
package com.oracle.truffle.r.runtime.data.nodes; package com.oracle.truffle.r.runtime.data.nodes;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.ValueType; import com.oracle.truffle.api.CompilerDirectives.ValueType;
import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.Fallback;
...@@ -45,15 +46,16 @@ import com.oracle.truffle.r.runtime.data.model.RAbstractLogicalVector; ...@@ -45,15 +46,16 @@ import com.oracle.truffle.r.runtime.data.model.RAbstractLogicalVector;
import com.oracle.truffle.r.runtime.data.model.RAbstractRawVector; import com.oracle.truffle.r.runtime.data.model.RAbstractRawVector;
import com.oracle.truffle.r.runtime.data.model.RAbstractStringVector; import com.oracle.truffle.r.runtime.data.model.RAbstractStringVector;
import com.oracle.truffle.r.runtime.data.model.RAbstractVector; import com.oracle.truffle.r.runtime.data.model.RAbstractVector;
import com.oracle.truffle.r.runtime.data.nodes.GetNextNodeGen.GetNextGenericNodeGen;
import com.oracle.truffle.r.runtime.data.nodes.VectorIterator.IteratorData; import com.oracle.truffle.r.runtime.data.nodes.VectorIterator.IteratorData;
abstract class VectorIteratorNodeAdapter extends Node { abstract class VectorIteratorNodeAdapter extends Node {
public static boolean hasNoNativeMemoryData(RAbstractVector vector) { public static boolean hasNoNativeMemoryData(RAbstractVector vector, Class<? extends RAbstractVector> vecClass) {
return !(vector instanceof RVector<?>) || !((RVector<?>) vector).hasNativeMemoryData(); return !(RVector.class.isAssignableFrom(vecClass)) || !((RVector<?>) vecClass.cast(vector)).hasNativeMemoryData();
} }
public static boolean isRVector(RAbstractVector vector) { public static boolean isRVector(Class<? extends RAbstractVector> vecClass) {
return vector instanceof RVector<?>; return RVector.class.isAssignableFrom(vecClass);
} }
} }
...@@ -65,11 +67,11 @@ abstract class GetIteratorNode extends VectorIteratorNodeAdapter { ...@@ -65,11 +67,11 @@ abstract class GetIteratorNode extends VectorIteratorNodeAdapter {
return new IteratorData<>(vector.getNativeMirror(), vector.getLength()); return new IteratorData<>(vector.getNativeMirror(), vector.getLength());
} }
@Specialization(guards = {"hasNoNativeMemoryData(vector)", "vectorClass == vector.getClass()"}) @Specialization(guards = {"vectorClass == vector.getClass()", "hasNoNativeMemoryData(vector, vectorClass)"}, limit = "10")
protected IteratorData<?> generic(RAbstractVector vector, protected IteratorData<?> generic(RAbstractVector vector,
@Cached("vector.getClass()") Class<? extends RAbstractVector> vectorClass) { @Cached("vector.getClass()") Class<? extends RAbstractVector> vectorClass) {
RAbstractVector profiledVec = vectorClass.cast(vector); RAbstractVector profiledVec = vectorClass.cast(vector);
return new IteratorData<>(profiledVec.getInternalStore(), vector.getLength()); return new IteratorData<>(profiledVec.getInternalStore(), profiledVec.getLength());
} }
@Fallback @Fallback
...@@ -106,7 +108,7 @@ abstract class HasNextNode extends VectorIteratorNodeAdapter { ...@@ -106,7 +108,7 @@ abstract class HasNextNode extends VectorIteratorNodeAdapter {
return iter.index < ((String[]) iter.store).length; return iter.index < ((String[]) iter.store).length;
} }
@Specialization(guards = {"!isRVector(vector)", "vectorClass == vector.getClass()"}) @Specialization(guards = {"vectorClass == vector.getClass()", "!isRVector(vectorClass)"}, limit = "10")
protected boolean generic(RAbstractVector vector, IteratorData<?> iter, protected boolean generic(RAbstractVector vector, IteratorData<?> iter,
@Cached("vector.getClass()") Class<? extends RAbstractVector> vectorClass) { @Cached("vector.getClass()") Class<? extends RAbstractVector> vectorClass) {
RAbstractVector profiledVec = vectorClass.cast(vector); RAbstractVector profiledVec = vectorClass.cast(vector);
...@@ -183,40 +185,62 @@ abstract class GetNextNode extends VectorIteratorNodeAdapter { ...@@ -183,40 +185,62 @@ abstract class GetNextNode extends VectorIteratorNodeAdapter {
throw RInternalError.unimplemented("string vectors backed by native memory"); throw RInternalError.unimplemented("string vectors backed by native memory");
} }
@Specialization(guards = "!isRVector(vector)") @Child private GetNextGenericNode getNextGenericNode;
protected int intVectorGeneric(RAbstractIntVector vector, IteratorData<?> iter,
@Cached("create()") GetDataAt.Int getDataAtNode) {
return getDataAtNode.get(vector, iter.store, iter.index);
}
@Specialization(guards = "!isRVector(vector)") @Fallback
protected double doubleVectorGeneric(RAbstractDoubleVector vector, IteratorData<?> iter, protected Object doGeneric(RAbstractVector vector, IteratorData<?> iter) {
@Cached("create()") GetDataAt.Double getDataAtNode) { // we use fallback and extra node so that we do not have to explicitly check that the vector is not
return getDataAtNode.get(vector, iter.store, iter.index); // RVector, DSL generates fallback guard that compares the class with the all RVector subclasses
// used in the specializations above, "vector instanceof RVector" would not be a leaf-check.
if (getNextGenericNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
getNextGenericNode = insert(GetNextGenericNode.create());
}
return getNextGenericNode.execute(vector, iter);
} }
@Specialization(guards = "!isRVector(vector)") static abstract class GetNextGenericNode extends Node {
protected String stringVectorGeneric(RAbstractStringVector vector, IteratorData<?> iter, public abstract Object execute(RAbstractVector vector, IteratorData<?> iter);
@Cached("create()") GetDataAt.String getDataAtNode) {
return getDataAtNode.get(vector, iter.store, iter.index);
}
@Specialization(guards = "!isRVector(vector)") public static GetNextGenericNode create() {
protected byte rawVectorGeneric(RAbstractRawVector vector, IteratorData<?> iter, return GetNextGenericNodeGen.create();
@Cached("create()") GetDataAt.Raw getDataAtNode) { }
return getDataAtNode.get(vector, iter.store, iter.index);
}
@Specialization(guards = "!isRVector(vector)") @Specialization
protected byte logicalVectorGeneric(RAbstractLogicalVector vector, IteratorData<?> iter, protected int intVectorGeneric(RAbstractIntVector vector, IteratorData<?> iter,
@Cached("create()") GetDataAt.Logical getDataAtNode) { @Cached("create()") GetDataAt.Int getDataAtNode) {
return getDataAtNode.get(vector, iter.store, iter.index); return getDataAtNode.get(vector, iter.store, iter.index);
} }
@Specialization(guards = "!isRVector(vector)") @Specialization
protected RComplex complexVectorGeneric(RAbstractComplexVector vector, IteratorData<?> iter, protected double doubleVectorGeneric(RAbstractDoubleVector vector, IteratorData<?> iter,
@Cached("create()") GetDataAt.Complex getDataAtNode) { @Cached("create()") GetDataAt.Double getDataAtNode) {
return getDataAtNode.get(vector, iter.store, iter.index); return getDataAtNode.get(vector, iter.store, iter.index);
}
@Specialization
protected String stringVectorGeneric(RAbstractStringVector vector, IteratorData<?> iter,
@Cached("create()") GetDataAt.String getDataAtNode) {
return getDataAtNode.get(vector, iter.store, iter.index);
}
@Specialization
protected byte rawVectorGeneric(RAbstractRawVector vector, IteratorData<?> iter,
@Cached("create()") GetDataAt.Raw getDataAtNode) {
return getDataAtNode.get(vector, iter.store, iter.index);
}
@Specialization
protected byte logicalVectorGeneric(RAbstractLogicalVector vector, IteratorData<?> iter,
@Cached("create()") GetDataAt.Logical getDataAtNode) {
return getDataAtNode.get(vector, iter.store, iter.index);
}
@Specialization
protected RComplex complexVectorGeneric(RAbstractComplexVector vector, IteratorData<?> iter,
@Cached("create()") GetDataAt.Complex getDataAtNode) {
return getDataAtNode.get(vector, iter.store, iter.index);
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment