|
57 | 57 |
|
58 | 58 | import com.oracle.graal.python.builtins.objects.cext.CExtNodes.PCallBinaryCapiFunction;
|
59 | 59 | import com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols;
|
| 60 | +import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.AppendNodeGen; |
60 | 61 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.CastToByteNodeGen;
|
61 | 62 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.CmpNodeGen;
|
62 | 63 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.ConcatNodeGen;
|
@@ -1998,7 +1999,7 @@ public static GeneralizationNode create(String invalidItemErrorMessage) {
|
1998 | 1999 | // Implements list generalization rules; previously in 'SequenceStroage.generalizeFor'.
|
1999 | 2000 | public abstract static class ListGeneralizationNode extends GeneralizationNode {
|
2000 | 2001 |
|
2001 |
| - private static final int DEFAULT_CAPACITY = 16; |
| 2002 | + private static final int DEFAULT_CAPACITY = 8; |
2002 | 2003 |
|
2003 | 2004 | @Specialization
|
2004 | 2005 | SequenceStorage doObject(@SuppressWarnings("unused") ObjectSequenceStorage s, @SuppressWarnings("unused") Object indicationValue) {
|
@@ -2050,6 +2051,11 @@ SequenceStorage doEmptyPTuple(@SuppressWarnings("unused") EmptySequenceStorage s
|
2050 | 2051 | return new TupleSequenceStorage();
|
2051 | 2052 | }
|
2052 | 2053 |
|
| 2054 | + @Specialization |
| 2055 | + SequenceStorage doEmptyObject(@SuppressWarnings("unused") EmptySequenceStorage s, @SuppressWarnings("unused") Object val) { |
| 2056 | + return new ObjectSequenceStorage(DEFAULT_CAPACITY); |
| 2057 | + } |
| 2058 | + |
2053 | 2059 | @Specialization
|
2054 | 2060 | IntSequenceStorage doByteInteger(@SuppressWarnings("unused") ByteSequenceStorage s, @SuppressWarnings("unused") int val) {
|
2055 | 2061 | int[] copied = new int[s.length()];
|
@@ -2099,4 +2105,67 @@ public static ListGeneralizationNode create() {
|
2099 | 2105 |
|
2100 | 2106 | }
|
2101 | 2107 |
|
| 2108 | + public abstract static class AppendNode extends SequenceStorageBaseNode { |
| 2109 | + |
| 2110 | + @Child private GeneralizationNode genNode; |
| 2111 | + |
| 2112 | + private final Supplier<GeneralizationNode> genNodeProvider; |
| 2113 | + |
| 2114 | + public AppendNode(Supplier<GeneralizationNode> genNodeProvider) { |
| 2115 | + this.genNodeProvider = genNodeProvider; |
| 2116 | + } |
| 2117 | + |
| 2118 | + public abstract SequenceStorage execute(SequenceStorage s, Object val); |
| 2119 | + |
| 2120 | + @Specialization |
| 2121 | + SequenceStorage doEmpty(EmptySequenceStorage s, Object val, |
| 2122 | + @Cached("createRecursive()") AppendNode recursive) { |
| 2123 | + SequenceStorage newStorage = generalizeStore(s, val); |
| 2124 | + return recursive.execute(newStorage, val); |
| 2125 | + } |
| 2126 | + |
| 2127 | + @Specialization(limit = "9", guards = "s.getClass() == cachedClass") |
| 2128 | + SequenceStorage doManaged(BasicSequenceStorage s, Object val, |
| 2129 | + @Cached("s.getClass()") Class<? extends BasicSequenceStorage> cachedClass, |
| 2130 | + @Cached("create()") SetItemScalarNode setItemNode) { |
| 2131 | + BasicSequenceStorage profiled = cachedClass.cast(s); |
| 2132 | + int len = profiled.length(); |
| 2133 | + profiled.ensureCapacity(len + 1); |
| 2134 | + try { |
| 2135 | + setItemNode.execute(profiled, len, val); |
| 2136 | + profiled.setNewLength(len + 1); |
| 2137 | + return profiled; |
| 2138 | + } catch (SequenceStoreException e) { |
| 2139 | + SequenceStorage generalized = generalizeStore(profiled, e.getIndicationValue()); |
| 2140 | + generalized.ensureCapacity(len + 1); |
| 2141 | + try { |
| 2142 | + setItemNode.execute(generalized, len, val); |
| 2143 | + generalized.setNewLength(len + 1); |
| 2144 | + return generalized; |
| 2145 | + } catch (SequenceStoreException e1) { |
| 2146 | + CompilerDirectives.transferToInterpreter(); |
| 2147 | + throw new IllegalStateException(); |
| 2148 | + } |
| 2149 | + } |
| 2150 | + } |
| 2151 | + |
| 2152 | + // TODO native sequence storage |
| 2153 | + |
| 2154 | + protected AppendNode createRecursive() { |
| 2155 | + return AppendNodeGen.create(genNodeProvider); |
| 2156 | + } |
| 2157 | + |
| 2158 | + private SequenceStorage generalizeStore(SequenceStorage storage, Object value) { |
| 2159 | + if (genNode == null) { |
| 2160 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 2161 | + genNode = insert(genNodeProvider.get()); |
| 2162 | + } |
| 2163 | + return genNode.execute(storage, value); |
| 2164 | + } |
| 2165 | + |
| 2166 | + public static AppendNode create(Supplier<GeneralizationNode> genNodeProvider) { |
| 2167 | + return AppendNodeGen.create(genNodeProvider); |
| 2168 | + } |
| 2169 | + |
| 2170 | + } |
2102 | 2171 | }
|
0 commit comments