|
80 | 80 | import com.oracle.truffle.api.CompilerDirectives;
|
81 | 81 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
82 | 82 | import com.oracle.truffle.api.dsl.Cached;
|
83 |
| -import com.oracle.truffle.api.dsl.Cached.Shared; |
84 | 83 | import com.oracle.truffle.api.dsl.Fallback;
|
85 | 84 | import com.oracle.truffle.api.dsl.GenerateNodeFactory;
|
86 | 85 | import com.oracle.truffle.api.dsl.ImportStatic;
|
@@ -108,7 +107,7 @@ public abstract static class IndexNode extends PythonBuiltinNode {
|
108 | 107 |
|
109 | 108 | private static final String ERROR_TYPE_MESSAGE = "slice indices must be integers or have an __index__ method";
|
110 | 109 |
|
111 |
| - @Child private SequenceStorageNodes.GetItemNode getItemNode; |
| 110 | + @Child private SequenceStorageNodes.ItemIndexNode itemIndexNode; |
112 | 111 | @Child private SequenceStorageNodes.LenNode lenNode;
|
113 | 112 |
|
114 | 113 | public abstract int execute(VirtualFrame frame, Object arg1, Object arg2, Object arg3, Object arg4);
|
@@ -137,74 +136,47 @@ private int correctIndex(PTuple tuple, PInt index) {
|
137 | 136 | return value.min(BigInteger.valueOf(Integer.MAX_VALUE)).intValue();
|
138 | 137 | }
|
139 | 138 |
|
140 |
| - private int findIndex(VirtualFrame frame, PTuple tuple, Object value, int start, int end, |
141 |
| - PythonObjectLibrary valueLib, PythonObjectLibrary otherLib) { |
142 |
| - SequenceStorage tupleStore = tuple.getSequenceStorage(); |
143 |
| - int len = tupleStore.length(); |
144 |
| - for (int i = start; i < end && i < len; i++) { |
145 |
| - Object object = getGetItemNode().execute(frame, tupleStore, i); |
146 |
| - if (valueLib.equals(object, value, otherLib)) { |
147 |
| - return i; |
148 |
| - } |
| 139 | + private int findIndex(VirtualFrame frame, SequenceStorage s, Object value, int start, int end) { |
| 140 | + int idx = getItemIndexNode().execute(frame, s, value, start, end); |
| 141 | + if (idx != -1) { |
| 142 | + return idx; |
149 | 143 | }
|
150 | 144 | throw raise(PythonErrorType.ValueError, "tuple.index(x): x not in tuple");
|
151 | 145 | }
|
152 | 146 |
|
153 |
| - private SequenceStorageNodes.GetItemNode getGetItemNode() { |
154 |
| - if (getItemNode == null) { |
155 |
| - CompilerDirectives.transferToInterpreterAndInvalidate(); |
156 |
| - getItemNode = insert(SequenceStorageNodes.GetItemNode.createNotNormalized()); |
157 |
| - } |
158 |
| - return getItemNode; |
159 |
| - } |
160 |
| - |
161 | 147 | @Specialization
|
162 |
| - int index(VirtualFrame frame, PTuple self, Object value, @SuppressWarnings("unused") PNone start, @SuppressWarnings("unused") PNone end, |
163 |
| - @Shared("valueLib") @CachedLibrary("value") PythonObjectLibrary valueLib, |
164 |
| - @Shared("otherLib") @CachedLibrary(limit = "2") PythonObjectLibrary otherLib) { |
165 |
| - return findIndex(frame, self, value, 0, getLength(self), valueLib, otherLib); |
| 148 | + int index(VirtualFrame frame, PTuple self, Object value, @SuppressWarnings("unused") PNone start, @SuppressWarnings("unused") PNone end) { |
| 149 | + return findIndex(frame, self.getSequenceStorage(), value, 0, getLength(self)); |
166 | 150 | }
|
167 | 151 |
|
168 | 152 | @Specialization
|
169 |
| - int index(VirtualFrame frame, PTuple self, Object value, long start, @SuppressWarnings("unused") PNone end, |
170 |
| - @Shared("valueLib") @CachedLibrary("value") PythonObjectLibrary valueLib, |
171 |
| - @Shared("otherLib") @CachedLibrary(limit = "2") PythonObjectLibrary otherLib) { |
172 |
| - return findIndex(frame, self, value, correctIndex(self, start), getLength(self), valueLib, otherLib); |
| 153 | + int index(VirtualFrame frame, PTuple self, Object value, long start, @SuppressWarnings("unused") PNone end) { |
| 154 | + return findIndex(frame, self.getSequenceStorage(), value, correctIndex(self, start), getLength(self)); |
173 | 155 | }
|
174 | 156 |
|
175 | 157 | @Specialization
|
176 |
| - int index(VirtualFrame frame, PTuple self, Object value, long start, long end, |
177 |
| - @Shared("valueLib") @CachedLibrary("value") PythonObjectLibrary valueLib, |
178 |
| - @Shared("otherLib") @CachedLibrary(limit = "2") PythonObjectLibrary otherLib) { |
179 |
| - return findIndex(frame, self, value, correctIndex(self, start), correctIndex(self, end), valueLib, otherLib); |
| 158 | + int index(VirtualFrame frame, PTuple self, Object value, long start, long end) { |
| 159 | + return findIndex(frame, self.getSequenceStorage(), value, correctIndex(self, start), correctIndex(self, end)); |
180 | 160 | }
|
181 | 161 |
|
182 | 162 | @Specialization
|
183 |
| - int indexPI(VirtualFrame frame, PTuple self, Object value, PInt start, @SuppressWarnings("unused") PNone end, |
184 |
| - @Shared("valueLib") @CachedLibrary("value") PythonObjectLibrary valueLib, |
185 |
| - @Shared("otherLib") @CachedLibrary(limit = "2") PythonObjectLibrary otherLib) { |
186 |
| - return findIndex(frame, self, value, correctIndex(self, start), getLength(self), valueLib, otherLib); |
| 163 | + int indexPI(VirtualFrame frame, PTuple self, Object value, PInt start, @SuppressWarnings("unused") PNone end) { |
| 164 | + return findIndex(frame, self.getSequenceStorage(), value, correctIndex(self, start), getLength(self)); |
187 | 165 | }
|
188 | 166 |
|
189 | 167 | @Specialization
|
190 |
| - int indexPIPI(VirtualFrame frame, PTuple self, Object value, PInt start, PInt end, |
191 |
| - @Shared("valueLib") @CachedLibrary("value") PythonObjectLibrary valueLib, |
192 |
| - @Shared("otherLib") @CachedLibrary(limit = "2") PythonObjectLibrary otherLib) { |
193 |
| - return findIndex(frame, self, value, correctIndex(self, start), correctIndex(self, end), valueLib, otherLib); |
| 168 | + int indexPIPI(VirtualFrame frame, PTuple self, Object value, PInt start, PInt end) { |
| 169 | + return findIndex(frame, self.getSequenceStorage(), value, correctIndex(self, start), correctIndex(self, end)); |
194 | 170 | }
|
195 | 171 |
|
196 | 172 | @Specialization
|
197 |
| - int indexLPI(VirtualFrame frame, PTuple self, Object value, long start, PInt end, |
198 |
| - @Shared("valueLib") @CachedLibrary("value") PythonObjectLibrary valueLib, |
199 |
| - @Shared("otherLib") @CachedLibrary(limit = "2") PythonObjectLibrary otherLib) { |
200 |
| - return findIndex(frame, self, value, correctIndex(self, start), correctIndex(self, end), valueLib, otherLib); |
| 173 | + int indexLPI(VirtualFrame frame, PTuple self, Object value, long start, PInt end) { |
| 174 | + return findIndex(frame, self.getSequenceStorage(), value, correctIndex(self, start), correctIndex(self, end)); |
201 | 175 | }
|
202 | 176 |
|
203 | 177 | @Specialization
|
204 |
| - int indexPIL(VirtualFrame frame, PTuple self, Object value, PInt start, Long end, |
205 |
| - @Shared("valueLib") @CachedLibrary("value") PythonObjectLibrary valueLib, |
206 |
| - @Shared("otherLib") @CachedLibrary(limit = "2") PythonObjectLibrary otherLib) { |
207 |
| - return findIndex(frame, self, value, correctIndex(self, start), correctIndex(self, end), valueLib, otherLib); |
| 178 | + int indexPIL(VirtualFrame frame, PTuple self, Object value, PInt start, Long end) { |
| 179 | + return findIndex(frame, self.getSequenceStorage(), value, correctIndex(self, start), correctIndex(self, end)); |
208 | 180 | }
|
209 | 181 |
|
210 | 182 | @Specialization
|
@@ -272,6 +244,14 @@ protected IndexNode createIndexNode() {
|
272 | 244 | return TupleBuiltinsFactory.IndexNodeFactory.create(new ReadArgumentNode[0]);
|
273 | 245 | }
|
274 | 246 |
|
| 247 | + private SequenceStorageNodes.ItemIndexNode getItemIndexNode() { |
| 248 | + if (itemIndexNode == null) { |
| 249 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 250 | + itemIndexNode = insert(SequenceStorageNodes.ItemIndexNode.create()); |
| 251 | + } |
| 252 | + return itemIndexNode; |
| 253 | + } |
| 254 | + |
275 | 255 | }
|
276 | 256 |
|
277 | 257 | @Builtin(name = "count", minNumOfPositionalArgs = 2)
|
|
0 commit comments