Skip to content

Commit 19f473b

Browse files
committed
[GR-13916] avoid the storage switch for dict literal nodes when not all keys are strings
PullRequest: graalpython/433
2 parents 37b0397 + e52001c commit 19f473b

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/literal/DictLiteralNode.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -49,20 +49,50 @@ public DictLiteralNode(ExpressionNode[] keys, ExpressionNode[] values) {
4949
assert keys.length == values.length;
5050
}
5151

52-
@Override
52+
static final class Keys {
53+
public final Object[] keys;
54+
public final boolean allStrings;
55+
56+
Keys(Object[] keys, boolean allStrings) {
57+
this.keys = keys;
58+
this.allStrings = allStrings;
59+
}
60+
}
61+
5362
@ExplodeLoop
54-
public PDict execute(VirtualFrame frame) {
55-
HashingStorage dictStorage = PDict.createNewStorage(true, values.length);
63+
private Keys evalKeys(VirtualFrame frame) {
64+
boolean allStrings = true;
65+
Object[] evalKeys = new Object[this.keys.length];
66+
for (int i = 0; i < values.length; i++) {
67+
evalKeys[i] = keys[i].execute(frame);
68+
if (!(evalKeys[i] instanceof String)) {
69+
allStrings = false;
70+
}
71+
}
72+
return new Keys(evalKeys, allStrings);
73+
}
74+
75+
@ExplodeLoop
76+
private HashingStorage evalAndSetValues(VirtualFrame frame, HashingStorage dictStorage, Keys evalKeys) {
77+
HashingStorage storage = dictStorage;
5678
for (int i = 0; i < values.length; i++) {
57-
final Object key = keys[i].execute(frame);
5879
final Object val = values[i].execute(frame);
5980

6081
if (setItemNode == null) {
6182
CompilerDirectives.transferToInterpreterAndInvalidate();
6283
setItemNode = insert(SetItemNode.create());
6384
}
64-
dictStorage = setItemNode.execute(dictStorage, key, val);
85+
86+
storage = setItemNode.execute(storage, evalKeys.keys[i], val);
6587
}
88+
return storage;
89+
}
90+
91+
@Override
92+
public PDict execute(VirtualFrame frame) {
93+
Keys evalKeys = evalKeys(frame);
94+
HashingStorage dictStorage = PDict.createNewStorage(evalKeys.allStrings, evalKeys.keys.length);
95+
dictStorage = evalAndSetValues(frame, dictStorage, evalKeys);
6696
return factory().createDict(dictStorage);
6797
}
6898
}

0 commit comments

Comments
 (0)