Skip to content

Commit 4f9ea9e

Browse files
committed
Feat: Adding Array Based Storage abstraction
1 parent c358bc0 commit 4f9ea9e

File tree

12 files changed

+102
-54
lines changed

12 files changed

+102
-54
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
import com.oracle.graal.python.runtime.exception.PythonErrorType;
125125
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
126126
import com.oracle.graal.python.runtime.sequence.PSequence;
127-
import com.oracle.graal.python.runtime.sequence.storage.BasicSequenceStorage;
127+
import com.oracle.graal.python.runtime.sequence.storage.ArrayBasedSequenceStorage;
128128
import com.oracle.graal.python.runtime.sequence.storage.EmptySequenceStorage;
129129
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
130130
import com.oracle.graal.python.util.PythonUtils;
@@ -1158,7 +1158,7 @@ static Object doSequence(PSequence seq,
11581158
Object arrayObject;
11591159
if (storage instanceof EmptySequenceStorage) {
11601160
arrayObject = seq instanceof PBytesLike ? EMPTY_BYTE_ARRAY : EMPTY_OBJECT_ARRAY;
1161-
} else if (storage instanceof BasicSequenceStorage basicStorage) {
1161+
} else if (storage instanceof ArrayBasedSequenceStorage basicStorage) {
11621162
arrayObject = basicStorage.getInternalArrayObject();
11631163
} else {
11641164
throw PRaiseNode.raiseUncached(inliningTarget, PythonBuiltinClassType.NotImplementedError, ErrorMessages.GETTING_POLYGLOT_STORAGE_FOR_NATIVE_STORAGE_NOT_IMPLEMENTED);

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

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
import com.oracle.graal.python.runtime.exception.PException;
110110
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
111111
import com.oracle.graal.python.runtime.sequence.PSequence;
112+
import com.oracle.graal.python.runtime.sequence.storage.ArrayBasedSequenceStorage;
112113
import com.oracle.graal.python.runtime.sequence.storage.BasicSequenceStorage;
113114
import com.oracle.graal.python.runtime.sequence.storage.BoolSequenceStorage;
114115
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
@@ -1180,8 +1181,8 @@ protected static void nothing(SequenceStorage storage, int distPos, int srcPos,
11801181
}
11811182

11821183
@Specialization(limit = "MAX_BASIC_STORAGES", guards = {"length > 0", "storage.getClass() == cachedClass"})
1183-
protected static void doMove(BasicSequenceStorage storage, int distPos, int srcPos, int length,
1184-
@Cached("storage.getClass()") Class<? extends BasicSequenceStorage> cachedClass) {
1184+
protected static void doArrayBasedMove(ArrayBasedSequenceStorage storage, int distPos, int srcPos, int length,
1185+
@Cached("storage.getClass()") Class<? extends ArrayBasedSequenceStorage> cachedClass) {
11851186
Object array = cachedClass.cast(storage).getInternalArrayObject();
11861187
PythonUtils.arraycopy(array, srcPos, array, distPos, length);
11871188
}
@@ -1214,8 +1215,8 @@ protected static void nothing(SequenceStorage dist, int distPos, SequenceStorage
12141215
}
12151216

12161217
@Specialization(limit = "MAX_BASIC_STORAGES", guards = {"length > 0", "dist.getClass() == cachedClass", "src.getClass() == dist.getClass()"})
1217-
protected static void doCopy(BasicSequenceStorage dist, int distPos, BasicSequenceStorage src, int srcPos, int length,
1218-
@Cached("dist.getClass()") Class<? extends BasicSequenceStorage> cachedClass) {
1218+
protected static void doArrayBasedCopy(ArrayBasedSequenceStorage dist, int distPos, ArrayBasedSequenceStorage src, int srcPos, int length,
1219+
@Cached("dist.getClass()") Class<? extends ArrayBasedSequenceStorage> cachedClass) {
12191220
Object distArray = cachedClass.cast(dist).getInternalArrayObject();
12201221
Object srcArray = cachedClass.cast(src).getInternalArrayObject();
12211222
PythonUtils.arraycopy(srcArray, srcPos, distArray, distPos, length);
@@ -1314,10 +1315,10 @@ abstract static class SetStorageSliceNode extends Node {
13141315
public abstract void execute(SequenceStorage s, SliceInfo info, SequenceStorage iterable, boolean canGeneralize);
13151316

13161317
@Specialization(limit = "MAX_BASIC_STORAGES", guards = {"self.getClass() == cachedClass", "self.getClass() == sequence.getClass()", "replacesWholeSequence(cachedClass, self, info)"})
1317-
static void doWholeSequence(BasicSequenceStorage self, @SuppressWarnings("unused") SliceInfo info, BasicSequenceStorage sequence, @SuppressWarnings("unused") boolean canGeneralize,
1318-
@Cached("self.getClass()") Class<? extends BasicSequenceStorage> cachedClass) {
1319-
BasicSequenceStorage selfProfiled = cachedClass.cast(self);
1320-
BasicSequenceStorage otherProfiled = cachedClass.cast(sequence);
1318+
static void doWholeSequence(ArrayBasedSequenceStorage self, @SuppressWarnings("unused") SliceInfo info, ArrayBasedSequenceStorage sequence, @SuppressWarnings("unused") boolean canGeneralize,
1319+
@Cached("self.getClass()") Class<? extends ArrayBasedSequenceStorage> cachedClass) {
1320+
ArrayBasedSequenceStorage selfProfiled = cachedClass.cast(self);
1321+
ArrayBasedSequenceStorage otherProfiled = cachedClass.cast(sequence);
13211322
selfProfiled.setInternalArrayObject(otherProfiled.getCopyOfInternalArrayObject());
13221323
selfProfiled.setNewLength(otherProfiled.length());
13231324
selfProfiled.minimizeCapacity();
@@ -1835,12 +1836,12 @@ static SequenceStorage doRightEmpty(@SuppressWarnings("unused") EmptySequenceSto
18351836
}
18361837

18371838
@Specialization(guards = {"dest == left", "left.getClass() == right.getClass()", "cachedClass == left.getClass()"}, limit = "1")
1838-
static SequenceStorage doManagedManagedSameTypeInplace(@SuppressWarnings("unused") BasicSequenceStorage dest, BasicSequenceStorage left, BasicSequenceStorage right,
1839+
static SequenceStorage doArrayBasedManagedManagedSameTypeInplace(@SuppressWarnings("unused") ArrayBasedSequenceStorage dest, ArrayBasedSequenceStorage left, ArrayBasedSequenceStorage right,
18391840
@Bind("this") Node inliningTarget,
1840-
@Cached("left.getClass()") Class<? extends BasicSequenceStorage> cachedClass,
1841+
@Cached("left.getClass()") Class<? extends ArrayBasedSequenceStorage> cachedClass,
18411842
@Shared @Cached SetLenNode setLenNode) {
1842-
BasicSequenceStorage leftProfiled = cachedClass.cast(left);
1843-
BasicSequenceStorage rightProfiled = cachedClass.cast(right);
1843+
ArrayBasedSequenceStorage leftProfiled = cachedClass.cast(left);
1844+
ArrayBasedSequenceStorage rightProfiled = cachedClass.cast(right);
18441845
Object arr1 = leftProfiled.getInternalArrayObject();
18451846
int len1 = leftProfiled.length();
18461847
Object arr2 = rightProfiled.getInternalArrayObject();
@@ -1851,13 +1852,13 @@ static SequenceStorage doManagedManagedSameTypeInplace(@SuppressWarnings("unused
18511852
}
18521853

18531854
@Specialization(guards = {"dest != left", "dest.getClass() == left.getClass()", "left.getClass() == right.getClass()", "cachedClass == dest.getClass()"}, limit = "1")
1854-
static SequenceStorage doManagedManagedSameType(BasicSequenceStorage dest, BasicSequenceStorage left, BasicSequenceStorage right,
1855+
static SequenceStorage doArrayBasedManagedManagedSameType(ArrayBasedSequenceStorage dest, ArrayBasedSequenceStorage left, ArrayBasedSequenceStorage right,
18551856
@Bind("this") Node inliningTarget,
1856-
@Cached("left.getClass()") Class<? extends BasicSequenceStorage> cachedClass,
1857+
@Cached("left.getClass()") Class<? extends ArrayBasedSequenceStorage> cachedClass,
18571858
@Shared @Cached SetLenNode setLenNode) {
1858-
BasicSequenceStorage destProfiled = cachedClass.cast(dest);
1859-
BasicSequenceStorage leftProfiled = cachedClass.cast(left);
1860-
BasicSequenceStorage rightProfiled = cachedClass.cast(right);
1859+
ArrayBasedSequenceStorage destProfiled = cachedClass.cast(dest);
1860+
ArrayBasedSequenceStorage leftProfiled = cachedClass.cast(left);
1861+
ArrayBasedSequenceStorage rightProfiled = cachedClass.cast(right);
18611862
Object arr1 = leftProfiled.getInternalArrayObject();
18621863
int len1 = leftProfiled.length();
18631864
Object arr2 = rightProfiled.getInternalArrayObject();
@@ -1868,12 +1869,12 @@ static SequenceStorage doManagedManagedSameType(BasicSequenceStorage dest, Basic
18681869
}
18691870

18701871
@Specialization(guards = {"dest.getClass() == right.getClass()", "cachedClass == dest.getClass()"}, limit = "1")
1871-
static SequenceStorage doEmptyManagedSameType(BasicSequenceStorage dest, @SuppressWarnings("unused") EmptySequenceStorage left, BasicSequenceStorage right,
1872+
static SequenceStorage doArrayBasedEmptyManagedSameType(ArrayBasedSequenceStorage dest, @SuppressWarnings("unused") EmptySequenceStorage left, ArrayBasedSequenceStorage right,
18721873
@Bind("this") Node inliningTarget,
1873-
@Cached("dest.getClass()") Class<? extends BasicSequenceStorage> cachedClass,
1874+
@Cached("dest.getClass()") Class<? extends ArrayBasedSequenceStorage> cachedClass,
18741875
@Shared @Cached SetLenNode setLenNode) {
1875-
BasicSequenceStorage destProfiled = cachedClass.cast(dest);
1876-
BasicSequenceStorage rightProfiled = cachedClass.cast(right);
1876+
ArrayBasedSequenceStorage destProfiled = cachedClass.cast(dest);
1877+
ArrayBasedSequenceStorage rightProfiled = cachedClass.cast(right);
18771878
Object arr2 = rightProfiled.getInternalArrayObject();
18781879
int len2 = rightProfiled.length();
18791880
PythonUtils.arraycopy(arr2, 0, destProfiled.getInternalArrayObject(), 0, len2);
@@ -1882,12 +1883,12 @@ static SequenceStorage doEmptyManagedSameType(BasicSequenceStorage dest, @Suppre
18821883
}
18831884

18841885
@Specialization(guards = {"dest.getClass() == left.getClass()", "cachedClass == dest.getClass()"}, limit = "1")
1885-
static SequenceStorage doManagedEmptySameType(BasicSequenceStorage dest, BasicSequenceStorage left, @SuppressWarnings("unused") EmptySequenceStorage right,
1886+
static SequenceStorage doArrayBasedManagedEmptySameType(ArrayBasedSequenceStorage dest, ArrayBasedSequenceStorage left, @SuppressWarnings("unused") EmptySequenceStorage right,
18861887
@Bind("this") Node inliningTarget,
1887-
@Cached("left.getClass()") Class<? extends BasicSequenceStorage> cachedClass,
1888+
@Cached("left.getClass()") Class<? extends ArrayBasedSequenceStorage> cachedClass,
18881889
@Shared @Cached SetLenNode setLenNode) {
1889-
BasicSequenceStorage destProfiled = cachedClass.cast(dest);
1890-
BasicSequenceStorage leftProfiled = cachedClass.cast(left);
1890+
ArrayBasedSequenceStorage destProfiled = cachedClass.cast(dest);
1891+
ArrayBasedSequenceStorage leftProfiled = cachedClass.cast(left);
18911892
Object arr1 = leftProfiled.getInternalArrayObject();
18921893
int len1 = leftProfiled.length();
18931894
PythonUtils.arraycopy(arr1, 0, destProfiled.getInternalArrayObject(), 0, len1);
@@ -2259,15 +2260,15 @@ ObjectSequenceStorage doObjectSingleElement(ObjectSequenceStorage s, int times,
22592260
}
22602261

22612262
@Specialization(limit = "MAX_BASIC_STORAGES", guards = {"times > 0", "!isNative(s)", "s.getClass() == cachedClass"})
2262-
SequenceStorage doManaged(BasicSequenceStorage s, int times,
2263+
SequenceStorage doArrayBasedManaged(ArrayBasedSequenceStorage s, int times,
22632264
@Shared @Cached PRaiseNode raiseNode,
2264-
@Cached("s.getClass()") Class<? extends BasicSequenceStorage> cachedClass) {
2265+
@Cached("s.getClass()") Class<? extends ArrayBasedSequenceStorage> cachedClass) {
22652266
try {
2266-
BasicSequenceStorage profiled = cachedClass.cast(s);
2267+
ArrayBasedSequenceStorage profiled = cachedClass.cast(s);
22672268
Object arr1 = profiled.getInternalArrayObject();
22682269
int len = profiled.length();
22692270
int newLength = PythonUtils.multiplyExact(len, times);
2270-
BasicSequenceStorage repeated = profiled.createEmpty(newLength);
2271+
ArrayBasedSequenceStorage repeated = profiled.createEmpty(newLength);
22712272
Object destArr = repeated.getInternalArrayObject();
22722273
repeat(destArr, arr1, len, times);
22732274
repeated.setNewLength(newLength);
@@ -2279,7 +2280,7 @@ SequenceStorage doManaged(BasicSequenceStorage s, int times,
22792280
}
22802281
}
22812282

2282-
@Specialization(replaces = "doManaged", guards = "times > 0")
2283+
@Specialization(replaces = "doArrayBasedManaged", guards = "times > 0")
22832284
@SuppressWarnings("truffle-static-method")
22842285
SequenceStorage doGeneric(SequenceStorage s, int times,
22852286
@Bind("this") Node inliningTarget,
@@ -2935,8 +2936,8 @@ public static Object[] executeUncached(SequenceStorage s) {
29352936
}
29362937

29372938
@Specialization(limit = "MAX_BASIC_STORAGES", guards = "s.getClass() == cachedClass")
2938-
static Object[] doTyped(BasicSequenceStorage s,
2939-
@Cached("s.getClass()") Class<? extends BasicSequenceStorage> cachedClass) {
2939+
static Object[] doArrayBased(ArrayBasedSequenceStorage s,
2940+
@Cached("s.getClass()") Class<? extends ArrayBasedSequenceStorage> cachedClass) {
29402941
return cachedClass.cast(s).getCopyOfInternalArray();
29412942
}
29422943

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.runtime.sequence.storage;
42+
43+
public abstract class ArrayBasedSequenceStorage extends BasicSequenceStorage {
44+
45+
public abstract Object[] getInternalArray();
46+
47+
public abstract Object[] getCopyOfInternalArray();
48+
49+
public abstract Object getInternalArrayObject();
50+
51+
public abstract Object getCopyOfInternalArrayObject();
52+
53+
public abstract void setInternalArrayObject(Object arrayObject);
54+
55+
public abstract ArrayBasedSequenceStorage createEmpty(int newCapacity);
56+
57+
}

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,10 @@ public abstract class BasicSequenceStorage extends SequenceStorage {
4444

4545
public abstract SequenceStorage generalizeFor(Object value, SequenceStorage other);
4646

47-
public abstract Object[] getInternalArray();
48-
49-
public abstract Object[] getCopyOfInternalArray();
50-
5147
/**
5248
* Get internal array object without copying. Note: The length must be taken from the sequence
5349
* storage object.
5450
*/
55-
public abstract Object getInternalArrayObject();
56-
57-
public abstract Object getCopyOfInternalArrayObject();
58-
59-
public abstract void setInternalArrayObject(Object arrayObject);
60-
6151
public final void setNewLength(int length) {
6252
this.length = length;
6353
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public SequenceStorage copy() {
6565
}
6666

6767
@Override
68-
public BasicSequenceStorage createEmpty(int newLength) {
68+
public BoolSequenceStorage createEmpty(int newLength) {
6969
return new BoolSequenceStorage(newLength);
7070
}
7171

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public SequenceStorage copy() {
7171
}
7272

7373
@Override
74-
public BasicSequenceStorage createEmpty(int newCapacity) {
74+
public ByteSequenceStorage createEmpty(int newCapacity) {
7575
return new ByteSequenceStorage(newCapacity);
7676
}
7777

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public SequenceStorage copy() {
6767
}
6868

6969
@Override
70-
public BasicSequenceStorage createEmpty(int newCapacity) {
70+
public ArrayBasedSequenceStorage createEmpty(int newCapacity) {
7171
return new DoubleSequenceStorage(newCapacity);
7272
}
7373

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public SequenceStorage copy() {
6767
}
6868

6969
@Override
70-
public BasicSequenceStorage createEmpty(int newCapacity) {
70+
public IntSequenceStorage createEmpty(int newCapacity) {
7171
return new IntSequenceStorage(newCapacity);
7272
}
7373

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public SequenceStorage copy() {
6969
}
7070

7171
@Override
72-
public BasicSequenceStorage createEmpty(int newCapacity) {
72+
public LongSequenceStorage createEmpty(int newCapacity) {
7373
return new LongSequenceStorage(newCapacity);
7474
}
7575

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public SequenceStorage copy() {
147147
}
148148

149149
@Override
150-
public BasicSequenceStorage createEmpty(int newCapacity) {
150+
public MroSequenceStorage createEmpty(int newCapacity) {
151151
return new MroSequenceStorage(getClassName(), newCapacity);
152152
}
153153

0 commit comments

Comments
 (0)