Skip to content

Commit 0ab21b7

Browse files
committed
Move storage length and capacity to base class
1 parent e804a59 commit 0ab21b7

File tree

7 files changed

+60
-122
lines changed

7 files changed

+60
-122
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/BytesIOBuiltins.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
9898
import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
9999
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.EnsureCapacityNode;
100-
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetCapacityNode;
101100
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetInternalArrayNode;
102101
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetInternalByteArrayNode;
103102
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetInternalObjectArrayNode;
@@ -685,8 +684,8 @@ protected static boolean shouldCopy(PBytesIO self) {
685684
return self.getStringSize() <= 1 || self.getExports() > 0;
686685
}
687686

688-
protected static boolean shouldUnshare(GetCapacityNode getCapacityNode, PBytesIO self) {
689-
int capacity = getCapacityNode.execute(self.getBuf().getSequenceStorage());
687+
protected static boolean shouldUnshare(PBytesIO self) {
688+
int capacity = self.getBuf().getSequenceStorage().getCapacity();
690689
return self.getStringSize() != capacity;
691690
}
692691

@@ -697,15 +696,13 @@ Object doCopy(PBytesIO self,
697696
return factory().createBytes(PythonUtils.arrayCopyOf(buf, self.getStringSize()));
698697
}
699698

700-
@Specialization(guards = {"self.hasBuf()", "!shouldCopy(self)", "!shouldUnshare(getCapacityNode, self)"}, limit = "1")
701-
static Object doShare(PBytesIO self,
702-
@SuppressWarnings("unused") @Shared("getCapacityNode") @Cached GetCapacityNode getCapacityNode) {
699+
@Specialization(guards = {"self.hasBuf()", "!shouldCopy(self)", "!shouldUnshare(self)"})
700+
static Object doShare(PBytesIO self) {
703701
return self.getBuf();
704702
}
705703

706-
@Specialization(guards = {"self.hasBuf()", "!shouldCopy(self)", "shouldUnshare(getCapacityNode, self)"}, limit = "1")
704+
@Specialization(guards = {"self.hasBuf()", "!shouldCopy(self)", "shouldUnshare(self)"})
707705
Object doUnshare(PBytesIO self,
708-
@SuppressWarnings("unused") @Shared("getCapacityNode") @Cached GetCapacityNode getCapacityNode,
709706
@Cached GetInternalArrayNode internalArray) {
710707
// if (SHARED_BUF(self))
711708
unshareBuffer(self, self.getStringSize(), internalArray, factory());

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,7 +2839,7 @@ static SequenceStorage doManaged(BasicSequenceStorage s, Object val, GenNodeSupp
28392839
@Shared("genNode") @Cached DoGeneralizationNode doGenNode) {
28402840
int len = lenNode.execute(s);
28412841
int newLen = len + 1;
2842-
int capacity = s.capacity();
2842+
int capacity = s.getCapacity();
28432843
if (newLen > capacity) {
28442844
increaseCapacity.enter();
28452845
ensureCapacity.execute(s, len + 1);
@@ -3066,27 +3066,6 @@ private static NativeSequenceStorage reallocNativeSequenceStorage(NativeSequence
30663066
}
30673067
}
30683068

3069-
@GenerateUncached
3070-
public abstract static class GetCapacityNode extends Node {
3071-
3072-
public abstract int execute(SequenceStorage s);
3073-
3074-
@Specialization
3075-
static int doBasicSequenceStorage(BasicSequenceStorage s) {
3076-
return s.capacity();
3077-
}
3078-
3079-
@Specialization
3080-
static int doNativeSequenceStorage(NativeSequenceStorage s) {
3081-
return s.getCapacity();
3082-
}
3083-
3084-
@Specialization
3085-
static int doBasicSequenceStorage(@SuppressWarnings("unused") EmptySequenceStorage s) {
3086-
return 0;
3087-
}
3088-
}
3089-
30903069
@GenerateUncached
30913070
@ImportStatic(SequenceStorageBaseNode.class)
30923071
public abstract static class GetInternalArrayNode extends Node {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/literal/ListLiteralNode.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private int updateFrom(int newSizeEstimate) {
7676
}
7777
}
7878

79-
@CompilationFinal private SizeEstimate initialCapacity;
79+
private final SizeEstimate initialCapacity;
8080
private final boolean hasStarredExpressions;
8181

8282
public ListLiteralNode(ExpressionNode[] values) {
@@ -136,18 +136,14 @@ private SequenceStorageNodes.AppendNode ensureAppendNode() {
136136
public void reportUpdatedCapacity(BasicSequenceStorage newStore) {
137137
if (CompilerDirectives.inInterpreter()) {
138138
if (PythonContext.get(this).getOption(PythonOptions.OverallocateLiteralLists)) {
139-
if (newStore.capacity() > initialCapacity.estimate()) {
140-
initialCapacity.updateFrom(newStore.capacity());
141-
LOGGER.finest(() -> {
142-
return String.format("Updating list size estimate at %s. Observed capacity: %d, new estimate: %d", getSourceSection().toString(), newStore.capacity(),
143-
initialCapacity.estimate());
144-
});
139+
if (newStore.getCapacity() > initialCapacity.estimate()) {
140+
initialCapacity.updateFrom(newStore.getCapacity());
141+
LOGGER.finest(() -> String.format("Updating list size estimate at %s. Observed capacity: %d, new estimate: %d", getSourceSection().toString(), newStore.getCapacity(),
142+
initialCapacity.estimate()));
145143
}
146144
if (newStore.getElementType().generalizesFrom(type)) {
147145
type = newStore.getElementType();
148-
LOGGER.finest(() -> {
149-
return String.format("Updating list type estimate at %s. New type: %s", getSourceSection().toString(), type.name());
150-
});
146+
LOGGER.finest(() -> String.format("Updating list type estimate at %s. New type: %s", getSourceSection().toString(), type.name()));
151147
}
152148
}
153149
}

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,6 @@
2929

3030
public abstract class BasicSequenceStorage extends SequenceStorage {
3131

32-
// nominated storage length
33-
protected int length;
34-
35-
// physical storage length
36-
protected int capacity;
37-
38-
@Override
39-
public final int length() {
40-
return length;
41-
}
42-
43-
@Override
44-
public void setNewLength(int length) {
45-
this.length = length;
46-
}
47-
48-
protected final void incLength() {
49-
this.length++;
50-
}
51-
52-
public final int capacity() {
53-
return capacity;
54-
}
55-
5632
public abstract Object getCopyOfInternalArrayObject();
5733

5834
public abstract void setInternalArrayObject(Object arrayObject);

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
public final class EmptySequenceStorage extends SequenceStorage {
3636

3737
public static final EmptySequenceStorage INSTANCE = new EmptySequenceStorage();
38-
private static final Object[] EMPTY_ARRAY = new Object[0];
3938

4039
@Override
4140
public SequenceStorage generalizeFor(Object value, SequenceStorage target) {
@@ -71,11 +70,6 @@ public Object getIndicativeValue() {
7170
return null;
7271
}
7372

74-
@Override
75-
public int length() {
76-
return 0;
77-
}
78-
7973
@Override
8074
public void setNewLength(int length) {
8175
if (length != 0) {
@@ -96,12 +90,12 @@ public SequenceStorage createEmpty(int newCapacity) {
9690

9791
@Override
9892
public Object[] getInternalArray() {
99-
return EMPTY_ARRAY;
93+
return PythonUtils.EMPTY_OBJECT_ARRAY;
10094
}
10195

10296
@Override
10397
public Object[] getCopyOfInternalArray() {
104-
return EMPTY_ARRAY;
98+
return PythonUtils.EMPTY_OBJECT_ARRAY;
10599
}
106100

107101
@Override

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

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,11 @@ public final class NativeSequenceStorage extends SequenceStorage {
5858
/* native pointer object */
5959
private Object ptr;
6060

61-
/* length of contents */
62-
private int len;
63-
64-
/* allocated capacity */
65-
private int capacity;
66-
6761
private final ListStorageType elementType;
6862

6963
public NativeSequenceStorage(Object ptr, int length, int capacity, ListStorageType elementType) {
64+
super(length, capacity);
7065
this.ptr = ptr;
71-
this.capacity = capacity;
72-
this.len = length;
7366
this.elementType = elementType;
7467
}
7568

@@ -81,10 +74,6 @@ public void setPtr(Object ptr) {
8174
this.ptr = ptr;
8275
}
8376

84-
public int getCapacity() {
85-
return capacity;
86-
}
87-
8877
public void setCapacity(int capacity) {
8978
this.capacity = capacity;
9079
}
@@ -94,105 +83,86 @@ public ListStorageType getElementType() {
9483
return elementType;
9584
}
9685

97-
@Override
98-
public final int length() {
99-
return len;
100-
}
101-
10286
@Override
10387
public void setNewLength(int length) {
10488
assert length <= capacity;
105-
this.len = length;
89+
this.length = length;
10690
}
10791

10892
@Override
10993
public String toString() {
11094
CompilerAsserts.neverPartOfCompilation();
111-
return String.format("<NativeSequenceStorage(type=%s, len=%d, cap=%d) at %s>", elementType, len, capacity, ptr);
95+
return String.format("<NativeSequenceStorage(type=%s, len=%d, cap=%d) at %s>", elementType, length, capacity, ptr);
11296
}
11397

11498
@Override
11599
public void ensureCapacity(@SuppressWarnings("unused") int newCapacity) {
116-
CompilerDirectives.transferToInterpreterAndInvalidate();
117-
throw new IllegalStateException("should not reach");
100+
throw CompilerDirectives.shouldNotReachHere();
118101
}
119102

120103
@Override
121104
public SequenceStorage copy() {
122-
CompilerDirectives.transferToInterpreterAndInvalidate();
123-
throw new IllegalStateException("should not reach");
105+
throw CompilerDirectives.shouldNotReachHere();
124106
}
125107

126108
@Override
127109
public SequenceStorage createEmpty(int newCapacity) {
128-
CompilerDirectives.transferToInterpreterAndInvalidate();
129-
throw new IllegalStateException("should not reach");
110+
throw CompilerDirectives.shouldNotReachHere();
130111
}
131112

132113
@Override
133114
public Object[] getInternalArray() {
134-
CompilerDirectives.transferToInterpreterAndInvalidate();
135-
throw new IllegalStateException("should not reach");
115+
throw CompilerDirectives.shouldNotReachHere();
136116
}
137117

138118
@Override
139119
public Object[] getCopyOfInternalArray() {
140-
CompilerDirectives.transferToInterpreterAndInvalidate();
141-
throw new IllegalStateException("should not reach");
120+
throw CompilerDirectives.shouldNotReachHere();
142121
}
143122

144123
@Override
145124
public Object getItemNormalized(int idx) {
146-
CompilerDirectives.transferToInterpreterAndInvalidate();
147-
throw new IllegalStateException("should not reach");
125+
throw CompilerDirectives.shouldNotReachHere();
148126
}
149127

150128
@Override
151129
public void setItemNormalized(int idx, Object value) throws SequenceStoreException {
152-
CompilerAsserts.neverPartOfCompilation();
153-
throw new IllegalStateException("should not reach");
130+
throw CompilerDirectives.shouldNotReachHere();
154131
}
155132

156133
@Override
157134
public void insertItem(int idx, Object value) throws SequenceStoreException {
158-
CompilerDirectives.transferToInterpreterAndInvalidate();
159-
throw new IllegalStateException("should not reach");
135+
throw CompilerDirectives.shouldNotReachHere();
160136
}
161137

162138
@Override
163139
public SequenceStorage getSliceInBound(int start, int stop, int step, int length) {
164-
CompilerAsserts.neverPartOfCompilation();
165-
throw new IllegalStateException("should not reach");
140+
throw CompilerDirectives.shouldNotReachHere();
166141
}
167142

168143
@Override
169144
public void reverse() {
170-
CompilerDirectives.transferToInterpreterAndInvalidate();
171-
throw new IllegalStateException("should not reach");
145+
throw CompilerDirectives.shouldNotReachHere();
172146
}
173147

174148
@Override
175149
public boolean equals(SequenceStorage other) {
176-
CompilerAsserts.neverPartOfCompilation();
177-
throw new IllegalStateException("should not reach");
150+
throw CompilerDirectives.shouldNotReachHere();
178151
}
179152

180153
@Override
181154
public SequenceStorage generalizeFor(Object value, SequenceStorage other) {
182-
CompilerDirectives.transferToInterpreterAndInvalidate();
183-
throw new IllegalStateException("should not reach");
155+
throw CompilerDirectives.shouldNotReachHere();
184156
}
185157

186158
@Override
187159
public Object getIndicativeValue() {
188-
CompilerDirectives.transferToInterpreterAndInvalidate();
189-
throw new IllegalStateException("should not reach");
160+
throw CompilerDirectives.shouldNotReachHere();
190161
}
191162

192163
@Override
193164
public void copyItem(int idxTo, int idxFrom) {
194-
CompilerDirectives.transferToInterpreterAndInvalidate();
195-
throw new IllegalStateException("should not reach");
165+
throw CompilerDirectives.shouldNotReachHere();
196166
}
197167

198168
@Override
@@ -214,7 +184,7 @@ boolean isReadonly() {
214184

215185
@ExportMessage
216186
int getBufferLength() {
217-
return len;
187+
return length;
218188
}
219189

220190
@ExportMessage

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,35 @@ public boolean generalizesFrom(ListStorageType other) {
5555
}
5656
}
5757

58-
public abstract int length();
58+
// nominated storage length
59+
protected int length;
5960

60-
public abstract void setNewLength(int length);
61+
// physical storage length
62+
protected int capacity;
63+
64+
protected SequenceStorage() {
65+
}
66+
67+
protected SequenceStorage(int length, int capacity) {
68+
this.length = length;
69+
this.capacity = capacity;
70+
}
71+
72+
public final int length() {
73+
return length;
74+
}
75+
76+
public void setNewLength(int length) {
77+
this.length = length;
78+
}
79+
80+
protected final void incLength() {
81+
this.length++;
82+
}
83+
84+
public final int getCapacity() {
85+
return capacity;
86+
}
6187

6288
public abstract SequenceStorage copy();
6389

0 commit comments

Comments
 (0)