Skip to content

Commit 737c620

Browse files
committed
add a cache for the monomorphic case in GetNativeClassNode
1 parent 6100401 commit 737c620

File tree

1 file changed

+18
-3
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext

1 file changed

+18
-3
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.AsLongNodeGen;
5252
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.AsPythonObjectNodeGen;
5353
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.CextUpcallNodeGen;
54+
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.GetNativeClassNodeGen;
5455
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.ObjectUpcallNodeGen;
5556
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.ToJavaNodeGen;
5657
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.ToSulongNodeGen;
@@ -535,14 +536,28 @@ public static FromCharPointerNode create() {
535536
}
536537
}
537538

538-
public static class GetNativeClassNode extends CExtBaseNode {
539+
public abstract static class GetNativeClassNode extends CExtBaseNode {
539540

540541
@Child PCallNativeNode callGetObTypeNode;
541542
@Child ToJavaNode toJavaNode;
542543

543544
@CompilationFinal private TruffleObject func;
544545

545-
public PythonClass execute(PythonNativeObject object) {
546+
public abstract PythonClass execute(PythonNativeObject object);
547+
548+
@Specialization(guards = "object == cachedObject", limit = "1")
549+
PythonClass getNativeClassCached(@SuppressWarnings("unused") PythonNativeObject object,
550+
@SuppressWarnings("unused") @Cached("object") PythonNativeObject cachedObject,
551+
@Cached("getNativeClass(cachedObject)") PythonClass cachedClass) {
552+
// TODO: (tfel) is this really something we can do? It's so rare for this class to
553+
// change that it shouldn't be worth the effort, but in native code, anything can
554+
// happen. OTOH, CPython also has caches that can become invalid when someone just goes
555+
// and changes the ob_type of an object.
556+
return cachedClass;
557+
}
558+
559+
@Specialization
560+
PythonClass getNativeClass(PythonNativeObject object) {
546561
// do not convert wrap 'object.object' since that is really the native pointer object
547562
Object[] args = new Object[]{object.object};
548563
return (PythonClass) getToJavaNode().execute(getCallGetObTypeNode().execute(getObTypeFunction(), args));
@@ -573,7 +588,7 @@ TruffleObject getObTypeFunction() {
573588
}
574589

575590
public static GetNativeClassNode create() {
576-
return new GetNativeClassNode();
591+
return GetNativeClassNodeGen.create();
577592
}
578593
}
579594

0 commit comments

Comments
 (0)