Skip to content

Commit 9a86e2a

Browse files
committed
Refactor: remove copy method from BasicSequenceStorage
WIP: broken build
1 parent 6418288 commit 9a86e2a

File tree

11 files changed

+206
-98
lines changed

11 files changed

+206
-98
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java

Lines changed: 197 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package com.oracle.graal.python.builtins.objects.common;
2727

2828
import static com.oracle.graal.python.builtins.objects.iterator.IteratorBuiltins.NextHelperNode.STOP_MARKER;
29+
import static com.oracle.graal.python.nodes.truffle.TruffleStringMigrationHelpers.assertNoJavaString;
2930
import static com.oracle.graal.python.runtime.exception.PythonErrorType.IndexError;
3031
import static com.oracle.graal.python.runtime.exception.PythonErrorType.MemoryError;
3132
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
@@ -40,6 +41,7 @@
4041
import static com.oracle.graal.python.runtime.sequence.storage.SequenceStorage.StorageType.Uninitialized;
4142

4243
import java.lang.reflect.Array;
44+
import java.math.BigInteger;
4345
import java.util.Arrays;
4446

4547
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
@@ -3170,10 +3172,39 @@ static SequenceStorage doEmpty(EmptySequenceStorage s) {
31703172
return s;
31713173
}
31723174

3173-
@Specialization(limit = "MAX_BASIC_STORAGES", guards = "s.getClass() == cachedClass")
3174-
static SequenceStorage doSpecial(BasicSequenceStorage s,
3175-
@Cached("s.getClass()") Class<? extends BasicSequenceStorage> cachedClass) {
3176-
return CompilerDirectives.castExact(CompilerDirectives.castExact(s, cachedClass).copy(), cachedClass);
3175+
@Specialization
3176+
static SequenceStorage doInt(IntSequenceStorage storage) {
3177+
return new IntSequenceStorage(PythonUtils.arrayCopyOf(storage.getInternalIntArray(), storage.length()));
3178+
}
3179+
3180+
@Specialization
3181+
static SequenceStorage doLong(LongSequenceStorage storage) {
3182+
return new LongSequenceStorage(PythonUtils.arrayCopyOf(storage.getInternalLongArray(), storage.length()));
3183+
}
3184+
3185+
@Specialization
3186+
static SequenceStorage doDouble(DoubleSequenceStorage storage) {
3187+
return new DoubleSequenceStorage(PythonUtils.arrayCopyOf(storage.getInternalDoubleArray(), storage.length()));
3188+
}
3189+
3190+
@Specialization
3191+
static SequenceStorage doByte(ByteSequenceStorage storage) {
3192+
return new ByteSequenceStorage(PythonUtils.arrayCopyOf(storage.getInternalByteArray(), storage.length()));
3193+
}
3194+
3195+
@Specialization
3196+
static SequenceStorage doBoolean(BoolSequenceStorage storage) {
3197+
return new BoolSequenceStorage(PythonUtils.arrayCopyOf(storage.getInternalBoolArray(), storage.length()));
3198+
}
3199+
3200+
@Specialization
3201+
static SequenceStorage doObject(ObjectSequenceStorage storage) {
3202+
return new ObjectSequenceStorage(PythonUtils.arrayCopyOf(storage.getCopyOfInternalArray(), storage.length()));
3203+
}
3204+
3205+
@Specialization
3206+
static SequenceStorage doMro(MroSequenceStorage storage) {
3207+
return new MroSequenceStorage(storage.getClassName(), PythonUtils.arrayCopyOf(storage.getInternalClassArray(), storage.length()));
31773208
}
31783209

31793210
@Specialization
@@ -3667,53 +3698,185 @@ static boolean isObjectSequenceStorage(SequenceStorage s) {
36673698

36683699
}
36693700

3701+
@GenerateCached
36703702
@GenerateUncached
36713703
@GenerateInline(inlineByDefault = true)
3672-
@GenerateCached
3673-
@ImportStatic(SequenceStorageBaseNode.class)
3674-
public abstract static class InsertItemNode extends Node {
3675-
public final SequenceStorage execute(Node inliningTarget, SequenceStorage storage, int index, Object value) {
3676-
return execute(inliningTarget, storage, index, value, true);
3704+
public abstract static class InsertItemArrayBasedStorageNode extends Node {
3705+
3706+
protected abstract SequenceStorage execute(Node inliningTarget, ArrayBasedSequenceStorage storage, int index, Object value);
3707+
3708+
public final SequenceStorage executeCached(ArrayBasedSequenceStorage storage, int index, Object value) {
3709+
return execute(this, storage, index, value);
36773710
}
36783711

3679-
public static SequenceStorage executeUncached(SequenceStorage storage, int index, Object value) {
3680-
return InsertItemNodeGen.getUncached().execute(null, storage, index, value);
3712+
@Specialization
3713+
static SequenceStorage doIntStorage(Node inliningTarget, IntSequenceStorage storage, int idx, int value,
3714+
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3715+
final int length = storage.length();
3716+
ensureCapacityNode.execute(inliningTarget, storage, length + 1);
3717+
final int[] values = storage.getInternalIntArray();
3718+
3719+
// shifting tail to the right by one slot
3720+
for (int i = values.length - 1; i > idx; i--) {
3721+
values[i] = values[i - 1];
3722+
}
3723+
3724+
values[idx] = value;
3725+
storage.incLength();
3726+
return storage;
3727+
}
3728+
3729+
@Specialization
3730+
static SequenceStorage doDoubleStorage(Node inliningTarget, DoubleSequenceStorage storage, int idx, double value,
3731+
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3732+
final int length = storage.length();
3733+
ensureCapacityNode.execute(inliningTarget, storage, length + 1);
3734+
final double[] values = storage.getInternalDoubleArray();
3735+
3736+
// shifting tail to the right by one slot
3737+
for (int i = values.length - 1; i > idx; i--) {
3738+
values[i] = values[i - 1];
3739+
}
3740+
3741+
values[idx] = value;
3742+
storage.incLength();
3743+
return storage;
36813744
}
36823745

3683-
protected final SequenceStorage executeCached(SequenceStorage storage, int index, Object value, boolean recursive) {
3684-
return execute(this, storage, index, value, recursive);
3746+
@Specialization
3747+
static SequenceStorage doLongWithLongStorage(Node inliningTarget, LongSequenceStorage storage, int idx, long value,
3748+
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3749+
return insertLong(inliningTarget, storage, idx, value, ensureCapacityNode);
36853750
}
36863751

3687-
protected abstract SequenceStorage execute(Node inliningTarget, SequenceStorage storage, int index, Object value, boolean recursive);
3752+
@Specialization
3753+
static SequenceStorage doIntWithLongStorage(Node inliningTarget, LongSequenceStorage storage, int idx, int value,
3754+
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3755+
return insertLong(inliningTarget, storage, idx, value, ensureCapacityNode);
3756+
}
36883757

36893758
@Specialization
3690-
protected static SequenceStorage doEmptyStorage(EmptySequenceStorage storage, int index, Object value, boolean recursive,
3691-
@Shared @Cached(inline = false) InsertItemNode recursiveNode) {
3692-
if (!recursive) {
3693-
throw CompilerDirectives.shouldNotReachHere();
3759+
static SequenceStorage doBigIntWithLongStorage(Node inliningTarget, LongSequenceStorage storage, int idx, BigInteger value,
3760+
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3761+
return insertLong(inliningTarget, storage, idx, PInt.longValue(value), ensureCapacityNode);
3762+
}
3763+
3764+
@Specialization
3765+
static SequenceStorage doByteWithByteStorage(Node inliningTarget, ByteSequenceStorage storage, int idx, byte value,
3766+
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3767+
return insertByte(inliningTarget, storage, idx, value, ensureCapacityNode);
3768+
}
3769+
3770+
@Specialization
3771+
static SequenceStorage doIntWithByteStorage(Node inliningTarget, ByteSequenceStorage storage, int idx, int value,
3772+
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3773+
return insertByte(inliningTarget, storage, idx, (byte) value, ensureCapacityNode);
3774+
}
3775+
3776+
@Specialization
3777+
static SequenceStorage doBoolStorage(Node inliningTarget, BoolSequenceStorage storage, int idx, boolean value,
3778+
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3779+
final int length = storage.length();
3780+
ensureCapacityNode.execute(inliningTarget, storage, length + 1);
3781+
final boolean[] values = storage.getInternalBoolArray();
3782+
3783+
// shifting tail to the right by one slot
3784+
for (int i = values.length - 1; i > idx; i--) {
3785+
values[i] = values[i - 1];
36943786
}
3695-
SequenceStorage newStorage = storage.generalizeFor(value, null);
3696-
return recursiveNode.executeCached(newStorage, index, value, false);
3787+
3788+
values[idx] = value;
3789+
storage.incLength();
3790+
return storage;
36973791
}
36983792

3699-
@Specialization(limit = "MAX_BASIC_STORAGES", guards = {"storage.getClass() == cachedClass"})
3700-
protected static SequenceStorage doStorage(BasicSequenceStorage storage, int index, Object value, boolean recursive,
3701-
@Shared @Cached(inline = false) InsertItemNode recursiveNode,
3702-
@Cached("storage.getClass()") Class<? extends BasicSequenceStorage> cachedClass) {
3703-
try {
3704-
cachedClass.cast(storage).insertItem(index, value);
3705-
return storage;
3706-
} catch (SequenceStoreException e) {
3707-
if (!recursive) {
3708-
throw CompilerDirectives.shouldNotReachHere();
3709-
}
3710-
SequenceStorage newStorage = cachedClass.cast(storage).generalizeFor(value, null);
3711-
return recursiveNode.executeCached(newStorage, index, value, false);
3793+
@Specialization
3794+
static SequenceStorage doObjectStorage(Node inliningTarget, ObjectSequenceStorage storage, int idx, Object value,
3795+
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3796+
final int length = storage.length();
3797+
ensureCapacityNode.execute(inliningTarget, storage, length + 1);
3798+
final Object[] values = storage.getInternalArray();
3799+
3800+
// shifting tail to the right by one slot
3801+
for (int i = values.length - 1; i > idx; i--) {
3802+
values[i] = values[i - 1];
3803+
}
3804+
3805+
values[idx] = assertNoJavaString(value);
3806+
storage.incLength();
3807+
return storage;
3808+
}
3809+
3810+
@Specialization
3811+
static SequenceStorage doMroStorage(MroSequenceStorage storage, int idx, Object value) {
3812+
throw CompilerDirectives.shouldNotReachHere();
3813+
}
3814+
3815+
@Fallback
3816+
static SequenceStorage doGeneralization(ArrayBasedSequenceStorage storage, int idx, Object value,
3817+
@Cached(inline = false) InsertItemArrayBasedStorageNode recursiveNode) {
3818+
ObjectSequenceStorage newStorage = storage.generalize();
3819+
return recursiveNode.executeCached(newStorage, idx, value);
3820+
}
3821+
3822+
private static SequenceStorage insertLong(Node inliningTarget, LongSequenceStorage storage, int idx, long value, EnsureCapacityNode ensureCapacityNode) {
3823+
final int length = storage.length();
3824+
ensureCapacityNode.execute(inliningTarget, storage, length + 1);
3825+
final long[] values = storage.getInternalLongArray();
3826+
3827+
// shifting tail to the right by one slot
3828+
for (int i = values.length - 1; i > idx; i--) {
3829+
values[i] = values[i - 1];
37123830
}
3831+
3832+
values[idx] = value;
3833+
storage.incLength();
3834+
return storage;
3835+
}
3836+
3837+
private static SequenceStorage insertByte(Node inliningTarget, ByteSequenceStorage storage, int idx, byte value, EnsureCapacityNode ensureCapacityNode) {
3838+
final int length = storage.length();
3839+
ensureCapacityNode.execute(inliningTarget, storage, length + 1);
3840+
final byte[] values = storage.getInternalByteArray();
3841+
3842+
// shifting tail to the right by one slot
3843+
for (int i = values.length - 1; i > idx; i--) {
3844+
values[i] = values[i - 1];
3845+
}
3846+
3847+
values[idx] = value;
3848+
storage.incLength();
3849+
return storage;
3850+
}
3851+
}
3852+
3853+
@GenerateUncached
3854+
@GenerateInline
3855+
@GenerateCached(false)
3856+
@ImportStatic(SequenceStorageBaseNode.class)
3857+
public abstract static class InsertItemNode extends Node {
3858+
public static SequenceStorage executeUncached(SequenceStorage storage, int index, Object value) {
3859+
return InsertItemNodeGen.getUncached().execute(null, storage, index, value);
3860+
}
3861+
3862+
public abstract SequenceStorage execute(Node inliningTarget, SequenceStorage storage, int index, Object value);
3863+
3864+
@Specialization
3865+
protected static SequenceStorage doEmptyStorage(Node inliningTarget, EmptySequenceStorage storage, int index, Object value,
3866+
@Shared @Cached InsertItemArrayBasedStorageNode insertArrayBasedNode) {
3867+
ArrayBasedSequenceStorage newStorage = storage.generalizeFor(value);
3868+
return insertArrayBasedNode.execute(inliningTarget, newStorage, index, value);
3869+
}
3870+
3871+
@Specialization
3872+
static SequenceStorage doArrayBasedStorage(Node inliningTarget, ArrayBasedSequenceStorage storage, int index, Object value,
3873+
@Shared @Cached InsertItemArrayBasedStorageNode insertArrayBasedNode) {
3874+
return insertArrayBasedNode.execute(inliningTarget, storage, index, value);
3875+
37133876
}
37143877

37153878
@Specialization
3716-
protected static SequenceStorage doStorage(Node inliningTarget, NativeSequenceStorage storage, int index, Object value, @SuppressWarnings("unused") boolean recursive,
3879+
protected static SequenceStorage doNativeStorage(Node inliningTarget, NativeSequenceStorage storage, int index, Object value,
37173880
@Cached EnsureCapacityNode ensureCapacityNode,
37183881
@Cached(inline = false) GetItemScalarNode getItem,
37193882
@Cached SetItemScalarNode setItem) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/ArrayBasedSequenceStorage.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public abstract class ArrayBasedSequenceStorage extends BasicSequenceStorage {
5454

5555
public abstract ArrayBasedSequenceStorage createEmpty(int newCapacity);
5656

57-
@Override
58-
public ObjectSequenceStorage generalizeFor(Object value, SequenceStorage other) {
57+
public ObjectSequenceStorage generalize() {
5958
return new ObjectSequenceStorage(getInternalArray());
6059
}
6160

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/BasicSequenceStorage.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ public abstract class BasicSequenceStorage extends SequenceStorage {
3636

3737
public abstract void insertItem(int idx, Object value) throws SequenceStoreException;
3838

39-
public abstract SequenceStorage copy();
40-
41-
public abstract SequenceStorage generalizeFor(Object value, SequenceStorage other);
42-
4339
/**
4440
* Get internal array object without copying. Note: The length must be taken from the sequence
4541
* storage object.
@@ -48,7 +44,7 @@ public final void setNewLength(int length) {
4844
this.length = length;
4945
}
5046

51-
protected final void incLength() {
47+
public final void incLength() {
5248
this.length++;
5349
}
5450

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/BoolSequenceStorage.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
import java.util.Arrays;
2929

30-
import com.oracle.graal.python.util.PythonUtils;
31-
3230
public final class BoolSequenceStorage extends ArrayBasedSequenceStorage {
3331

3432
private boolean[] values;
@@ -59,11 +57,6 @@ protected void increaseCapacityExactWithCopy(int newCapacity) {
5957
capacity = values.length;
6058
}
6159

62-
@Override
63-
public SequenceStorage copy() {
64-
return new BoolSequenceStorage(PythonUtils.arrayCopyOf(values, length));
65-
}
66-
6760
@Override
6861
public BoolSequenceStorage createEmpty(int newLength) {
6962
return new BoolSequenceStorage(newLength);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/ByteSequenceStorage.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ protected void increaseCapacityExactWithCopy(int newCapacity) {
6666
capacity = values.length;
6767
}
6868

69-
@Override
70-
public SequenceStorage copy() {
71-
return new ByteSequenceStorage(PythonUtils.arrayCopyOf(values, length));
72-
}
73-
7469
@Override
7570
public ByteSequenceStorage createEmpty(int newCapacity) {
7671
return new ByteSequenceStorage(newCapacity);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/DoubleSequenceStorage.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
import java.util.Arrays;
2929

30-
import com.oracle.graal.python.util.PythonUtils;
31-
3230
public final class DoubleSequenceStorage extends ArrayBasedSequenceStorage {
3331

3432
private double[] values;
@@ -61,11 +59,6 @@ protected void increaseCapacityExactWithCopy(int newCapacity) {
6159
capacity = values.length;
6260
}
6361

64-
@Override
65-
public SequenceStorage copy() {
66-
return new DoubleSequenceStorage(PythonUtils.arrayCopyOf(values, length));
67-
}
68-
6962
@Override
7063
public ArrayBasedSequenceStorage createEmpty(int newCapacity) {
7164
return new DoubleSequenceStorage(newCapacity);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/EmptySequenceStorage.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,17 @@ public final class EmptySequenceStorage extends SequenceStorage {
3232

3333
public static final EmptySequenceStorage INSTANCE = new EmptySequenceStorage();
3434

35-
public SequenceStorage generalizeFor(Object value, SequenceStorage target) {
36-
final SequenceStorage generalized;
35+
public ArrayBasedSequenceStorage generalizeFor(Object value) {
36+
final ArrayBasedSequenceStorage generalized;
3737

3838
if (value instanceof Byte) {
3939
generalized = new ByteSequenceStorage(16);
4040
} else if (value instanceof Boolean) {
4141
generalized = new BoolSequenceStorage(16);
4242
} else if (value instanceof Integer) {
43-
if (target instanceof ByteSequenceStorage) {
44-
generalized = new ByteSequenceStorage(16);
45-
} else {
46-
generalized = new IntSequenceStorage();
47-
}
43+
generalized = new IntSequenceStorage();
4844
} else if (value instanceof Long) {
49-
if (target instanceof ByteSequenceStorage) {
50-
generalized = new ByteSequenceStorage(16);
51-
} else {
52-
generalized = new LongSequenceStorage();
53-
}
45+
generalized = new LongSequenceStorage();
5446
} else if (value instanceof Double) {
5547
generalized = new DoubleSequenceStorage();
5648
} else {

0 commit comments

Comments
 (0)