|
60 | 60 | import com.oracle.graal.python.builtins.objects.PNone;
|
61 | 61 | import com.oracle.graal.python.builtins.objects.PNotImplemented;
|
62 | 62 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
|
| 63 | +import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.ListGeneralizationNode; |
63 | 64 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.NormalizeIndexNode;
|
64 | 65 | import com.oracle.graal.python.builtins.objects.ints.PInt;
|
65 | 66 | import com.oracle.graal.python.builtins.objects.iterator.PDoubleSequenceIterator;
|
|
97 | 98 | import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
|
98 | 99 | import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
|
99 | 100 | import com.oracle.graal.python.runtime.sequence.storage.SequenceStoreException;
|
100 |
| -import com.oracle.graal.python.runtime.sequence.storage.SetSequenceStorageItem; |
101 | 101 | import com.oracle.graal.python.runtime.sequence.storage.TupleSequenceStorage;
|
102 | 102 | import com.oracle.truffle.api.CompilerDirectives;
|
103 | 103 | import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
|
@@ -297,73 +297,49 @@ protected Object doGeneric(Object self, Object objectIdx) {
|
297 | 297 | public abstract static class SetItemNode extends PythonTernaryBuiltinNode {
|
298 | 298 |
|
299 | 299 | @Child private NormalizeIndexNode normalize = NormalizeIndexNode.forListAssign();
|
| 300 | + private final ConditionProfile generalizedProfile = ConditionProfile.createBinaryProfile(); |
300 | 301 |
|
301 | 302 | @Specialization
|
302 |
| - public PNone doPList(PList list, PSlice slice, Object value, |
| 303 | + public PNone doPList(PList primary, PSlice slice, Object value, |
303 | 304 | @Cached("create()") ListNodes.SetSliceNode sliceNode) {
|
304 |
| - return sliceNode.execute(list, slice, value); |
305 |
| - } |
306 |
| - |
307 |
| - @Specialization(guards = "isIntStorage(primary)") |
308 |
| - public Object doPListInt(PList primary, int idx, int value) { |
309 |
| - IntSequenceStorage store = (IntSequenceStorage) primary.getSequenceStorage(); |
310 |
| - store.setIntItemNormalized(normalize.execute(idx, store.length()), value); |
311 |
| - return PNone.NONE; |
312 |
| - } |
313 |
| - |
314 |
| - @Specialization(guards = "isDoubleStorage(primary)") |
315 |
| - public Object doPListDouble(PList primary, int idx, double value) { |
316 |
| - DoubleSequenceStorage store = (DoubleSequenceStorage) primary.getSequenceStorage(); |
317 |
| - store.setDoubleItemNormalized(normalize.execute(idx, store.length()), value); |
318 |
| - return PNone.NONE; |
319 |
| - } |
320 |
| - |
321 |
| - @Specialization(guards = "isObjectStorage(primary)") |
322 |
| - public Object doPListObject(PList primary, int idx, Object value) { |
323 |
| - ObjectSequenceStorage store = (ObjectSequenceStorage) primary.getSequenceStorage(); |
324 |
| - store.setItemNormalized(normalize.execute(idx, store.length()), value); |
325 |
| - return PNone.NONE; |
| 305 | + return sliceNode.execute(primary, slice, value); |
326 | 306 | }
|
327 | 307 |
|
328 | 308 | @Specialization
|
329 |
| - public Object doPList(PList list, int idx, Object value, |
330 |
| - @Cached("create()") SetSequenceStorageItem setItem) { |
331 |
| - setItem.setItem(list, idx, value); |
332 |
| - return PNone.NONE; |
333 |
| - } |
334 |
| - |
335 |
| - @Specialization(guards = "isIntStorage(primary)") |
336 |
| - public Object doPListInt(PList primary, long idx, int value) { |
337 |
| - IntSequenceStorage store = (IntSequenceStorage) primary.getSequenceStorage(); |
338 |
| - store.setIntItemNormalized(normalize.execute(idx, store.length()), value); |
| 309 | + public Object doPListInt(PList primary, int idx, Object value, |
| 310 | + @Cached("createSetItem()") SequenceStorageNodes.SetItemNode setItemNode) { |
| 311 | + updateStorage(primary, setItemNode.execute(primary.getSequenceStorage(), idx, value)); |
339 | 312 | return PNone.NONE;
|
340 | 313 | }
|
341 | 314 |
|
342 |
| - @Specialization(guards = "isDoubleStorage(primary)") |
343 |
| - public Object doPListDouble(PList primary, long idx, double value) { |
344 |
| - DoubleSequenceStorage store = (DoubleSequenceStorage) primary.getSequenceStorage(); |
345 |
| - store.setDoubleItemNormalized(normalize.execute(idx, store.length()), value); |
| 315 | + @Specialization |
| 316 | + public Object doPListLong(PList primary, long idx, Object value, |
| 317 | + @Cached("createSetItem()") SequenceStorageNodes.SetItemNode setItemNode) { |
| 318 | + updateStorage(primary, setItemNode.execute(primary.getSequenceStorage(), idx, value)); |
346 | 319 | return PNone.NONE;
|
347 | 320 | }
|
348 | 321 |
|
349 |
| - @Specialization(guards = "isObjectStorage(primary)") |
350 |
| - public Object doPListObject(PList primary, long idx, Object value) { |
351 |
| - ObjectSequenceStorage store = (ObjectSequenceStorage) primary.getSequenceStorage(); |
352 |
| - store.setItemNormalized(normalize.execute(idx, store.length()), value); |
| 322 | + @Specialization |
| 323 | + public Object doPListPInt(PList primary, PInt idx, Object value, |
| 324 | + @Cached("createSetItem()") SequenceStorageNodes.SetItemNode setItemNode) { |
| 325 | + updateStorage(primary, setItemNode.execute(primary.getSequenceStorage(), idx, value)); |
353 | 326 | return PNone.NONE;
|
354 | 327 | }
|
355 | 328 |
|
356 |
| - @Specialization |
357 |
| - public Object doPList(PList list, long idx, Object value, |
358 |
| - @Cached("create()") SetSequenceStorageItem setItem) { |
359 |
| - setItem.setItem(list, idx, value); |
360 |
| - return PNone.NONE; |
| 329 | + private void updateStorage(PList primary, SequenceStorage newStorage) { |
| 330 | + if (generalizedProfile.profile(primary.getSequenceStorage() != newStorage)) { |
| 331 | + primary.setSequenceStorage(newStorage); |
| 332 | + } |
361 | 333 | }
|
362 | 334 |
|
363 | 335 | protected static SetItemNode create() {
|
364 | 336 | return ListBuiltinsFactory.SetItemNodeFactory.create();
|
365 | 337 | }
|
366 | 338 |
|
| 339 | + protected static SequenceStorageNodes.SetItemNode createSetItem() { |
| 340 | + return SequenceStorageNodes.SetItemNode.create(NormalizeIndexNode.forArrayAssign(), () -> ListGeneralizationNode.create()); |
| 341 | + } |
| 342 | + |
367 | 343 | @Specialization
|
368 | 344 | protected Object doObjectIndex(PList self, Object objectIdx, Object value,
|
369 | 345 | @Cached("create()") IndexNode getIndexNode,
|
@@ -458,14 +434,15 @@ public PNone extendSequenceStore(PList list, Object source) throws SequenceStore
|
458 | 434 | }
|
459 | 435 |
|
460 | 436 | @Specialization(guards = {"isPSequenceWithStorage(source)"})
|
461 |
| - public PNone extendSequence(PList list, Object source) { |
| 437 | + public PNone extendSequence(PList list, Object source, |
| 438 | + @Cached("create()") SequenceStorageNodes.ListGeneralizationNode genNode) { |
462 | 439 | SequenceStorage eSource = ((PSequence) source).getSequenceStorage();
|
463 | 440 | if (eSource.length() > 0) {
|
464 | 441 | SequenceStorage target = list.getSequenceStorage();
|
465 | 442 | try {
|
466 | 443 | target.extend(eSource);
|
467 | 444 | } catch (SequenceStoreException e) {
|
468 |
| - target = target.generalizeFor(eSource.getItemNormalized(0), eSource); |
| 445 | + target = genNode.execute(target, e.getIndicationValue()); |
469 | 446 | list.setSequenceStorage(target);
|
470 | 447 | try {
|
471 | 448 | target.extend(eSource);
|
@@ -1057,9 +1034,26 @@ PList doPListObject(PList left, PList right) {
|
1057 | 1034 | }
|
1058 | 1035 |
|
1059 | 1036 | @Specialization
|
1060 |
| - PList doPList(PList left, PList right) { |
| 1037 | + PList doPList(PList left, PList other, |
| 1038 | + @Cached("create()") SequenceStorageNodes.ListGeneralizationNode genNode) { |
1061 | 1039 | try {
|
1062 |
| - return left.__add__(right); |
| 1040 | + SequenceStorage store = left.getSequenceStorage(); |
| 1041 | + SequenceStorage otherStore = other.getSequenceStorage(); |
| 1042 | + SequenceStorage newStore = store.copy(); |
| 1043 | + |
| 1044 | + try { |
| 1045 | + newStore.extend(otherStore); |
| 1046 | + } catch (SequenceStoreException e) { |
| 1047 | + newStore = genNode.execute(newStore, e.getIndicationValue()); |
| 1048 | + |
| 1049 | + try { |
| 1050 | + newStore.extend(otherStore); |
| 1051 | + } catch (SequenceStoreException e1) { |
| 1052 | + throw new IllegalStateException(); |
| 1053 | + } |
| 1054 | + } |
| 1055 | + |
| 1056 | + return factory().createList(left.getPythonClass(), newStore); |
1063 | 1057 | } catch (ArithmeticException e) {
|
1064 | 1058 | throw raise(MemoryError);
|
1065 | 1059 | }
|
|
0 commit comments