Skip to content

Commit 75dc645

Browse files
committed
Using SequenceStorage.extend methods, if it's possible.
1 parent db463c0 commit 75dc645

File tree

3 files changed

+39
-68
lines changed

3 files changed

+39
-68
lines changed

graalpython/com.oracle.graal.python.test/src/tests/list_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def __length_hint__(self):
288288
a = self.type2test([1,2,3,4])
289289
a.extend(CustomIter())
290290
self.assertEqual(a, [1,2,3,4])
291-
'''
291+
292292
def test_insert(self):
293293
a = self.type2test([0, 1, 2])
294294
a.insert(0, -2)

graalpython/com.oracle.graal.python.test/src/tests/test_list.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,22 @@ def test_extend_spec(self):
290290
a = [1.1, 2.1]
291291
a.extend(a)
292292
self.assertEqual([1.1, 2.1, 1.1, 2.1], a)
293+
294+
a = []
295+
a.extend(range(1,4))
296+
self.assertEqual([1,2,3], a)
297+
298+
a = []
299+
a.extend('ahoj')
300+
self.assertEqual(['a','h','o','j'], a)
301+
293302
def test_remove_spec(self):
294303
a = [1,2]
295304
a.remove(2);
296305
self.assertEqual([1], a)
297306
a.remove(1)
298307
self.assertEqual([], a)
299-
308+
300309
a = [0,1,0,1,2]
301310
a.remove(True)
302311
self.assertEqual([0,0,1,2], a)
@@ -306,13 +315,14 @@ def test_remove_spec(self):
306315
a = list([LONG_NUMBER, LONG_NUMBER + 1])
307316
a.remove(LONG_NUMBER + 1)
308317
self.assertEqual([LONG_NUMBER], a)
309-
318+
310319
class MyInt(int):
311320
pass
312-
321+
313322
a = [1,2,3]
314323
a.remove(MyInt(2))
315324
self.assertEqual([1,3], a)
325+
316326
def test_insert_spec(self):
317327
a = [1,2]
318328
self.assertRaises(TypeError, a.insert, [1,2,3], 1)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list/ListBuiltins.java

Lines changed: 25 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.oracle.graal.python.builtins.objects.iterator.PIntegerSequenceIterator;
4949
import com.oracle.graal.python.builtins.objects.iterator.PLongSequenceIterator;
5050
import com.oracle.graal.python.builtins.objects.iterator.PSequenceIterator;
51+
import com.oracle.graal.python.builtins.objects.range.PRange;
5152
import com.oracle.graal.python.builtins.objects.slice.PSlice;
5253
import com.oracle.graal.python.builtins.objects.str.PString;
5354
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
@@ -477,77 +478,38 @@ protected boolean isKnownStorage(PList list) {
477478
@GenerateNodeFactory
478479
public abstract static class ListExtendNode extends PythonBuiltinNode {
479480

480-
@Specialization(guards = {"isIntStorage(list)", "isIntStorage(source)"})
481-
public PNone extendIntInt(PList list, PList source) {
482-
IntSequenceStorage target = (IntSequenceStorage) list.getSequenceStorage();
483-
IntSequenceStorage what = list != source
484-
? (IntSequenceStorage) source.getSequenceStorage()
485-
: (IntSequenceStorage) source.getSequenceStorage().copy();
486-
int[] array = what.getInternalIntArray();
487-
for (int i = 0; i < what.length(); i++) {
488-
target.appendInt(array[i]);
489-
}
490-
return PNone.NONE;
491-
}
492-
493-
@Specialization(guards = {"isLongStorage(list)", "isIntStorage(source)"})
494-
public PNone extendLongInt(PList list, PList source) {
495-
LongSequenceStorage target = (LongSequenceStorage) list.getSequenceStorage();
496-
IntSequenceStorage what = list != source
497-
? (IntSequenceStorage) source.getSequenceStorage()
498-
: (IntSequenceStorage) source.getSequenceStorage().copy();
499-
int[] array = what.getInternalIntArray();
500-
for (int i = 0; i < what.length(); i++) {
501-
target.appendLong(array[i]);
502-
}
503-
return PNone.NONE;
504-
}
505-
506-
@Specialization(guards = {"isLongStorage(list)", "isLongStorage(source)"})
507-
public PNone extendLongLong(PList list, PList source) {
508-
LongSequenceStorage target = (LongSequenceStorage) list.getSequenceStorage();
509-
LongSequenceStorage what = list != source
510-
? (LongSequenceStorage) source.getSequenceStorage()
511-
: (LongSequenceStorage) source.getSequenceStorage().copy();
512-
long[] array = what.getInternalLongArray();
513-
for (int i = 0; i < what.length(); i++) {
514-
target.appendLong(array[i]);
515-
}
516-
return PNone.NONE;
517-
}
518-
519-
@Specialization(guards = {"isDoubleStorage(list)", "isDoubleStorage(source)"})
520-
public PNone extendDoubleDouble(PList list, PList source) {
521-
DoubleSequenceStorage target = (DoubleSequenceStorage) list.getSequenceStorage();
522-
DoubleSequenceStorage what = list != source
523-
? (DoubleSequenceStorage) source.getSequenceStorage()
524-
: (DoubleSequenceStorage) source.getSequenceStorage().copy();
525-
double[] array = what.getInternalDoubleArray();
526-
for (int i = 0; i < what.length(); i++) {
527-
target.appendDouble(array[i]);
528-
}
481+
@Specialization(guards = {"isPSequenceWithStorage(source)"}, rewriteOn = {SequenceStoreException.class})
482+
public PNone extendSequenceStore(PList list, Object source) throws SequenceStoreException {
483+
SequenceStorage target = list.getSequenceStorage();
484+
target.extend(((PSequence) source).getSequenceStorage());
529485
return PNone.NONE;
530486
}
531487

532-
@Specialization(guards = {"isObjectStorage(list)", "isObjectStorage(source)"})
533-
public PNone extendObjectObject(PList list, PList source) {
534-
ObjectSequenceStorage target = (ObjectSequenceStorage) list.getSequenceStorage();
535-
ObjectSequenceStorage what = list != source
536-
? (ObjectSequenceStorage) source.getSequenceStorage()
537-
: (ObjectSequenceStorage) source.getSequenceStorage().copy();
538-
Object[] array = what.getInternalArray();
539-
for (int i = 0; i < what.length(); i++) {
540-
target.append(array[i]);
488+
@Specialization(guards = {"isPSequenceWithStorage(source)"})
489+
public PNone extendSequence(PList list, Object source) {
490+
SequenceStorage eSource = ((PSequence) source).getSequenceStorage();
491+
if (eSource.length() > 0) {
492+
SequenceStorage target = list.getSequenceStorage();
493+
try {
494+
target.extend(eSource);
495+
} catch (SequenceStoreException e) {
496+
target = target.generalizeFor(eSource.getItemNormalized(0));
497+
list.setSequenceStorage(target);
498+
try {
499+
target.extend(eSource);
500+
} catch (SequenceStoreException e1) {
501+
throw new IllegalStateException();
502+
}
503+
}
541504
}
542505
return PNone.NONE;
543506
}
544507

545-
@Specialization(guards = "isNotSpecialCase(list, source)")
508+
@Specialization(guards = "!isPSequenceWithStorage(source)")
546509
public PNone extend(PList list, Object source,
547510
@Cached("create()") GetIteratorNode getIterator,
548511
@Cached("create()") GetNextNode next,
549512
@Cached("createBinaryProfile()") ConditionProfile errorProfile) {
550-
551513
Object workSource = list != source ? source : factory().createList(((PList) source).getSequenceStorage().copy());
552514
Object iterator = getIterator.executeWith(workSource);
553515
while (true) {
@@ -562,11 +524,10 @@ public PNone extend(PList list, Object source,
562524
}
563525
}
564526

565-
protected boolean isNotSpecialCase(PList list, Object source) {
566-
return !(source instanceof PList && (((PGuards.isIntStorage(list) || PGuards.isLongStorage(list)) && PGuards.isIntStorage((PList) source)) ||
567-
(PGuards.isLongStorage(list) && PGuards.isLongStorage((PList) source)) || (PGuards.isDoubleStorage(list) && PGuards.isDoubleStorage((PList) source)) ||
568-
(PGuards.isObjectStorage(list) && PGuards.isObjectStorage((PList) source))));
527+
protected boolean isPSequenceWithStorage(Object source) {
528+
return (source instanceof PSequence && !(source instanceof PTuple || source instanceof PRange));
569529
}
530+
570531
}
571532

572533
// list.insert(i, x)

0 commit comments

Comments
 (0)