Skip to content

Commit 88d9f85

Browse files
committed
Removed GetSliceInBound from Array Based storages
1 parent 6c2624c commit 88d9f85

File tree

12 files changed

+306
-140
lines changed

12 files changed

+306
-140
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 SequenceStorageTests {
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 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 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 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 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 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.getInternalArray()[i]);
165+
}
166+
}
167+
168+
@Test
169+
public void boolGetSlice() {
170+
var storage = new RootNode(null) {
171+
@Child 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+
}

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/runtime/SequenceStorageTests.java

Lines changed: 1 addition & 21 deletions
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.
@@ -45,16 +45,6 @@ public void objectsGetAndSet() {
4545
assertEquals(10, store.getItemNormalized(5));
4646
}
4747

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-
5848
@Test
5949
public void objectsInsert() {
6050
ObjectSequenceStorage store = new ObjectSequenceStorage(getObjectValues());
@@ -79,16 +69,6 @@ public void intGetAndSet() throws SequenceStoreException {
7969
assertEquals(10, store.getItemNormalized(5));
8070
}
8171

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-
9272
@Test
9373
public void intInsert() throws SequenceStoreException {
9474
IntSequenceStorage store = new IntSequenceStorage(getIntValues());

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

Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
import com.oracle.graal.python.builtins.objects.slice.SliceNodes.CoerceToIntSlice;
8989
import com.oracle.graal.python.builtins.objects.slice.SliceNodes.ComputeIndices;
9090
import com.oracle.graal.python.builtins.objects.str.PString;
91+
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
9192
import com.oracle.graal.python.lib.GetNextNode;
9293
import com.oracle.graal.python.lib.PyIndexCheckNode;
9394
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
@@ -586,10 +587,123 @@ protected static EmptySequenceStorage doEmpty(EmptySequenceStorage storage, int
586587
return EmptySequenceStorage.INSTANCE;
587588
}
588589

589-
@Specialization(limit = "MAX_BASIC_STORAGES", guards = {"storage.getClass() == cachedClass"})
590-
protected static SequenceStorage doManagedStorage(BasicSequenceStorage storage, int start, int stop, int step, int length,
591-
@Cached("storage.getClass()") Class<? extends BasicSequenceStorage> cachedClass) {
592-
return cachedClass.cast(storage).getSliceInBound(start, stop, step, length);
590+
@Specialization
591+
protected static SequenceStorage doIntSequenceStorage(IntSequenceStorage storage, int start, int stop, int step, int length) {
592+
int[] newArray = new int[length];
593+
int[] values = storage.getInternalIntArray();
594+
595+
if (step == 1) {
596+
PythonUtils.arraycopy(values, start, newArray, 0, length);
597+
return new IntSequenceStorage(newArray);
598+
}
599+
600+
for (int i = start, j = 0; j < length; i += step, j++) {
601+
newArray[j] = values[i];
602+
}
603+
604+
return new IntSequenceStorage(newArray);
605+
}
606+
607+
@Specialization
608+
protected static SequenceStorage doLongSequenceStorage(LongSequenceStorage storage, int start, int stop, int step, int length) {
609+
long[] newArray = new long[length];
610+
long[] values = storage.getInternalLongArray();
611+
612+
if (step == 1) {
613+
PythonUtils.arraycopy(values, start, newArray, 0, length);
614+
return new LongSequenceStorage(newArray);
615+
}
616+
617+
for (int i = start, j = 0; j < length; i += step, j++) {
618+
newArray[j] = values[i];
619+
}
620+
621+
return new LongSequenceStorage(newArray);
622+
}
623+
624+
@Specialization
625+
protected static SequenceStorage doDoubleSequenceStorage(DoubleSequenceStorage storage, int start, int stop, int step, int length) {
626+
double[] newArray = new double[length];
627+
double[] values = storage.getInternalDoubleArray();
628+
629+
if (step == 1) {
630+
PythonUtils.arraycopy(values, start, newArray, 0, length);
631+
return new DoubleSequenceStorage(newArray);
632+
}
633+
634+
for (int i = start, j = 0; j < length; i += step, j++) {
635+
newArray[j] = values[i];
636+
}
637+
638+
return new DoubleSequenceStorage(newArray);
639+
}
640+
641+
@Specialization
642+
protected static SequenceStorage doBoolSequenceStorage(BoolSequenceStorage storage, int start, int stop, int step, int length) {
643+
boolean[] newArray = new boolean[length];
644+
boolean[] values = storage.getInternalBoolArray();
645+
646+
if (step == 1) {
647+
PythonUtils.arraycopy(values, start, newArray, 0, length);
648+
return new BoolSequenceStorage(newArray);
649+
}
650+
651+
for (int i = start, j = 0; j < length; i += step, j++) {
652+
newArray[j] = values[i];
653+
}
654+
655+
return new BoolSequenceStorage(newArray);
656+
}
657+
658+
@Specialization
659+
protected static SequenceStorage doByteSequenceStorage(ByteSequenceStorage storage, int start, int stop, int step, int length) {
660+
byte[] newArray = new byte[length];
661+
byte[] values = storage.getInternalByteArray();
662+
663+
if (step == 1) {
664+
PythonUtils.arraycopy(values, start, newArray, 0, length);
665+
return new ByteSequenceStorage(newArray);
666+
}
667+
668+
for (int i = start, j = 0; j < length; i += step, j++) {
669+
newArray[j] = values[i];
670+
}
671+
672+
return new ByteSequenceStorage(newArray);
673+
}
674+
675+
@Specialization
676+
protected static SequenceStorage doObjectSequenceStorage(ObjectSequenceStorage storage, int start, int stop, int step, int length) {
677+
Object[] newArray = new Object[length];
678+
Object[] values = storage.getInternalArray();
679+
680+
if (step == 1) {
681+
PythonUtils.arraycopy(values, start, newArray, 0, length);
682+
return new ObjectSequenceStorage(newArray);
683+
}
684+
685+
for (int i = start, j = 0; j < length; i += step, j++) {
686+
newArray[j] = values[i];
687+
}
688+
689+
return new ObjectSequenceStorage(newArray);
690+
}
691+
692+
@Specialization
693+
protected static SequenceStorage doMroSequenceStorage(MroSequenceStorage storage, int start, int stop, int step, int length) {
694+
PythonAbstractClass[] newArray = new PythonAbstractClass[length];
695+
PythonAbstractClass[] values = storage.getInternalClassArray();
696+
697+
if (step == 1) {
698+
PythonUtils.arraycopy(values, start, newArray, 0, length);
699+
return new MroSequenceStorage(storage.getClassName(), newArray);
700+
}
701+
702+
for (int i = start, j = 0; j < length; i += step, j++) {
703+
newArray[j] = values[i];
704+
}
705+
706+
return new MroSequenceStorage(storage.getClassName(), newArray);
593707
}
594708

595709
@Specialization

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public abstract class BasicSequenceStorage extends SequenceStorage {
4040

4141
public abstract SequenceStorage copy();
4242

43-
public abstract SequenceStorage getSliceInBound(int start, int stop, int step, int len);
44-
4543
public abstract SequenceStorage generalizeFor(Object value, SequenceStorage other);
4644

4745
/**

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,6 @@ private void insertBoolItem(int idx, boolean value) {
130130
length++;
131131
}
132132

133-
@Override
134-
public SequenceStorage getSliceInBound(int start, int stop, int step, int sliceLength) {
135-
boolean[] newArray = new boolean[sliceLength];
136-
137-
if (step == 1) {
138-
PythonUtils.arraycopy(values, start, newArray, 0, sliceLength);
139-
return new BoolSequenceStorage(newArray);
140-
}
141-
142-
for (int i = start, j = 0; j < sliceLength; i += step, j++) {
143-
newArray[j] = values[i];
144-
}
145-
146-
return new BoolSequenceStorage(newArray);
147-
}
148-
149133
@Override
150134
public void reverse() {
151135
if (length > 0) {

0 commit comments

Comments
 (0)