Skip to content

Commit e13b6a6

Browse files
committed
Take over values of existing object dict if exists.
1 parent 76a8307 commit e13b6a6

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@
8383
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage.PythonObjectDictStorage;
8484
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage.PythonObjectHybridDictStorage;
8585
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
86+
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
87+
import com.oracle.graal.python.builtins.objects.common.HashingStorage.Equivalence;
8688
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
89+
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.PythonEquivalence;
8790
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
8891
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
8992
import com.oracle.graal.python.builtins.objects.dict.PDict;
@@ -513,14 +516,20 @@ Object doTpRepr(PythonManagedClass object, @SuppressWarnings("unused") String ke
513516
@Specialization(guards = "eq(TP_DICT, key)")
514517
Object doTpDict(PythonManagedClass object, @SuppressWarnings("unused") String key,
515518
@Cached PythonObjectFactory factory,
516-
@Shared("toSulongNode") @Cached CExtNodes.ToSulongNode toSulongNode) {
519+
@Shared("toSulongNode") @Cached CExtNodes.ToSulongNode toSulongNode,
520+
@Cached(value = "createEquivalence()", uncached = "getSlowPathEquivalence()") Equivalence equivalence) {
517521
// TODO(fa): we could cache the dict instance on the class' native wrapper
518522
PHashingCollection dict = object.getDict();
519-
if (dict != null && dict.getDictStorage() instanceof PythonObjectHybridDictStorage) {
523+
HashingStorage dictStorage = dict != null ? dict.getDictStorage() : null;
524+
if (dictStorage instanceof PythonObjectHybridDictStorage) {
520525
// reuse the existing and modifiable storage
521526
return toSulongNode.execute(factory.createDict(dict.getDictStorage()));
522527
}
523528
PythonObjectHybridDictStorage storage = new PythonObjectHybridDictStorage(object.getStorage());
529+
if (dictStorage != null) {
530+
// copy all mappings to the new storage
531+
storage.addAll(dictStorage, equivalence);
532+
}
524533
object.setDict(factory.createMappingproxy(storage));
525534
return toSulongNode.execute(factory.createDict(storage));
526535
}
@@ -544,6 +553,14 @@ Object doTpTraverse(PythonManagedClass object, @SuppressWarnings("unused") Strin
544553
public static ReadTypeNativeMemberNode create() {
545554
return ReadTypeNativeMemberNodeGen.create();
546555
}
556+
557+
protected static Equivalence createEquivalence() {
558+
return PythonEquivalence.create();
559+
}
560+
561+
protected static Equivalence getSlowPathEquivalence() {
562+
return HashingStorage.getSlowPathEquivalence(null);
563+
}
547564
}
548565

549566
@GenerateUncached

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/HashingStorage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ public void addAll(HashingStorage other) {
230230
addAll(other, DEFAULT_EQIVALENCE);
231231
}
232232

233+
@TruffleBoundary
233234
public void addAll(HashingStorage other, Equivalence eq) {
234235
for (DictEntry e : other.entries()) {
235236
setItem(e.getKey(), e.getValue(), eq);

0 commit comments

Comments
 (0)