Skip to content

Commit 3d65724

Browse files
committed
Optimize GetDictIfExistsNode for native types
* Instead of having to do this optimization in callers of GetDictIfExistsNode.
1 parent a84af49 commit 3d65724

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/object/GetDictIfExistsNode.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.SystemError;
4444
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbol.FUN_PY_OBJECT_GET_DICT_PTR;
45+
import static com.oracle.graal.python.builtins.objects.cext.structs.CFields.PyTypeObject__tp_dict;
4546

4647
import com.oracle.graal.python.PythonLanguage;
4748
import com.oracle.graal.python.builtins.objects.PNone;
@@ -53,6 +54,7 @@
5354
import com.oracle.graal.python.builtins.objects.module.PythonModule;
5455
import com.oracle.graal.python.builtins.objects.object.PythonObject;
5556
import com.oracle.graal.python.builtins.objects.type.PythonManagedClass;
57+
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsTypeNode;
5658
import com.oracle.graal.python.nodes.ErrorMessages;
5759
import com.oracle.graal.python.nodes.HiddenAttr;
5860
import com.oracle.graal.python.nodes.PNodeWithContext;
@@ -125,12 +127,25 @@ static PDict doPythonObject(PythonObject object,
125127
@InliningCutoff
126128
static PDict doNativeObject(PythonAbstractNativeObject object,
127129
@Bind Node inliningTarget,
130+
@Cached IsTypeNode isTypeNode,
131+
@Cached CStructAccess.ReadObjectNode getNativeDict,
128132
@CachedLibrary(limit = "1") InteropLibrary lib,
129133
@Cached PythonToNativeNode toNative,
130134
@Cached CStructAccess.ReadObjectNode readObjectNode,
131135
@Cached CStructAccess.WriteObjectNewRefNode writeObjectNode,
132136
@Cached InlinedBranchProfile createDict,
133137
@Cached CExtNodes.PCallCapiFunction callGetDictPtr) {
138+
if (isTypeNode.execute(inliningTarget, object)) {
139+
// Optimization for native types: read at the known offset instead of calling
140+
// _PyObject_GetDictPtr()
141+
Object dict = getNativeDict.readFromObj(object, PyTypeObject__tp_dict);
142+
if (dict instanceof PDict pdict) {
143+
return pdict;
144+
} else {
145+
return null;
146+
}
147+
}
148+
134149
Object dictPtr = callGetDictPtr.call(FUN_PY_OBJECT_GET_DICT_PTR, toNative.execute(object));
135150
if (lib.isNull(dictPtr)) {
136151
return null;

0 commit comments

Comments
 (0)