Skip to content

Commit 8e1672a

Browse files
committed
- do not create new storage on clear if not really necessary
- in dict.update() check changes on others only based on size
1 parent 6a92c8d commit 8e1672a

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ private static Object runNode(DynamicObjectStorage self, Object key, Object acc,
341341
@ExportMessage
342342
@TruffleBoundary
343343
public HashingStorage clear() {
344-
return new DynamicObjectStorage();
344+
store.setShapeAndResize(store.getShape(), EMPTY_SHAPE);
345+
store.updateShape();
346+
return this;
345347
}
346348

347349
@Override

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,16 +372,17 @@ static HashingStorage delItemWithStateWithSideEffect(EconomicMapStorage self, Ob
372372
static class Clear {
373373

374374
@Specialization(guards = "!hasSideEffect(self)")
375-
static HashingStorage clear(@SuppressWarnings("unused") EconomicMapStorage self) {
376-
return create();
375+
static HashingStorage clear(EconomicMapStorage self) {
376+
self.map.clear();
377+
return self;
377378
}
378379

379380
@Specialization
380381
static HashingStorage clearWithSideEffect(EconomicMapStorage self,
381382
@Exclusive @Cached LookupInheritedAttributeNode.Dynamic lookup,
382383
@Exclusive @Cached CallUnaryMethodNode callNode) {
383384
if (self.map.size() == 0) {
384-
return create();
385+
return self;
385386
}
386387
Object[] entries = new Object[self.map.size() * 2];
387388
MapCursor<DictKey, Object> cursor = self.map.getEntries();
@@ -398,7 +399,7 @@ static HashingStorage clearWithSideEffect(EconomicMapStorage self,
398399
callNode.executeObject(lookup.execute(o, __DEL__), o);
399400
}
400401
}
401-
return create();
402+
return self;
402403
}
403404

404405
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict/DictBuiltins.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,11 +574,12 @@ private HashingStorage addAll(PDict self, PDict other, GetDictStorageNode getSto
574574
HashingStorage selfStorage = getStorage.execute(self);
575575
HashingStorage otherStorage = getStorage.execute(other);
576576
HashingStorageIterator<DictEntry> itOther = libOther.entries(otherStorage).iterator();
577+
int initialSize = libOther.length(otherStorage);
577578
HashingStorage newStorage = selfStorage;
578579
while (itOther.hasNext()) {
579580
DictEntry next = itOther.next();
580581
newStorage = libSelf.setItem(selfStorage, next.key, next.value);
581-
if (otherStorage != getStorage.execute(other)) {
582+
if (initialSize != libOther.length(otherStorage)) {
582583
throw raise(RuntimeError, ErrorMessages.MUTATED_DURING_UPDATE, "dict");
583584
}
584585
}

0 commit comments

Comments
 (0)