Skip to content

Commit e804a59

Browse files
committed
Do not assume storage type of bytes object
1 parent 96d9262 commit e804a59

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
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;
100101
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetInternalArrayNode;
101102
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetInternalByteArrayNode;
102103
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetInternalObjectArrayNode;
@@ -684,24 +685,27 @@ protected static boolean shouldCopy(PBytesIO self) {
684685
return self.getStringSize() <= 1 || self.getExports() > 0;
685686
}
686687

687-
protected static boolean shouldUnshare(PBytesIO self) {
688-
return self.getStringSize() != self.getBufCapacity();
688+
protected static boolean shouldUnshare(GetCapacityNode getCapacityNode, PBytesIO self) {
689+
int capacity = getCapacityNode.execute(self.getBuf().getSequenceStorage());
690+
return self.getStringSize() != capacity;
689691
}
690692

691693
@Specialization(guards = {"self.hasBuf()", "shouldCopy(self)"})
692-
Object copy(PBytesIO self,
694+
Object doCopy(PBytesIO self,
693695
@Cached GetInternalByteArrayNode getBytes) {
694696
byte[] buf = getBytes.execute(self.getBuf().getSequenceStorage());
695697
return factory().createBytes(PythonUtils.arrayCopyOf(buf, self.getStringSize()));
696698
}
697699

698-
@Specialization(guards = {"self.hasBuf()", "!shouldCopy(self)", "!shouldUnshare(self)"})
699-
static Object doit(PBytesIO self) {
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) {
700703
return self.getBuf();
701704
}
702705

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

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@
4343
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
4444
import com.oracle.graal.python.builtins.objects.memoryview.BufferLifecycleManager;
4545
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
46-
import com.oracle.graal.python.runtime.sequence.storage.BasicSequenceStorage;
4746
import com.oracle.truffle.api.object.Shape;
4847

49-
public class PBytesIO extends PythonBuiltinObject {
48+
public final class PBytesIO extends PythonBuiltinObject {
5049
private PBytes buf;
5150
private int pos;
5251
private int stringSize;
@@ -89,11 +88,6 @@ public void setStringSize(int size) {
8988
this.stringSize = size;
9089
}
9190

92-
public int getBufCapacity() {
93-
// Casting is safe as we only create/replace buf internally.
94-
return ((BasicSequenceStorage) buf.getSequenceStorage()).capacity();
95-
}
96-
9791
public int getExports() {
9892
return exports.getExports().get();
9993
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3066,6 +3066,27 @@ 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+
30693090
@GenerateUncached
30703091
@ImportStatic(SequenceStorageBaseNode.class)
30713092
public abstract static class GetInternalArrayNode extends Node {

0 commit comments

Comments
 (0)