Skip to content

Commit aa1da48

Browse files
committed
refactor: move all methods to ArrayBasedSequenceStorage
1 parent 2a26b6a commit aa1da48

File tree

13 files changed

+35
-97
lines changed

13 files changed

+35
-97
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/runtime/SequenceStorageTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private static Object[] getObjectValues() {
4141
public void objectsGetAndSet() {
4242
ObjectSequenceStorage store = new ObjectSequenceStorage(getObjectValues());
4343
assertEquals(4, store.getObjectItemNormalized(3));
44-
store.setItemNormalized(5, 10);
44+
store.setObjectItemNormalized(5, 10);
4545
assertEquals(10, store.getObjectItemNormalized(5));
4646
}
4747

@@ -56,7 +56,7 @@ private static int[] getIntValues() {
5656
public void intGetAndSet() throws SequenceStoreException {
5757
IntSequenceStorage store = new IntSequenceStorage(getIntValues());
5858
assertEquals(4, store.getIntItemNormalized(3));
59-
store.setItemNormalized(5, 10);
59+
store.setIntItemNormalized(5, 10);
6060
assertEquals(10, store.getIntItemNormalized(5));
6161
}
6262
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextObjectBuiltins.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
import com.oracle.graal.python.runtime.exception.PException;
137137
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
138138
import com.oracle.graal.python.runtime.sequence.PSequence;
139+
import com.oracle.graal.python.runtime.sequence.storage.ArrayBasedSequenceStorage;
139140
import com.oracle.graal.python.runtime.sequence.storage.BasicSequenceStorage;
140141
import com.oracle.graal.python.runtime.sequence.storage.EmptySequenceStorage;
141142
import com.oracle.graal.python.runtime.sequence.storage.NativeSequenceStorage;
@@ -584,7 +585,7 @@ static PNone set(PSequence obj, long size,
584585
@Cached InlinedBranchProfile nativeProfile) {
585586
SequenceStorage storage = getSequenceStorageNode.execute(inliningTarget, obj);
586587
// Can't use SetLenNode as that decrefs items for native storages when shrinking
587-
if (storage instanceof BasicSequenceStorage basicStorage) {
588+
if (storage instanceof ArrayBasedSequenceStorage basicStorage) {
588589
basicProfile.enter(inliningTarget);
589590
basicStorage.setNewLength((int) size);
590591
} else if (storage instanceof NativeSequenceStorage nativeStorage) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/jni/GraalHPyJNIContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,7 @@ private boolean ctxListSetItem(Object receiver, long lidx, long hValue) {
19831983
return true;
19841984
} else if (storage instanceof ObjectSequenceStorage) {
19851985
Object value = context.getObjectForHPyHandle(GraalHPyBoxing.unboxHandle(hValue));
1986-
((ObjectSequenceStorage) storage).setItemNormalized(idx, value);
1986+
((ObjectSequenceStorage) storage).setObjectItemNormalized(idx, value);
19871987
return true;
19881988
}
19891989
// TODO: other storages...

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@
115115
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
116116
import com.oracle.graal.python.runtime.sequence.PSequence;
117117
import com.oracle.graal.python.runtime.sequence.storage.ArrayBasedSequenceStorage;
118-
import com.oracle.graal.python.runtime.sequence.storage.BasicSequenceStorage;
119118
import com.oracle.graal.python.runtime.sequence.storage.BoolSequenceStorage;
120119
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
121120
import com.oracle.graal.python.runtime.sequence.storage.DoubleSequenceStorage;
@@ -911,14 +910,14 @@ public final SequenceStorage execute(SequenceStorage s, int key, double value) {
911910
@Specialization
912911
protected SequenceStorage doScalarInt(IntSequenceStorage storage, int idx, int value) {
913912
int normalized = normalizeIndex(idx, storage);
914-
storage.setItemNormalized(normalized, value);
913+
storage.setIntItemNormalized(normalized, value);
915914
return storage;
916915
}
917916

918917
@Specialization
919918
protected SequenceStorage doScalarInt(DoubleSequenceStorage storage, int idx, double value) {
920919
int normalized = normalizeIndex(idx, storage);
921-
storage.setItemNormalized(normalized, value);
920+
storage.setDoubleItemNormalized(normalized, value);
922921
return storage;
923922
}
924923

@@ -1168,7 +1167,7 @@ protected static void doDouble(@SuppressWarnings("unused") Node inliningTarget,
11681167

11691168
@Specialization
11701169
protected static void doObject(@SuppressWarnings("unused") Node inliningTarget, ObjectSequenceStorage storage, int idx, Object value) {
1171-
storage.setItemNormalized(idx, value);
1170+
storage.setObjectItemNormalized(idx, value);
11721171
}
11731172

11741173
@Fallback
@@ -2499,7 +2498,7 @@ SequenceStorage doGeneric(SequenceStorage s, int times,
24992498
try {
25002499
int len = s.length();
25012500
int newLen = PythonUtils.multiplyExact(len, times);
2502-
BasicSequenceStorage repeated = createEmptyNode.execute(inliningTarget, s, newLen, -1);
2501+
ArrayBasedSequenceStorage repeated = createEmptyNode.execute(inliningTarget, s, newLen, -1);
25032502

25042503
for (int i = 0; i < len; i++) {
25052504
setItemNode.execute(inliningTarget, repeated, i, getItemNode.execute(inliningTarget, s, i));
@@ -2738,7 +2737,7 @@ static ObjectSequenceStorage doObject(@SuppressWarnings("unused") ObjectSequence
27382737
}
27392738

27402739
@Specialization
2741-
static SequenceStorage doEmptyStorage(Node inliningTarget, @SuppressWarnings("unused") EmptySequenceStorage s, BasicSequenceStorage other,
2740+
static SequenceStorage doEmptyStorage(Node inliningTarget, @SuppressWarnings("unused") EmptySequenceStorage s, ArrayBasedSequenceStorage other,
27422741
@Exclusive @Cached InlinedExactClassProfile otherProfile) {
27432742
return otherProfile.profile(inliningTarget, other).createEmpty(DEFAULT_CAPACITY);
27442743
}
@@ -2928,14 +2927,14 @@ public static AppendNode getUncached() {
29282927
@GenerateCached(false)
29292928
public abstract static class CreateEmptyNode extends SequenceStorageBaseNode {
29302929

2931-
public abstract BasicSequenceStorage execute(Node inliningTarget, SequenceStorage s, int cap, int len);
2930+
public abstract ArrayBasedSequenceStorage execute(Node inliningTarget, SequenceStorage s, int cap, int len);
29322931

29332932
@Specialization
2934-
static BasicSequenceStorage doIt(Node inliningTarget, SequenceStorage s, int cap, int len,
2933+
static ArrayBasedSequenceStorage doIt(Node inliningTarget, SequenceStorage s, int cap, int len,
29352934
@Cached EnsureCapacityNode ensureCapacityNode,
29362935
@Cached GetElementType getElementType,
29372936
@Cached CreateEmptyForTypeNode createEmptyForTypeNode) {
2938-
BasicSequenceStorage ss = createEmptyForTypeNode.execute(inliningTarget, getElementType.execute(inliningTarget, s), cap);
2937+
ArrayBasedSequenceStorage ss = createEmptyForTypeNode.execute(inliningTarget, getElementType.execute(inliningTarget, s), cap);
29392938
if (len != -1) {
29402939
ensureCapacityNode.execute(inliningTarget, ss, len);
29412940
ss.setNewLength(len);
@@ -2948,7 +2947,7 @@ static BasicSequenceStorage doIt(Node inliningTarget, SequenceStorage s, int cap
29482947
@GenerateCached(false)
29492948
abstract static class CreateEmptyForTypeNode extends SequenceStorageBaseNode {
29502949

2951-
public abstract BasicSequenceStorage execute(Node inliningTarget, StorageType type, int cap);
2950+
public abstract ArrayBasedSequenceStorage execute(Node inliningTarget, StorageType type, int cap);
29522951

29532952
@Specialization(guards = "isBoolean(type)")
29542953
static BoolSequenceStorage doBoolean(@SuppressWarnings("unused") StorageType type, int cap) {
@@ -3251,7 +3250,7 @@ public abstract static class SetLenNode extends Node {
32513250
public abstract void execute(Node inliningTarget, SequenceStorage s, int len);
32523251

32533252
@Specialization
3254-
static void doBasic(BasicSequenceStorage s, int len) {
3253+
static void doBasic(ArrayBasedSequenceStorage s, int len) {
32553254
s.setNewLength(len);
32563255
}
32573256

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,23 @@ public abstract class ArrayBasedSequenceStorage extends BasicSequenceStorage {
4949
public abstract void setInternalArrayObject(Object arrayObject);
5050

5151
public abstract ArrayBasedSequenceStorage createEmpty(int newCapacity);
52+
53+
public final void setNewLength(int length) {
54+
this.length = length;
55+
}
56+
57+
public final void incLength() {
58+
this.length++;
59+
}
60+
61+
/**
62+
* The capacity we should allocate for a given length.
63+
*/
64+
protected static int capacityFor(int length) throws ArithmeticException {
65+
return Math.max(16, Math.multiplyExact(length, 2));
66+
}
67+
68+
public void minimizeCapacity() {
69+
capacity = length;
70+
}
5271
}

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,4 @@
2727

2828
public abstract class BasicSequenceStorage extends SequenceStorage {
2929

30-
public abstract void setItemNormalized(int idx, Object value) throws SequenceStoreException;
31-
32-
public final void setNewLength(int length) {
33-
this.length = length;
34-
}
35-
36-
public final void incLength() {
37-
this.length++;
38-
}
39-
40-
public abstract BasicSequenceStorage createEmpty(int newCapacity);
41-
42-
/**
43-
* The capacity we should allocate for a given length.
44-
*/
45-
protected static int capacityFor(int length) throws ArithmeticException {
46-
return Math.max(16, Math.multiplyExact(length, 2));
47-
}
48-
49-
public void minimizeCapacity() {
50-
capacity = length;
51-
}
5230
}

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,6 @@ public boolean getBoolItemNormalized(int idx) {
103103
return values[idx];
104104
}
105105

106-
@Override
107-
public void setItemNormalized(int idx, Object value) throws SequenceStoreException {
108-
if (value instanceof Boolean) {
109-
setBoolItemNormalized(idx, (boolean) value);
110-
} else {
111-
throw new SequenceStoreException(value);
112-
}
113-
}
114-
115106
public void setBoolItemNormalized(int idx, boolean value) {
116107
values[idx] = value;
117108
}

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,6 @@ public int getIntItemNormalized(int idx) {
9999
return values[idx] & 0xFF;
100100
}
101101

102-
@Override
103-
public void setItemNormalized(int idx, Object value) throws SequenceStoreException {
104-
if (value instanceof Byte) {
105-
setByteItemNormalized(idx, (byte) value);
106-
} else if (value instanceof Integer) {
107-
if ((int) value < 0 || (int) value >= 256) {
108-
throw PRaiseNode.raiseUncached(null, ValueError, ErrorMessages.BYTE_MUST_BE_IN_RANGE);
109-
}
110-
setByteItemNormalized(idx, ((Integer) value).byteValue());
111-
} else {
112-
throw PRaiseNode.raiseUncached(null, TypeError, ErrorMessages.INTEGER_REQUIRED);
113-
}
114-
}
115-
116102
public void setByteItemNormalized(int idx, byte value) {
117103
values[idx] = value;
118104
}

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,6 @@ public double getDoubleItemNormalized(int idx) {
9393
return values[idx];
9494
}
9595

96-
@Override
97-
public void setItemNormalized(int idx, Object value) throws SequenceStoreException {
98-
if (value instanceof Double) {
99-
setDoubleItemNormalized(idx, (double) value);
100-
} else {
101-
throw new SequenceStoreException(value);
102-
}
103-
}
104-
10596
public void setDoubleItemNormalized(int idx, double value) {
10697
values[idx] = value;
10798
}

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,6 @@ public int getIntItemNormalized(int idx) {
9393
return values[idx];
9494
}
9595

96-
@Override
97-
public void setItemNormalized(int idx, Object value) throws SequenceStoreException {
98-
if (value instanceof Integer) {
99-
setIntItemNormalized(idx, (int) value);
100-
} else {
101-
throw new SequenceStoreException(value);
102-
}
103-
}
104-
10596
public void setIntItemNormalized(int idx, int value) {
10697
values[idx] = value;
10798
}

0 commit comments

Comments
 (0)