diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Inherits.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Inherits.java index 12dee0ade14226d7f0af13f9b1af4f7ce8710e80..08360bdc8e8fb26bcd40d764d3cbbc553545ac42 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Inherits.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Inherits.java @@ -90,7 +90,7 @@ public abstract class Inherits extends RBuiltinNode { @TruffleBoundary // map operations lead to recursion resulting in compilation failure private static Object doDoesInherit(RStringVector classHr, RAbstractStringVector what) { - Map<String, Integer> classToPos = InheritsNode.initClassToPos(classHr); + HashMap<String, Integer> classToPos = InheritsNode.initClassToPos(classHr); int[] result = new int[what.getLength()]; for (int i = 0; i < what.getLength(); ++i) { final Integer pos = classToPos.get(what.getDataAt(i)); diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/InheritsNode.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/InheritsNode.java index 595032f7c687107d2f8fc6e431dcd35222edac65..1f70723480f688a8814a05a14d7b6786645e34ea 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/InheritsNode.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/InheritsNode.java @@ -16,6 +16,7 @@ import java.util.*; import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; import com.oracle.truffle.api.dsl.*; import com.oracle.truffle.api.frame.*; +import com.oracle.truffle.api.utilities.*; import com.oracle.truffle.r.nodes.binary.*; import com.oracle.truffle.r.runtime.*; import com.oracle.truffle.r.runtime.data.*; @@ -26,6 +27,9 @@ import com.oracle.truffle.r.runtime.env.*; * Basic support for "inherits" that is used by the {@code inherits} builtin and others. */ public abstract class InheritsNode extends BinaryNode { + + private final ConditionProfile sizeOneProfile = ConditionProfile.createBinaryProfile(); + public abstract byte execute(VirtualFrame frame, Object x, Object what); @SuppressWarnings("unused") @@ -40,7 +44,6 @@ public abstract class InheritsNode extends BinaryNode { return RRuntime.LOGICAL_FALSE; } - // map operations lead to recursion resulting in compilation failure @Specialization protected Object doesInherit(RAbstractVector x, RAbstractStringVector what) { return checkDoesInherit(x.getClassHierarchy(), what); @@ -51,21 +54,31 @@ public abstract class InheritsNode extends BinaryNode { return checkDoesInherit(x.getClassHierarchy(), what); } - private static byte checkDoesInherit(RStringVector classHr, RAbstractStringVector what) { - Map<String, Integer> classToPos = initClassToPos(classHr); - for (int i = 0; i < what.getLength(); ++i) { - if (classToPos.get(what.getDataAt(i)) != null) { - return RRuntime.LOGICAL_TRUE; + private byte checkDoesInherit(RStringVector classHr, RAbstractStringVector what) { + if (sizeOneProfile.profile(what.getLength() == 1)) { + String whatString = what.getDataAt(0); + for (int i = 0; i < classHr.getLength(); ++i) { + if (whatString.equals(classHr.getDataAt(i))) { + return RRuntime.LOGICAL_TRUE; + } + } + } else { + Map<String, Integer> classToPos = initClassToPos(classHr); + for (int i = 0; i < what.getLength(); ++i) { + if (classToPos.get(what.getDataAt(i)) != null) { + return RRuntime.LOGICAL_TRUE; + } } } return RRuntime.LOGICAL_FALSE; } + // map operations lead to recursion resulting in compilation failure @TruffleBoundary - public static Map<String, Integer> initClassToPos(RStringVector classHr) { + public static HashMap<String, Integer> initClassToPos(RStringVector classHr) { // Create a mapping for elements to their respective positions // in the vector for faster lookup. - Map<String, Integer> classToPos = new HashMap<>(); + HashMap<String, Integer> classToPos = new HashMap<>(); for (int i = 0; i < classHr.getLength(); ++i) { classToPos.put(classHr.getDataAt(i), i); }