Skip to content

Commit 10be668

Browse files
committed
DynamicObjectStorage: cache keys in non-explode-loop variants of the messages
1 parent 9d318b9 commit 10be668

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
@ExportLibrary(HashingStorageLibrary.class)
8585
public final class DynamicObjectStorage extends HashingStorage {
8686
public static final int SIZE_THRESHOLD = 100;
87+
public static final int EXPLODE_LOOP_SIZE_LIMIT = 16;
8788

8889
final DynamicObject store;
8990
private final MroSequenceStorage mro;
@@ -136,7 +137,7 @@ private static List<Object> filter(List<Object> l) {
136137
@ExportMessage
137138
static class Length {
138139

139-
@Specialization(guards = {"cachedShape == self.store.getShape()", "keys.length < 16"}, limit = "3")
140+
@Specialization(guards = {"cachedShape == self.store.getShape()", "keys.length < EXPLODE_LOOP_SIZE_LIMIT"}, limit = "2")
140141
@ExplodeLoop
141142
static int cachedLen(DynamicObjectStorage self,
142143
@SuppressWarnings("unused") @Cached("self.store.getShape()") Shape cachedShape,
@@ -150,18 +151,25 @@ static int cachedLen(DynamicObjectStorage self,
150151
return len;
151152
}
152153

153-
@Specialization(replaces = "cachedLen")
154-
static int length(DynamicObjectStorage self,
155-
@Exclusive @Cached ReadAttributeFromDynamicObjectNode readNode) {
154+
@Specialization(replaces = "cachedLen", guards = {"cachedShape == self.store.getShape()"}, limit = "3")
155+
static int cachedKeys(DynamicObjectStorage self,
156+
@SuppressWarnings("unused") @Cached("self.store.getShape()") Shape cachedShape,
157+
@Cached(value = "keyArray(self)", dimensions = 1) Object[] keys,
158+
@Cached ReadAttributeFromDynamicObjectNode readNode) {
156159
int len = 0;
157-
Object[] keys = keyArray(self);
158160
for (int i = 0; i < keys.length; i++) {
159161
Object key = keys[i];
160162
len = incrementLen(self, readNode, len, key);
161163
}
162164
return len;
163165
}
164166

167+
@Specialization(replaces = "cachedKeys")
168+
static int length(DynamicObjectStorage self,
169+
@Exclusive @Cached ReadAttributeFromDynamicObjectNode readNode) {
170+
return cachedKeys(self, self.store.getShape(), keyArray(self), readNode);
171+
}
172+
165173
private static boolean hasStringKey(DynamicObjectStorage self, String key, ReadAttributeFromDynamicObjectNode readNode) {
166174
return readNode.execute(self.store, key) != PNone.NO_VALUE;
167175
}
@@ -197,7 +205,7 @@ static Object pstring(DynamicObjectStorage self, PString key, ThreadState state,
197205
return string(self, castStr.execute(key), state, readKey, noValueProfile);
198206
}
199207

200-
@Specialization(guards = {"cachedShape == self.store.getShape()", "keyList.length < 16", "!isBuiltinString(key, profile)"}, limit = "1")
208+
@Specialization(guards = {"cachedShape == self.store.getShape()", "keyList.length < EXPLODE_LOOP_SIZE_LIMIT", "!isBuiltinString(key, profile)"}, limit = "1")
201209
@ExplodeLoop(kind = LoopExplosionKind.FULL_UNROLL_UNTIL_RETURN)
202210
static Object notString(DynamicObjectStorage self, Object key, ThreadState state,
203211
@Shared("readKey") @Cached ReadAttributeFromDynamicObjectNode readKey,
@@ -366,12 +374,12 @@ public HashingStorage delItemWithState(Object key, ThreadState state,
366374

367375
@ExportMessage
368376
static class ForEachUntyped {
369-
@Specialization(guards = {"cachedShape == self.store.getShape()", "keys.length < 16"}, limit = "1")
377+
@Specialization(guards = {"cachedShape == self.store.getShape()", "keys.length < EXPLODE_LOOP_SIZE_LIMIT"}, limit = "2")
370378
@ExplodeLoop
371379
static Object cachedLen(DynamicObjectStorage self, ForEachNode<Object> node, Object firstValue,
372380
@Exclusive @SuppressWarnings("unused") @Cached("self.store.getShape()") Shape cachedShape,
373381
@Cached(value = "keyArray(self)", dimensions = 1) Object[] keys,
374-
@Shared("readNodeInject") @Cached ReadAttributeFromDynamicObjectNode readNode) {
382+
@Cached ReadAttributeFromDynamicObjectNode readNode) {
375383
Object result = firstValue;
376384
for (int i = 0; i < keys.length; i++) {
377385
Object key = keys[i];
@@ -380,10 +388,11 @@ static Object cachedLen(DynamicObjectStorage self, ForEachNode<Object> node, Obj
380388
return result;
381389
}
382390

383-
@Specialization(replaces = "cachedLen")
384-
static Object addAll(DynamicObjectStorage self, ForEachNode<Object> node, Object firstValue,
385-
@Shared("readNodeInject") @Cached ReadAttributeFromDynamicObjectNode readNode) {
386-
Object[] keys = keyArray(self);
391+
@Specialization(replaces = "cachedLen", guards = {"cachedShape == self.store.getShape()"}, limit = "3")
392+
static Object cachedKeys(DynamicObjectStorage self, ForEachNode<Object> node, Object firstValue,
393+
@Exclusive @SuppressWarnings("unused") @Cached("self.store.getShape()") Shape cachedShape,
394+
@Cached(value = "keyArray(self)", dimensions = 1) Object[] keys,
395+
@Cached ReadAttributeFromDynamicObjectNode readNode) {
387396
Object result = firstValue;
388397
for (int i = 0; i < keys.length; i++) {
389398
Object key = keys[i];
@@ -392,6 +401,12 @@ static Object addAll(DynamicObjectStorage self, ForEachNode<Object> node, Object
392401
return result;
393402
}
394403

404+
@Specialization(replaces = "cachedKeys")
405+
static Object addAll(DynamicObjectStorage self, ForEachNode<Object> node, Object firstValue,
406+
@Cached ReadAttributeFromDynamicObjectNode readNode) {
407+
return cachedKeys(self, node, firstValue, self.store.getShape(), keyArray(self), readNode);
408+
}
409+
395410
private static Object runNode(DynamicObjectStorage self, Object key, Object acc, ReadAttributeFromDynamicObjectNode readNode, ForEachNode<Object> node) {
396411
if (key instanceof String) {
397412
Object value = readNode.execute(self.store, key);

0 commit comments

Comments
 (0)