Skip to content
Snippets Groups Projects
Commit a39615a1 authored by Lukas Stadler's avatar Lukas Stadler
Browse files

inline cache for "vector"

parent c1284c79
Branches
No related tags found
No related merge requests found
......@@ -38,7 +38,7 @@ import com.oracle.truffle.r.runtime.data.model.*;
@RBuiltin(name = "vector", kind = INTERNAL, parameterNames = {"mode", "length"})
public abstract class Vector extends RBuiltinNode {
private final ValueProfile modeProfile = ValueProfile.createIdentityProfile();
private static final String CACHED_MODES_LIMIT = "3";
private final BranchProfile errorProfile = BranchProfile.create();
@CreateCast("arguments")
......@@ -48,28 +48,53 @@ public abstract class Vector extends RBuiltinNode {
return arguments;
}
@Specialization
protected RAbstractVector vector(String mode, int length) {
controlVisibility();
switch (modeProfile.profile(mode)) {
protected RType modeToType(String mode) {
switch (mode) {
case "character":
return RDataFactory.createStringVector(length);
return RType.Character;
case "logical":
return RDataFactory.createLogicalVector(length);
return RType.Logical;
case "numeric":
case "double":
return RDataFactory.createDoubleVector(length);
return RType.Double;
case "integer":
return RDataFactory.createIntVector(length);
return RType.Integer;
case "list":
return RType.List;
case "raw":
return RType.Raw;
default:
errorProfile.enter();
throw RError.error(getEncapsulatingSourceSection(), RError.Message.CANNOT_MAKE_VECTOR_OF_MODE, mode);
}
}
@SuppressWarnings("unused")
@Specialization(guards = {"mode == cachedMode"}, limit = CACHED_MODES_LIMIT)
RAbstractVector vector(String mode, int length, @Cached("mode") String cachedMode, @Cached("modeToType(mode)") RType type) {
controlVisibility();
switch (type) {
case Character:
return RDataFactory.createStringVector(length);
case Logical:
return RDataFactory.createLogicalVector(length);
case Double:
return RDataFactory.createDoubleVector(length);
case Integer:
return RDataFactory.createIntVector(length);
case List:
Object[] data = new Object[length];
Arrays.fill(data, RNull.instance);
return RDataFactory.createList(data);
case "raw":
case Raw:
return RDataFactory.createRawVector(length);
default:
errorProfile.enter();
throw RError.error(getEncapsulatingSourceSection(), RError.Message.CANNOT_MAKE_VECTOR_OF_MODE, mode);
throw RInternalError.shouldNotReachHere();
}
}
@Specialization
protected RAbstractVector vector(String mode, int length) {
return vector(mode, length, mode, modeToType(mode));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment