Skip to content

Commit 10facc9

Browse files
committed
[GR-54490] Storage strategy hierarchy redesign
PullRequest: graalpython/3334
2 parents 32d84e2 + 18e95ca commit 10facc9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+944
-877
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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.test.nodes.storage;
42+
43+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetItemSliceNode;
44+
import com.oracle.graal.python.runtime.sequence.storage.BoolSequenceStorage;
45+
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
46+
import com.oracle.graal.python.runtime.sequence.storage.DoubleSequenceStorage;
47+
import com.oracle.graal.python.runtime.sequence.storage.IntSequenceStorage;
48+
import com.oracle.graal.python.runtime.sequence.storage.LongSequenceStorage;
49+
import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
50+
import com.oracle.graal.python.test.PythonTests;
51+
import com.oracle.truffle.api.frame.VirtualFrame;
52+
import com.oracle.truffle.api.nodes.RootNode;
53+
import org.junit.After;
54+
import org.junit.Before;
55+
import org.junit.Test;
56+
57+
import static org.junit.Assert.assertEquals;
58+
import static org.junit.Assert.assertFalse;
59+
import static org.junit.Assert.assertTrue;
60+
61+
public class GetItemSliceNodeTests {
62+
63+
@Before
64+
public void setUp() {
65+
PythonTests.enterContext();
66+
}
67+
68+
@After
69+
public void tearDown() {
70+
PythonTests.closeContext();
71+
}
72+
73+
@Test
74+
public void intGetSlice() {
75+
var storage = new RootNode(null) {
76+
@Child @SuppressWarnings("FieldMayBeFinal") private GetItemSliceNode getItemSliceNode = GetItemSliceNode.create();
77+
78+
@Override
79+
public Object execute(VirtualFrame frame) {
80+
var intStorage = new IntSequenceStorage(new int[]{1, 2, 3, 4, 5, 6});
81+
return getItemSliceNode.execute(intStorage, 1, 4, 1, 3);
82+
}
83+
}.getCallTarget().call();
84+
85+
assertEquals(IntSequenceStorage.class, storage.getClass());
86+
var intStorage = (IntSequenceStorage) storage;
87+
for (int i = 0; i < 3; i++) {
88+
assertEquals(i + 2, intStorage.getInternalIntArray()[i]);
89+
}
90+
}
91+
92+
@Test
93+
public void longGetSlice() {
94+
var storage = new RootNode(null) {
95+
@Child @SuppressWarnings("FieldMayBeFinal") private GetItemSliceNode getItemSliceNode = GetItemSliceNode.create();
96+
97+
@Override
98+
public Object execute(VirtualFrame frame) {
99+
var longStorage = new LongSequenceStorage(new long[]{1, 2, 3, 4, 5, 6});
100+
return getItemSliceNode.execute(longStorage, 1, 4, 1, 3);
101+
}
102+
}.getCallTarget().call();
103+
104+
assertEquals(LongSequenceStorage.class, storage.getClass());
105+
var longStorage = (LongSequenceStorage) storage;
106+
for (int i = 0; i < 3; i++) {
107+
assertEquals(i + 2, longStorage.getInternalLongArray()[i]);
108+
}
109+
}
110+
111+
@Test
112+
public void doubleGetSlice() {
113+
var storage = new RootNode(null) {
114+
@Child @SuppressWarnings("FieldMayBeFinal") private GetItemSliceNode getItemSliceNode = GetItemSliceNode.create();
115+
116+
@Override
117+
public Object execute(VirtualFrame frame) {
118+
var doubleStorage = new DoubleSequenceStorage(new double[]{1, 2, 3, 4, 5, 6});
119+
return getItemSliceNode.execute(doubleStorage, 1, 4, 1, 3);
120+
}
121+
}.getCallTarget().call();
122+
123+
assertEquals(DoubleSequenceStorage.class, storage.getClass());
124+
var doubleStorage = (DoubleSequenceStorage) storage;
125+
for (int i = 0; i < 3; i++) {
126+
assertEquals(i + 2, doubleStorage.getInternalDoubleArray()[i], 0);
127+
}
128+
}
129+
130+
@Test
131+
public void byteGetSlice() {
132+
var storage = new RootNode(null) {
133+
@Child @SuppressWarnings("FieldMayBeFinal") private GetItemSliceNode getItemSliceNode = GetItemSliceNode.create();
134+
135+
@Override
136+
public Object execute(VirtualFrame frame) {
137+
var byteStorage = new ByteSequenceStorage(new byte[]{1, 2, 3, 4, 5, 6});
138+
return getItemSliceNode.execute(byteStorage, 1, 4, 1, 3);
139+
}
140+
}.getCallTarget().call();
141+
142+
assertEquals(ByteSequenceStorage.class, storage.getClass());
143+
var byteStorage = (ByteSequenceStorage) storage;
144+
for (int i = 0; i < 3; i++) {
145+
assertEquals(i + 2, byteStorage.getInternalByteArray()[i]);
146+
}
147+
}
148+
149+
@Test
150+
public void objectGetSlice() {
151+
var storage = new RootNode(null) {
152+
@Child @SuppressWarnings("FieldMayBeFinal") private GetItemSliceNode getItemSliceNode = GetItemSliceNode.create();
153+
154+
@Override
155+
public Object execute(VirtualFrame frame) {
156+
var objectStorage = new ObjectSequenceStorage(new Object[]{1, 2, 3, 4, 5, 6});
157+
return getItemSliceNode.execute(objectStorage, 1, 4, 1, 3);
158+
}
159+
}.getCallTarget().call();
160+
161+
assertEquals(ObjectSequenceStorage.class, storage.getClass());
162+
var objectStorage = (ObjectSequenceStorage) storage;
163+
for (int i = 0; i < 3; i++) {
164+
assertEquals(i + 2, objectStorage.getInternalObjectArray()[i]);
165+
}
166+
}
167+
168+
@Test
169+
public void boolGetSlice() {
170+
var storage = new RootNode(null) {
171+
@Child @SuppressWarnings("FieldMayBeFinal") private GetItemSliceNode getItemSliceNode = GetItemSliceNode.create();
172+
173+
@Override
174+
public Object execute(VirtualFrame frame) {
175+
var boolStorage = new BoolSequenceStorage(new boolean[]{true, false, false, true, true, false});
176+
return getItemSliceNode.execute(boolStorage, 1, 4, 1, 3);
177+
}
178+
}.getCallTarget().call();
179+
180+
assertEquals(BoolSequenceStorage.class, storage.getClass());
181+
var boolArray = ((BoolSequenceStorage) storage).getInternalBoolArray();
182+
assertEquals(3, boolArray.length);
183+
assertFalse(boolArray[0]);
184+
assertFalse(boolArray[1]);
185+
assertTrue(boolArray[2]);
186+
}
187+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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.test.nodes.storage;
42+
43+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.InsertItemNode;
44+
import com.oracle.graal.python.runtime.sequence.storage.IntSequenceStorage;
45+
import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
46+
import com.oracle.graal.python.test.PythonTests;
47+
import com.oracle.truffle.api.frame.VirtualFrame;
48+
import com.oracle.truffle.api.nodes.RootNode;
49+
import org.junit.After;
50+
import org.junit.Before;
51+
import org.junit.Test;
52+
53+
import static org.junit.Assert.assertEquals;
54+
55+
public class InsertItemNodeTest {
56+
57+
@Before
58+
public void setUp() {
59+
PythonTests.enterContext();
60+
}
61+
62+
@After
63+
public void tearDown() {
64+
PythonTests.closeContext();
65+
}
66+
67+
@Test
68+
public void objectInsert() {
69+
var storage = new RootNode(null) {
70+
@Override
71+
public Object execute(VirtualFrame frame) {
72+
var objectStorage = new ObjectSequenceStorage(new Object[]{5, 6});
73+
return InsertItemNode.executeUncached(objectStorage, 2, 11);
74+
}
75+
}.getCallTarget().call();
76+
77+
assertEquals(ObjectSequenceStorage.class, storage.getClass());
78+
var objectStorage = ((ObjectSequenceStorage) storage).getInternalObjectArray();
79+
assertEquals(5, objectStorage[0]);
80+
assertEquals(6, objectStorage[1]);
81+
assertEquals(11, objectStorage[2]);
82+
}
83+
84+
@Test
85+
public void intInsert() {
86+
var storage = new RootNode(null) {
87+
@Override
88+
public Object execute(VirtualFrame frame) {
89+
var intStorage = new IntSequenceStorage(new int[]{5, 6});
90+
return InsertItemNode.executeUncached(intStorage, 2, 15);
91+
}
92+
}.getCallTarget().call();
93+
94+
assertEquals(IntSequenceStorage.class, storage.getClass());
95+
int[] intStorage = ((IntSequenceStorage) storage).getInternalIntArray();
96+
assertEquals(5, intStorage[0]);
97+
assertEquals(6, intStorage[1]);
98+
assertEquals(15, intStorage[2]);
99+
}
100+
101+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2024, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -40,28 +40,9 @@ private static Object[] getObjectValues() {
4040
@Test
4141
public void objectsGetAndSet() {
4242
ObjectSequenceStorage store = new ObjectSequenceStorage(getObjectValues());
43-
assertEquals(4, store.getItemNormalized(3));
44-
store.setItemNormalized(5, 10);
45-
assertEquals(10, store.getItemNormalized(5));
46-
}
47-
48-
@Test
49-
public void objectsGetSlice() {
50-
ObjectSequenceStorage store = new ObjectSequenceStorage(getObjectValues());
51-
ObjectSequenceStorage slice = store.getSliceInBound(1, 4, 1, 3);
52-
53-
for (int i = 0; i < 3; i++) {
54-
assertEquals(i + 2, slice.getItemNormalized(i));
55-
}
56-
}
57-
58-
@Test
59-
public void objectsInsert() {
60-
ObjectSequenceStorage store = new ObjectSequenceStorage(getObjectValues());
61-
store.insertItem(3, 42);
62-
assertEquals(42, store.getItemNormalized(3));
63-
assertEquals(6, store.getItemNormalized(6));
64-
assertEquals(7, store.length());
43+
assertEquals(4, store.getObjectItemNormalized(3));
44+
store.setObjectItemNormalized(5, 10);
45+
assertEquals(10, store.getObjectItemNormalized(5));
6546
}
6647

6748
/**
@@ -74,27 +55,8 @@ private static int[] getIntValues() {
7455
@Test
7556
public void intGetAndSet() throws SequenceStoreException {
7657
IntSequenceStorage store = new IntSequenceStorage(getIntValues());
77-
assertEquals(4, store.getItemNormalized(3));
78-
store.setItemNormalized(5, 10);
79-
assertEquals(10, store.getItemNormalized(5));
80-
}
81-
82-
@Test
83-
public void intGetSlice() {
84-
IntSequenceStorage store = new IntSequenceStorage(getIntValues());
85-
IntSequenceStorage slice = store.getSliceInBound(1, 4, 1, 3);
86-
87-
for (int i = 0; i < 3; i++) {
88-
assertEquals(i + 2, slice.getItemNormalized(i));
89-
}
90-
}
91-
92-
@Test
93-
public void intInsert() throws SequenceStoreException {
94-
IntSequenceStorage store = new IntSequenceStorage(getIntValues());
95-
store.insertItem(3, 42);
96-
assertEquals(42, store.getItemNormalized(3));
97-
assertEquals(6, store.getItemNormalized(6));
98-
assertEquals(7, store.length());
58+
assertEquals(4, store.getIntItemNormalized(3));
59+
store.setIntItemNormalized(5, 10);
60+
assertEquals(10, store.getIntItemNormalized(5));
9961
}
10062
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ Object forget(VirtualFrame frame, PBytesLike encoding,
10861086
@Cached AsciiDecodeNode asciiDecodeNode) {
10871087
PTuple decodeResult = (PTuple) asciiDecodeNode.execute(frame, encoding, PNone.NO_VALUE);
10881088
ObjectSequenceStorage resultStorage = (ObjectSequenceStorage) decodeResult.getSequenceStorage();
1089-
forget((TruffleString) resultStorage.getItemNormalized(0));
1089+
forget((TruffleString) resultStorage.getObjectItemNormalized(0));
10901090
return PNone.NONE;
10911091
}
10921092

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public abstract static class StatResultNode extends PythonTernaryBuiltinNode {
394394
public static PTuple generic(VirtualFrame frame, Object cls, Object sequence, Object dict,
395395
@Cached("create(STAT_RESULT_DESC)") StructSequence.NewNode newNode) {
396396
PTuple p = (PTuple) newNode.execute(frame, cls, sequence, dict);
397-
Object[] data = CompilerDirectives.castExact(p.getSequenceStorage(), ObjectSequenceStorage.class).getInternalArray();
397+
Object[] data = CompilerDirectives.castExact(p.getSequenceStorage(), ObjectSequenceStorage.class).getInternalObjectArray();
398398
for (int i = 7; i <= 9; i++) {
399399
if (data[i + 3] == PNone.NONE) {
400400
data[i + 3] = data[i];

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ private static Object[] collect(MroSequenceStorage mro, int idx) {
11031103
ArrayList<Object> l = new ArrayList<>();
11041104
int mroLength = mro.length();
11051105
for (int i = 0; i < mroLength; i++) {
1106-
PythonAbstractClass kls = mro.getItemNormalized(i);
1106+
PythonAbstractClass kls = mro.getPythonClassItemNormalized(i);
11071107
Object value = HiddenAttr.ReadNode.executeUncached((PythonAbstractObject) kls, NATIVE_SLOTS, null);
11081108
if (value != null) {
11091109
Object[] tuple = (Object[]) value;

0 commit comments

Comments
 (0)