Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-61853] Fix perf regression in Polyglot.castWithGenerics. #10772

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Idempotent;
import com.oracle.truffle.api.dsl.ReportPolymorphism;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.exception.AbstractTruffleException;
Expand Down Expand Up @@ -140,10 +141,12 @@ static StaticObject doGeneric(@JavaType(Class.class) StaticObject targetClass,
@Substitution
abstract static class CastWithGenerics extends SubstitutionNode {

@Idempotent
static boolean isNull(StaticObject object) {
return StaticObject.isNull(object);
}

static EspressoType getEspressoType(StaticObject targetType, Meta meta) {
if (StaticObject.isNull(targetType)) {
throw meta.throwException(meta.java_lang_NullPointerException);
}
return (EspressoType) meta.polyglot.HIDDEN_TypeLiteral_internalType.getHiddenObject(targetType);
}

Expand All @@ -159,24 +162,31 @@ protected static InstanceOf createInstanceOf(EspressoType superType) {
@JavaType(Object.class) StaticObject value,
@JavaType(internalName = "Lcom/oracle/truffle/espresso/polyglot/TypeLiteral;") StaticObject targetType);

@Specialization(guards = "getEspressoType(targetType, context.getMeta()) == cachedTargetType", limit = "2")
@Specialization(guards = "isNull(targetType)")
@JavaType(Object.class)
static StaticObject doNull(
@SuppressWarnings("unused") @JavaType(Object.class) StaticObject value,
@SuppressWarnings("unused") @JavaType(internalName = "Lcom/oracle/truffle/espresso/polyglot/TypeLiteral;") StaticObject targetType,
@SuppressWarnings("unused") @Bind Node node,
@Bind("get(node)") EspressoContext context) {
throw context.getMeta().throwException(context.getMeta().java_lang_NullPointerException);
}

@Specialization(guards = {
"!isNull(targetType)",
"getEspressoType(targetType, context.getMeta()) == cachedTargetType"}, limit = "1")
@JavaType(Object.class)
static StaticObject doCached(
@JavaType(Object.class) StaticObject value,
@JavaType(internalName = "Lcom/oracle/truffle/espresso/polyglot/TypeLiteral;") StaticObject targetType,
@Bind Node node,
@SuppressWarnings("unused") @Bind("get(node)") EspressoContext context,
@Cached InlinedBranchProfile nullTargetClassProfile,
@Cached InlinedBranchProfile reWrappingProfile,
@Cached InlinedBranchProfile errorProfile,
@SuppressWarnings("unused") @Cached("getEspressoType(targetType, context.getMeta())") EspressoType cachedTargetType,
@Cached("createInstanceOf(cachedTargetType.getRawType())") InstanceOf instanceOfTarget,
@Cached("createToEspressoNode(cachedTargetType, context.getMeta())") ToReference toEspressoNode) {
Meta meta = context.getMeta();
if (StaticObject.isNull(targetType)) {
nullTargetClassProfile.enter(node);
throw meta.throwException(meta.java_lang_NullPointerException);
}
if (StaticObject.isNull(value) || (!value.isForeignObject() && instanceOfTarget.execute(value.getKlass()))) {
return value;
}
Expand All @@ -203,7 +213,7 @@ static StaticObject doCached(
}

@ReportPolymorphism.Megamorphic
@Specialization(replaces = "doCached")
@Specialization(replaces = "doCached", guards = "!isNull(targetType)")
@JavaType(Object.class)
static StaticObject doGeneric(
@JavaType(Object.class) StaticObject value,
Expand All @@ -212,9 +222,6 @@ static StaticObject doGeneric(
@Cached InstanceOf.Dynamic instanceOfDynamic,
@Cached ToReference.DynamicToReference toEspressoNode) {
Meta meta = EspressoContext.get(node).getMeta();
if (StaticObject.isNull(targetType)) {
throw meta.throwException(meta.java_lang_NullPointerException);
}
if (StaticObject.isNull(value)) {
return value;
}
Expand Down
Loading