|
26 | 26 | package com.oracle.graal.python.nodes.literal;
|
27 | 27 |
|
28 | 28 | import com.oracle.graal.python.PythonLanguage;
|
| 29 | +import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage; |
| 30 | +import com.oracle.graal.python.builtins.objects.common.EmptyStorage; |
29 | 31 | import com.oracle.graal.python.builtins.objects.common.HashingStorage;
|
30 | 32 | import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
|
31 | 33 | import com.oracle.graal.python.builtins.objects.dict.PDict;
|
32 | 34 | import com.oracle.graal.python.nodes.expression.ExpressionNode;
|
| 35 | +import com.oracle.graal.python.nodes.literal.DictLiteralNodeFactory.DynamicDictLiteralNodeGen; |
| 36 | +import com.oracle.graal.python.nodes.literal.DictLiteralNodeFactory.FixedDictLiteralNodeGen; |
33 | 37 | import com.oracle.graal.python.runtime.object.PythonObjectFactory;
|
| 38 | +import com.oracle.truffle.api.CompilerDirectives; |
| 39 | +import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; |
34 | 40 | import com.oracle.truffle.api.dsl.Cached;
|
35 | 41 | import com.oracle.truffle.api.dsl.CachedLanguage;
|
36 |
| -import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
37 | 42 | import com.oracle.truffle.api.dsl.Specialization;
|
38 | 43 | import com.oracle.truffle.api.frame.VirtualFrame;
|
39 |
| -import com.oracle.truffle.api.library.CachedLibrary; |
40 | 44 | import com.oracle.truffle.api.nodes.ExplodeLoop;
|
| 45 | +import com.oracle.truffle.api.object.DynamicObject; |
| 46 | +import com.oracle.truffle.api.object.DynamicObjectLibrary; |
41 | 47 | import com.oracle.truffle.api.profiles.ConditionProfile;
|
42 | 48 |
|
43 |
| -@GenerateNodeFactory |
44 |
| -public abstract class DictLiteralNode extends LiteralNode { |
45 |
| - @Child private PythonObjectFactory factory = PythonObjectFactory.create(); |
46 |
| - @Children private final ExpressionNode[] keys; |
47 |
| - @Children private final ExpressionNode[] values; |
| 49 | +public abstract class DictLiteralNode { |
48 | 50 |
|
49 |
| - protected DictLiteralNode(ExpressionNode[] keys, ExpressionNode[] values) { |
50 |
| - this.keys = keys; |
51 |
| - this.values = values; |
52 |
| - assert keys.length == values.length; |
| 51 | + abstract static class FixedDictLiteralNode extends LiteralNode { |
| 52 | + |
| 53 | + @Child private PythonObjectFactory factory = PythonObjectFactory.create(); |
| 54 | + @Children private final ExpressionNode[] values; |
| 55 | + @Children private final DynamicObjectLibrary[] libs; |
| 56 | + |
| 57 | + @CompilationFinal(dimensions = 1) private final String[] keys; |
| 58 | + |
| 59 | + protected FixedDictLiteralNode(ExpressionNode[] keys, ExpressionNode[] values) { |
| 60 | + this.keys = new String[keys.length]; |
| 61 | + for (int i = 0; i < keys.length; i++) { |
| 62 | + this.keys[i] = ((StringLiteralNode) keys[i]).getValue(); |
| 63 | + } |
| 64 | + this.values = values; |
| 65 | + this.libs = new DynamicObjectLibrary[keys.length]; |
| 66 | + } |
| 67 | + |
| 68 | + @Specialization |
| 69 | + @ExplodeLoop |
| 70 | + public PDict create(VirtualFrame frame, |
| 71 | + @CachedLanguage PythonLanguage lang) { |
| 72 | + DynamicObjectStorage dictStorage = new DynamicObjectStorage(lang); |
| 73 | + DynamicObject storage = dictStorage.getStore(); |
| 74 | + for (int i = 0; i < values.length; i++) { |
| 75 | + Object value = values[i].execute(frame); |
| 76 | + DynamicObjectLibrary lib = libs[i]; |
| 77 | + if (lib == null) { |
| 78 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 79 | + libs[i] = lib = insert(DynamicObjectLibrary.getFactory().create(storage)); |
| 80 | + } |
| 81 | + if (!lib.accepts(storage)) { |
| 82 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 83 | + libs[i] = lib = insert(DynamicObjectLibrary.getFactory().createDispatched(2)); |
| 84 | + } |
| 85 | + lib.put(storage, keys[i], value); |
| 86 | + } |
| 87 | + return factory.createDict(dictStorage); |
| 88 | + } |
53 | 89 | }
|
54 | 90 |
|
55 |
| - static final class Keys { |
56 |
| - public final Object[] keys; |
57 |
| - public final boolean allStrings; |
| 91 | + abstract static class DynamicDictLiteralNode extends LiteralNode { |
58 | 92 |
|
59 |
| - Keys(Object[] keys, boolean allStrings) { |
| 93 | + @Child private PythonObjectFactory factory = PythonObjectFactory.create(); |
| 94 | + @Children private final ExpressionNode[] keys; |
| 95 | + @Children private final ExpressionNode[] values; |
| 96 | + @Children private final HashingStorageLibrary[] libs; |
| 97 | + |
| 98 | + protected DynamicDictLiteralNode(ExpressionNode[] keys, ExpressionNode[] values) { |
60 | 99 | this.keys = keys;
|
61 |
| - this.allStrings = allStrings; |
| 100 | + this.values = values; |
| 101 | + this.libs = new HashingStorageLibrary[keys.length]; |
| 102 | + for (int i = 0; i < this.libs.length; i++) { |
| 103 | + this.libs[i] = HashingStorageLibrary.getFactory().createDispatched(2); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + static final class Keys { |
| 108 | + public final Object[] keys; |
| 109 | + public final boolean allStrings; |
| 110 | + |
| 111 | + Keys(Object[] keys, boolean allStrings) { |
| 112 | + this.keys = keys; |
| 113 | + this.allStrings = allStrings; |
| 114 | + } |
62 | 115 | }
|
63 |
| - } |
64 | 116 |
|
65 |
| - @ExplodeLoop |
66 |
| - private Keys evalKeys(VirtualFrame frame) { |
67 |
| - boolean allStrings = true; |
68 |
| - Object[] evalKeys = new Object[this.keys.length]; |
69 |
| - for (int i = 0; i < values.length; i++) { |
70 |
| - evalKeys[i] = keys[i].execute(frame); |
71 |
| - if (!(evalKeys[i] instanceof String)) { |
72 |
| - allStrings = false; |
| 117 | + @ExplodeLoop |
| 118 | + private Keys evalKeys(VirtualFrame frame) { |
| 119 | + boolean allStrings = true; |
| 120 | + Object[] evalKeys = new Object[this.keys.length]; |
| 121 | + for (int i = 0; i < values.length; i++) { |
| 122 | + evalKeys[i] = keys[i].execute(frame); |
| 123 | + if (!(evalKeys[i] instanceof String)) { |
| 124 | + allStrings = false; |
| 125 | + } |
73 | 126 | }
|
| 127 | + return new Keys(evalKeys, allStrings); |
| 128 | + } |
| 129 | + |
| 130 | + @ExplodeLoop |
| 131 | + private HashingStorage evalAndSetValues(VirtualFrame frame, HashingStorage dictStorage, Keys evalKeys, ConditionProfile hasFrame) { |
| 132 | + HashingStorage storage = dictStorage; |
| 133 | + for (int i = 0; i < values.length; i++) { |
| 134 | + Object val = values[i].execute(frame); |
| 135 | + storage = libs[i].setItemWithFrame(storage, evalKeys.keys[i], val, hasFrame, frame); |
| 136 | + } |
| 137 | + return storage; |
| 138 | + } |
| 139 | + |
| 140 | + @Specialization |
| 141 | + public PDict create(VirtualFrame frame, |
| 142 | + @CachedLanguage PythonLanguage lang, |
| 143 | + @Cached("createBinaryProfile()") ConditionProfile hasFrame) { |
| 144 | + Keys evalKeys = evalKeys(frame); |
| 145 | + HashingStorage dictStorage = PDict.createNewStorage(lang, evalKeys.allStrings, evalKeys.keys.length); |
| 146 | + dictStorage = evalAndSetValues(frame, dictStorage, evalKeys, hasFrame); |
| 147 | + return factory.createDict(dictStorage); |
74 | 148 | }
|
75 |
| - return new Keys(evalKeys, allStrings); |
76 | 149 | }
|
77 | 150 |
|
78 |
| - @ExplodeLoop |
79 |
| - private HashingStorage evalAndSetValues(VirtualFrame frame, HashingStorage dictStorage, Keys evalKeys, ConditionProfile hasFrame, HashingStorageLibrary lib) { |
80 |
| - HashingStorage storage = dictStorage; |
81 |
| - for (int i = 0; i < values.length; i++) { |
82 |
| - final Object val = values[i].execute(frame); |
83 |
| - storage = lib.setItemWithFrame(storage, evalKeys.keys[i], val, hasFrame, frame); |
| 151 | + static final class EmptyDictLiteralNode extends LiteralNode { |
| 152 | + |
| 153 | + @Child private PythonObjectFactory factory = PythonObjectFactory.create(); |
| 154 | + |
| 155 | + @Override |
| 156 | + public Object execute(VirtualFrame frame) { |
| 157 | + return factory.createDict(new EmptyStorage()); |
84 | 158 | }
|
85 |
| - return storage; |
86 | 159 | }
|
87 | 160 |
|
88 |
| - @Specialization |
89 |
| - public PDict create(VirtualFrame frame, |
90 |
| - @CachedLanguage PythonLanguage lang, |
91 |
| - @Cached("createBinaryProfile()") ConditionProfile hasFrame, |
92 |
| - @CachedLibrary(limit = "3") HashingStorageLibrary lib) { |
93 |
| - Keys evalKeys = evalKeys(frame); |
94 |
| - HashingStorage dictStorage = PDict.createNewStorage(lang, evalKeys.allStrings, evalKeys.keys.length); |
95 |
| - dictStorage = evalAndSetValues(frame, dictStorage, evalKeys, hasFrame, lib); |
96 |
| - return factory.createDict(dictStorage); |
| 161 | + public static ExpressionNode create(ExpressionNode[] keys, ExpressionNode[] values) { |
| 162 | + assert keys.length == values.length; |
| 163 | + if (keys.length == 0) { |
| 164 | + return new EmptyDictLiteralNode(); |
| 165 | + } |
| 166 | + if (keys.length > DynamicObjectStorage.SIZE_THRESHOLD) { |
| 167 | + return DynamicDictLiteralNodeGen.create(keys, values); |
| 168 | + } |
| 169 | + for (ExpressionNode key : keys) { |
| 170 | + if (!(key instanceof StringLiteralNode)) { |
| 171 | + return DynamicDictLiteralNodeGen.create(keys, values); |
| 172 | + } |
| 173 | + } |
| 174 | + return FixedDictLiteralNodeGen.create(keys, values); |
97 | 175 | }
|
98 | 176 | }
|
0 commit comments