|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2017, 2018, Oracle and/or its affiliates. |
| 2 | + * Copyright (c) 2017, 2019, Oracle and/or its affiliates. |
3 | 3 | * Copyright (c) 2013, Regents of the University of California
|
4 | 4 | *
|
5 | 5 | * All rights reserved.
|
@@ -49,20 +49,50 @@ public DictLiteralNode(ExpressionNode[] keys, ExpressionNode[] values) {
|
49 | 49 | assert keys.length == values.length;
|
50 | 50 | }
|
51 | 51 |
|
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 | + |
53 | 62 | @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; |
56 | 78 | for (int i = 0; i < values.length; i++) {
|
57 |
| - final Object key = keys[i].execute(frame); |
58 | 79 | final Object val = values[i].execute(frame);
|
59 | 80 |
|
60 | 81 | if (setItemNode == null) {
|
61 | 82 | CompilerDirectives.transferToInterpreterAndInvalidate();
|
62 | 83 | setItemNode = insert(SetItemNode.create());
|
63 | 84 | }
|
64 |
| - dictStorage = setItemNode.execute(dictStorage, key, val); |
| 85 | + |
| 86 | + storage = setItemNode.execute(storage, evalKeys.keys[i], val); |
65 | 87 | }
|
| 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); |
66 | 96 | return factory().createDict(dictStorage);
|
67 | 97 | }
|
68 | 98 | }
|
0 commit comments