Skip to content

Commit 45f10e0

Browse files
committed
Create 'tp_dict' on demand.
1 parent 14d4444 commit 45f10e0

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import com.oracle.graal.python.builtins.objects.cext.UnicodeObjectNodes.UnicodeAsWideCharNode;
8484
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage;
8585
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage.PythonObjectDictStorage;
86+
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage.PythonObjectHybridDictStorage;
8687
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
8788
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
8889
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
@@ -305,10 +306,10 @@ Object doTpName(PythonManagedClass object, @SuppressWarnings("unused") String ke
305306

306307
@Specialization(guards = "eq(TP_DOC, key)")
307308
Object doTpDoc(PythonManagedClass object, @SuppressWarnings("unused") String key,
308-
@Cached("createForceType()") ReadAttributeFromObjectNode readAttrNode,
309+
@Cached PInteropGetAttributeNode getAttrNode,
309310
@Shared("getNativeNullNode") @Cached GetNativeNullNode getNativeNullNode) {
310311
// return a C string wrapper that really allocates 'char*' on TO_NATIVE
311-
Object docObj = readAttrNode.execute(object, SpecialAttributeNames.__DOC__);
312+
Object docObj = getAttrNode.execute(object, SpecialAttributeNames.__DOC__);
312313
if (docObj instanceof String) {
313314
return new CStringWrapper((String) docObj);
314315
} else if (docObj instanceof PString) {
@@ -513,9 +514,17 @@ Object doTpRepr(PythonManagedClass object, @SuppressWarnings("unused") String ke
513514

514515
@Specialization(guards = "eq(TP_DICT, key)")
515516
Object doTpDict(PythonManagedClass object, @SuppressWarnings("unused") String key,
516-
@Cached("createForceType()") ReadAttributeFromObjectNode readAttrNode,
517+
@Cached PythonObjectFactory factory,
517518
@Shared("toSulongNode") @Cached CExtNodes.ToSulongNode toSulongNode) {
518-
return toSulongNode.execute(readAttrNode.execute(object, __DICT__));
519+
// TODO(fa): we could cache the dict instance on the class' native wrapper
520+
PHashingCollection dict = object.getDict();
521+
if (dict != null && dict.getDictStorage() instanceof PythonObjectHybridDictStorage) {
522+
// reuse the existing and modifiable storage
523+
return toSulongNode.execute(factory.createDict(dict.getDictStorage()));
524+
}
525+
PythonObjectHybridDictStorage storage = new PythonObjectHybridDictStorage(object.getStorage());
526+
object.setDict(factory.createMappingproxy(storage));
527+
return toSulongNode.execute(factory.createDict(storage));
519528
}
520529

521530
@Specialization(guards = "eq(TP_TRAVERSE, key) || eq(TP_CLEAR, key)")

0 commit comments

Comments
 (0)