|
42 | 42 |
|
43 | 43 | import static com.oracle.graal.python.builtins.PythonBuiltinClassType.SystemError;
|
44 | 44 | 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; |
45 | 46 |
|
46 | 47 | import com.oracle.graal.python.PythonLanguage;
|
47 | 48 | import com.oracle.graal.python.builtins.objects.PNone;
|
|
53 | 54 | import com.oracle.graal.python.builtins.objects.module.PythonModule;
|
54 | 55 | import com.oracle.graal.python.builtins.objects.object.PythonObject;
|
55 | 56 | import com.oracle.graal.python.builtins.objects.type.PythonManagedClass;
|
| 57 | +import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsTypeNode; |
56 | 58 | import com.oracle.graal.python.nodes.ErrorMessages;
|
57 | 59 | import com.oracle.graal.python.nodes.HiddenAttr;
|
58 | 60 | import com.oracle.graal.python.nodes.PNodeWithContext;
|
@@ -125,12 +127,25 @@ static PDict doPythonObject(PythonObject object,
|
125 | 127 | @InliningCutoff
|
126 | 128 | static PDict doNativeObject(PythonAbstractNativeObject object,
|
127 | 129 | @Bind Node inliningTarget,
|
| 130 | + @Cached IsTypeNode isTypeNode, |
| 131 | + @Cached CStructAccess.ReadObjectNode getNativeDict, |
128 | 132 | @CachedLibrary(limit = "1") InteropLibrary lib,
|
129 | 133 | @Cached PythonToNativeNode toNative,
|
130 | 134 | @Cached CStructAccess.ReadObjectNode readObjectNode,
|
131 | 135 | @Cached CStructAccess.WriteObjectNewRefNode writeObjectNode,
|
132 | 136 | @Cached InlinedBranchProfile createDict,
|
133 | 137 | @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 | + |
134 | 149 | Object dictPtr = callGetDictPtr.call(FUN_PY_OBJECT_GET_DICT_PTR, toNative.execute(object));
|
135 | 150 | if (lib.isNull(dictPtr)) {
|
136 | 151 | return null;
|
|
0 commit comments