Skip to content

Commit 637f43a

Browse files
committed
chore: renaming and cleaning up based on review comments
1 parent 6353f28 commit 637f43a

File tree

13 files changed

+232
-345
lines changed

13 files changed

+232
-345
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
import java.util.List;
7979
import java.util.logging.Level;
8080

81+
import com.oracle.graal.python.nodes.util.ToNativePrimitiveStorageNode;
82+
import com.oracle.graal.python.runtime.sequence.storage.NativePrimitiveSequenceStorage;
8183
import org.graalvm.home.Version;
8284
import org.graalvm.nativeimage.ImageInfo;
8385

@@ -163,6 +165,7 @@
163165
import com.oracle.truffle.api.TruffleLogger;
164166
import com.oracle.truffle.api.dsl.Bind;
165167
import com.oracle.truffle.api.dsl.Cached;
168+
import com.oracle.truffle.api.dsl.Cached.Shared;
166169
import com.oracle.truffle.api.dsl.Fallback;
167170
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
168171
import com.oracle.truffle.api.dsl.NeverDefault;
@@ -1087,4 +1090,27 @@ TruffleString get() {
10871090
return context.getStdlibHome().concatUncached(sep, TS_ENCODING, false).concatUncached(context.getCoreHome(), TS_ENCODING, false);
10881091
}
10891092
}
1093+
1094+
@Builtin(name = "storage_to_native_primitive", minNumOfPositionalArgs = 1)
1095+
@GenerateNodeFactory
1096+
abstract static class StorageToNativePrimitive extends PythonUnaryBuiltinNode {
1097+
1098+
@Specialization
1099+
static Object doArray(PArray array,
1100+
@Shared @Cached ToNativePrimitiveStorageNode toNativePrimitiveNode,
1101+
@Bind("this") Node inliningTarget) {
1102+
NativePrimitiveSequenceStorage newStorage = toNativePrimitiveNode.execute(inliningTarget, array.getSequenceStorage());
1103+
array.setSequenceStorage(newStorage);
1104+
return array;
1105+
}
1106+
1107+
@Specialization
1108+
static Object doSequence(PSequence sequence,
1109+
@Shared @Cached ToNativePrimitiveStorageNode toNativePrimitiveNode,
1110+
@Bind("this") Node inliningTarget) {
1111+
NativePrimitiveSequenceStorage newStorage = toNativePrimitiveNode.execute(inliningTarget, sequence.getSequenceStorage());
1112+
sequence.setSequenceStorage(newStorage);
1113+
return sequence;
1114+
}
1115+
}
10901116
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/PySequenceArrayWrapper.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
import com.oracle.graal.python.runtime.sequence.storage.MroSequenceStorage;
5656
import com.oracle.graal.python.runtime.sequence.storage.NativeSequenceStorage;
5757
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
58-
import com.oracle.graal.python.runtime.sequence.storage.native2.ArrowSequenceStorage;
58+
import com.oracle.graal.python.runtime.sequence.storage.NativePrimitiveSequenceStorage;
5959
import com.oracle.graal.python.util.PythonUtils;
6060
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6161
import com.oracle.truffle.api.TruffleLogger;
@@ -101,11 +101,14 @@ static NativeSequenceStorage doManaged(Node inliningTarget, ArrayBasedSequenceSt
101101
return storageToNativeNode.execute(inliningTarget, array, s.length());
102102
}
103103

104+
/*
105+
* TODO This can be optimized further. Now we are converting NativePrimitiveSequenceStorage
106+
* to ObjectArrayStorage and then to NativeStorageStrategy
107+
*/
104108
@Specialization
105-
static NativeSequenceStorage doArrow(Node inliningTarget, ArrowSequenceStorage s, boolean isBytesLike,
109+
static NativeSequenceStorage doNativePrimitive(Node inliningTarget, NativePrimitiveSequenceStorage s, boolean isBytesLike,
106110
@Exclusive @Cached SequenceStorageNodes.StorageToNativeNode storageToNativeNode,
107111
@Exclusive @Cached SequenceStorageNodes.GetInternalObjectArrayNode getInternalArrayNode) {
108-
LOGGER.warning("The sequence backed by Arrow Storage is being converted to the Native Storage strategy. This operation is slow and should not typically occur.");
109112
Object array = getInternalArrayNode.execute(inliningTarget, s);
110113
return storageToNativeNode.execute(inliningTarget, array, s.length());
111114
}

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

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,8 @@
130130
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage.StorageType;
131131
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorageFactory;
132132
import com.oracle.graal.python.runtime.sequence.storage.SequenceStoreException;
133-
import com.oracle.graal.python.runtime.sequence.storage.native2.ArrowSequenceStorage;
134-
import com.oracle.graal.python.runtime.sequence.storage.native2.IntArrowSequenceStorage;
135-
import com.oracle.graal.python.runtime.sequence.storage.native2.NativeBuffer;
133+
import com.oracle.graal.python.runtime.sequence.storage.NativePrimitiveSequenceStorage;
134+
import com.oracle.graal.python.runtime.sequence.storage.NativeIntSequenceStorage;
136135
import com.oracle.graal.python.util.BiFunction;
137136
import com.oracle.graal.python.util.OverflowException;
138137
import com.oracle.graal.python.util.PythonUtils;
@@ -616,7 +615,7 @@ protected static Object doObject(ObjectSequenceStorage storage, int idx) {
616615
}
617616

618617
@Specialization
619-
protected static int doArrowInt(IntArrowSequenceStorage storage, int idx) {
618+
protected static int doNativeInt(NativeIntSequenceStorage storage, int idx) {
620619
return storage.getIntItemNormalized(idx);
621620
}
622621

@@ -786,10 +785,11 @@ protected static SequenceStorage doMroSequenceStorage(MroSequenceStorage storage
786785
}
787786

788787
@Specialization
789-
protected static SequenceStorage doArrowInt(IntArrowSequenceStorage storage, int start, int stop, int step, int length,
788+
protected static SequenceStorage doNativeInt(NativeIntSequenceStorage storage, int start, int stop, int step, int length,
790789
@Bind("this") Node node) {
791-
var newBuffer = doNativeSliceInBound(node, start, step, length, storage);
792-
return new IntArrowSequenceStorage(newBuffer, length);
790+
var valueBufferAddr = doNativePrimitiveSliceInBound(node, start, step, length, storage);
791+
long sizeInBytes = length * storage.getItemSize();
792+
return PythonContext.get(node).nativeBufferContext.wrapToIntStorage(valueBufferAddr, sizeInBytes, length);
793793
}
794794

795795
@Specialization
@@ -814,26 +814,25 @@ protected static SequenceStorage doNativeObject(NativeObjectSequenceStorage stor
814814
return new ObjectSequenceStorage(newArray);
815815
}
816816

817-
private static NativeBuffer doNativeSliceInBound(Node node, int start, int step, int sliceLength, ArrowSequenceStorage storage) {
818-
long typeWidth = storage.getTypeWidth();
819-
var context = PythonContext.get(node);
820-
var unsafe = context.getUnsafe();
821-
long sizeInBytes = sliceLength * typeWidth;
822-
var destNativeBuffer = context.nativeBufferContext.createNativeBuffer(sizeInBytes);
817+
private static long doNativePrimitiveSliceInBound(Node node, int start, int step, int sliceLength, NativePrimitiveSequenceStorage storage) {
818+
long itemSize = storage.getItemSize();
819+
var nativeContext = PythonContext.get(node).nativeBufferContext;
820+
long sizeInBytes = sliceLength * itemSize;
821+
long toAddr = nativeContext.allocateNativeMemory(sizeInBytes);
823822

824823
if (step == 1) {
825-
var startAddress = storage.getNativeBuffer().getMemoryAddress() + (start * typeWidth);
826-
unsafe.copyMemory(startAddress, destNativeBuffer.getMemoryAddress(), sizeInBytes);
827-
return destNativeBuffer;
824+
var startAddress = storage.getValueBufferAddr() + (start * itemSize);
825+
nativeContext.copyMemory(startAddress, toAddr, sizeInBytes);
826+
return toAddr;
828827
}
829828

830-
var stepInBytes = step * typeWidth;
831-
for (long srcAddr = storage.getNativeBuffer().getMemoryAddress() + (start * typeWidth), destAddr = destNativeBuffer.getMemoryAddress(),
832-
j = 0; j < sliceLength; srcAddr += stepInBytes, destAddr += typeWidth, j++) {
833-
unsafe.copyMemory(srcAddr, destAddr, typeWidth);
829+
var stepInBytes = step * itemSize;
830+
for (long srcAddr = storage.getValueBufferAddr() + (start * itemSize), destAddr = toAddr,
831+
j = 0; j < sliceLength; srcAddr += stepInBytes, destAddr += itemSize, j++) {
832+
nativeContext.copyMemory(srcAddr, destAddr, itemSize);
834833
}
835834

836-
return destNativeBuffer;
835+
return toAddr;
837836
}
838837

839838
@NeverDefault
@@ -1223,7 +1222,7 @@ protected static void doInt(@SuppressWarnings("unused") Node inliningTarget, Int
12231222
}
12241223

12251224
@Specialization
1226-
protected static void doArrowInt(@SuppressWarnings("unused") Node inliningTarget, IntArrowSequenceStorage storage, int idx, int value) {
1225+
protected static void doNativeInt(@SuppressWarnings("unused") Node inliningTarget, NativeIntSequenceStorage storage, int idx, int value) {
12271226
storage.setIntItemNormalized(idx, value);
12281227
}
12291228

@@ -1560,19 +1559,19 @@ static void doObjectStorage(ObjectSequenceStorage storage) {
15601559
}
15611560

15621561
@Specialization
1563-
static void doArrow(Node inliningTarget, ArrowSequenceStorage storage) {
1562+
static void doNativePrimitive(Node inliningTarget, NativePrimitiveSequenceStorage storage) {
15641563
var length = storage.length();
15651564
var unsafe = PythonContext.get(inliningTarget).getUnsafe();
1566-
long typeWidth = storage.getTypeWidth();
1567-
long startAddress = storage.getNativeBuffer().getMemoryAddress();
1568-
long endAddress = startAddress + ((length - 1) * typeWidth);
1569-
byte[] tempBuffer = new byte[(int) typeWidth];
1565+
long itemSize = storage.getItemSize();
1566+
long startAddress = storage.getValueBufferAddr();
1567+
long endAddress = startAddress + ((length - 1) * itemSize);
1568+
byte[] tempBuffer = new byte[(int) itemSize];
15701569
while (startAddress < endAddress) {
1571-
unsafe.copyMemory(null, startAddress, tempBuffer, Unsafe.ARRAY_BYTE_BASE_OFFSET, typeWidth);
1572-
unsafe.copyMemory(endAddress, startAddress, typeWidth);
1573-
unsafe.copyMemory(tempBuffer, Unsafe.ARRAY_BYTE_BASE_OFFSET, null, endAddress, typeWidth);
1574-
startAddress += typeWidth;
1575-
endAddress -= typeWidth;
1570+
unsafe.copyMemory(null, startAddress, tempBuffer, Unsafe.ARRAY_BYTE_BASE_OFFSET, itemSize);
1571+
unsafe.copyMemory(endAddress, startAddress, itemSize);
1572+
unsafe.copyMemory(tempBuffer, Unsafe.ARRAY_BYTE_BASE_OFFSET, null, endAddress, itemSize);
1573+
startAddress += itemSize;
1574+
endAddress -= itemSize;
15761575
}
15771576
}
15781577

@@ -3145,15 +3144,14 @@ static void doObject(ObjectSequenceStorage storage, int cap) {
31453144
}
31463145

31473146
@Specialization
3148-
static void doArrow(Node inliningTarget, ArrowSequenceStorage storage, int cap) {
3147+
static void doNativePrimitive(Node inliningTarget, NativePrimitiveSequenceStorage storage, int cap) {
31493148
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, cap > storage.getCapacity())) {
3150-
var context = getContext(inliningTarget);
3151-
long newCapacityInBytes = storage.getTypeWidth() * cap;
3152-
var oldNativeBuffer = storage.getNativeBuffer();
3153-
var newNativeBuffer = context.nativeBufferContext.createNativeBuffer(newCapacityInBytes);
3154-
context.getUnsafe().copyMemory(oldNativeBuffer.getMemoryAddress(), newNativeBuffer.getMemoryAddress(), oldNativeBuffer.getCapacityInBytes());
3155-
storage.setCapacity(cap);
3156-
storage.setNativeBuffer(newNativeBuffer);
3149+
var nativeContext = getContext(inliningTarget).nativeBufferContext;
3150+
long newCapacityInBytes = storage.getItemSize() * cap;
3151+
var oldAddr = storage.getValueBufferAddr();
3152+
var newAddr = nativeContext.allocateNativeMemory(newCapacityInBytes);
3153+
nativeContext.copyMemory(oldAddr, newAddr, storage.getCapacityInBytes());
3154+
nativeContext.setNewValueAddrToStorage(storage, newAddr, newCapacityInBytes);
31573155
}
31583156
}
31593157

@@ -3298,9 +3296,13 @@ static SequenceStorage doMro(MroSequenceStorage storage) {
32983296
}
32993297

33003298
@Specialization
3301-
static SequenceStorage doArrowInt(Node inliningTarget, IntArrowSequenceStorage storage) {
3302-
var copiedBuffer = copyNativeBuffer(inliningTarget, storage.getNativeBuffer());
3303-
return new IntArrowSequenceStorage(copiedBuffer, storage.length());
3299+
static SequenceStorage doNativeInt(Node inliningTarget, NativeIntSequenceStorage storage) {
3300+
var nativeContext = PythonContext.get(inliningTarget).getContext().nativeBufferContext;
3301+
var capacityInBytes = storage.getCapacityInBytes();
3302+
var toAddr = nativeContext.allocateNativeMemory(capacityInBytes);
3303+
nativeContext.copyMemory(storage.getValueBufferAddr(), toAddr, capacityInBytes);
3304+
3305+
return nativeContext.wrapToIntStorage(toAddr, capacityInBytes, storage.length());
33043306
}
33053307

33063308
@Specialization
@@ -3322,16 +3324,6 @@ static SequenceStorage doNativeObjects(NativeObjectSequenceStorage s,
33223324
}
33233325
return new ObjectSequenceStorage(objects);
33243326
}
3325-
3326-
private static NativeBuffer copyNativeBuffer(Node inliningTarget, NativeBuffer buffer) {
3327-
var context = PythonContext.get(inliningTarget);
3328-
var unsafe = context.getUnsafe();
3329-
var newBuffer = context.nativeBufferContext.createNativeBuffer(buffer.getCapacityInBytes());
3330-
3331-
unsafe.copyMemory(buffer.getMemoryAddress(), newBuffer.getMemoryAddress(), buffer.getCapacityInBytes());
3332-
3333-
return newBuffer;
3334-
}
33353327
}
33363328

33373329
@GenerateUncached
@@ -3422,7 +3414,7 @@ static void doNative(NativeSequenceStorage s, int len,
34223414
}
34233415

34243416
@Specialization
3425-
static void doArrow(ArrowSequenceStorage s, int len) {
3417+
static void doNativePrimitive(NativePrimitiveSequenceStorage s, int len) {
34263418
s.setNewLength(len);
34273419
}
34283420
}
@@ -3957,17 +3949,17 @@ static SequenceStorage doArrayBasedStorage(Node inliningTarget, ArrayBasedSequen
39573949

39583950
// TODO introduce something similar to InsertItemArrayBasedStorageNode
39593951
@Specialization
3960-
static SequenceStorage doArrowInt(Node inliningTarget, IntArrowSequenceStorage storage, int index, int value,
3952+
static SequenceStorage doNativeInt(Node inliningTarget, NativeIntSequenceStorage storage, int index, int value,
39613953
@Exclusive @Cached EnsureCapacityNode ensureCapacity) {
39623954
int length = storage.length();
39633955
var context = PythonContext.get(inliningTarget);
39643956
var unsafe = context.getUnsafe();
3965-
long typeWidth = storage.getTypeWidth();
3957+
long itemSize = storage.getItemSize();
39663958
ensureCapacity.execute(inliningTarget, storage, length + 1);
39673959
// shifting tail to the right by one slot
3968-
long startAddr = storage.getNativeBuffer().getMemoryAddress() + (index * typeWidth);
3969-
long endAddr = startAddr + typeWidth;
3970-
long sizeInBytes = (length - index) * typeWidth;
3960+
long startAddr = storage.getValueBufferAddr() + (index * itemSize);
3961+
long endAddr = startAddr + itemSize;
3962+
long sizeInBytes = (length - index) * itemSize;
39713963
unsafe.copyMemory(startAddr, endAddr, sizeInBytes);
39723964

39733965
storage.setIntItemNormalized(index, value);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/PBytecodeRootNode.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@
208208
import com.oracle.graal.python.runtime.sequence.storage.LongSequenceStorage;
209209
import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
210210
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
211-
import com.oracle.graal.python.runtime.sequence.storage.native2.IntArrowSequenceStorage;
212211
import com.oracle.graal.python.util.PythonUtils;
213212
import com.oracle.truffle.api.Assumption;
214213
import com.oracle.truffle.api.CompilerAsserts;
@@ -5037,13 +5036,12 @@ private void bytecodeLoadConstCollection(VirtualFrame virtualFrame, int stackTop
50375036
assert kind == CollectionBits.KIND_LIST || kind == CollectionBits.KIND_TUPLE;
50385037
boolean list = kind == CollectionBits.KIND_LIST;
50395038
var context = PythonContext.get(this);
5040-
boolean useNativeStorageStrategy = context.getLanguage().getEngineOption(PythonOptions.UseNativeStorageStrategy);
5039+
boolean useNativePrimitiveStorage = context.getLanguage().getEngineOption(PythonOptions.UseNativePrimitiveStorageStrategy);
50415040
switch (CollectionBits.elementType(typeAndKind)) {
50425041
case CollectionBits.ELEMENT_INT: {
50435042
int[] a = (int[]) array;
5044-
if (useNativeStorageStrategy) {
5045-
var nativeBuffer = context.nativeBufferContext.toNativeBuffer(a);
5046-
storage = new IntArrowSequenceStorage(nativeBuffer, a.length);
5043+
if (useNativePrimitiveStorage) {
5044+
storage = context.nativeBufferContext.toNativeIntStorage(a);
50475045
} else {
50485046
if (list) {
50495047
a = PythonUtils.arrayCopyOf(a, a.length);
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@
3838
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3939
* SOFTWARE.
4040
*/
41-
package com.oracle.graal.python.runtime.sequence.storage.native2;
41+
package com.oracle.graal.python.nodes.util;
4242

4343
import com.oracle.graal.python.runtime.PythonContext;
4444
import com.oracle.graal.python.runtime.sequence.storage.IntSequenceStorage;
4545
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
46+
import com.oracle.graal.python.runtime.sequence.storage.NativePrimitiveSequenceStorage;
4647
import com.oracle.truffle.api.dsl.GenerateCached;
4748
import com.oracle.truffle.api.dsl.GenerateInline;
4849
import com.oracle.truffle.api.dsl.GenerateUncached;
@@ -52,20 +53,18 @@
5253
@GenerateInline
5354
@GenerateCached(false)
5455
@GenerateUncached
55-
public abstract class ToArrowStorageNode extends Node {
56+
public abstract class ToNativePrimitiveStorageNode extends Node {
5657

57-
public abstract ArrowSequenceStorage execute(Node inliningTarget, SequenceStorage storage);
58+
public abstract NativePrimitiveSequenceStorage execute(Node inliningTarget, SequenceStorage storage);
5859

59-
public static ArrowSequenceStorage executeUncached(SequenceStorage storage) {
60-
return ToArrowStorageNodeGen.getUncached().execute(null, storage);
60+
public static NativePrimitiveSequenceStorage executeUncached(SequenceStorage storage) {
61+
return ToNativePrimitiveStorageNodeGen.getUncached().execute(null, storage);
6162
}
6263

6364
@Specialization
64-
static ArrowSequenceStorage doInt(Node inliningTarget, IntSequenceStorage storage) {
65+
static NativePrimitiveSequenceStorage doInt(Node inliningTarget, IntSequenceStorage storage) {
6566
int[] arr = storage.getInternalIntArray();
66-
var buffer = PythonContext.get(inliningTarget).nativeBufferContext.toNativeBuffer(arr);
67-
68-
return new IntArrowSequenceStorage(buffer, storage.length());
67+
return PythonContext.get(inliningTarget).nativeBufferContext.toNativeIntStorage(arr);
6968
}
7069

7170
}

0 commit comments

Comments
 (0)