Skip to content

Commit 77cd2cf

Browse files
committed
Moving reverse and insert method back to the storage. They are no longer abstract
1 parent 0510937 commit 77cd2cf

File tree

9 files changed

+140
-183
lines changed

9 files changed

+140
-183
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/json/JSONScannerBuiltins.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ private Object parseObjectUnicode(PJSONScanner scanner, String string, int start
162162
idx = nextIdx.value;
163163

164164
if (hasPairsHook) {
165-
var index = listStorage.length();
166-
var value = factory.createTuple(PythonBuiltinClassType.PTuple, tupleInstanceShape, new Object[]{key, val});
167-
listStorage = (ObjectSequenceStorage) InsertItemArrayBasedStorageNode.executeUncached(listStorage, index, value);
165+
listStorage.insertItem(listStorage.length(), factory.createTuple(PythonBuiltinClassType.PTuple, tupleInstanceShape, new Object[]{key, val}));
168166
} else {
169167
HashingStorage newStorage = HashingStorageSetItem.executeUncached(mapStorage, key, val);
170168
assert newStorage == mapStorage;
@@ -221,7 +219,7 @@ private Object parseArrayUnicode(PJSONScanner scanner, String string, int start,
221219

222220
/* read any JSON term */
223221
Object val = scanOnceUnicode(scanner, string, idx, nextIdx);
224-
storage = (ObjectSequenceStorage) InsertItemArrayBasedStorageNode.executeUncached(storage, storage.length(), val);
222+
storage.insertItem(storage.length(), val);
225223
idx = nextIdx.value;
226224

227225
/* skip whitespace between term and , */

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

Lines changed: 30 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,104 +1425,32 @@ static void doEmpty(@SuppressWarnings("unused") EmptySequenceStorage storage) {
14251425

14261426
@Specialization
14271427
static void doIntStorage(IntSequenceStorage storage) {
1428-
final int length = storage.length();
1429-
final int[] values = storage.getInternalIntArray();
1430-
if (length > 0) {
1431-
int head = 0;
1432-
int tail = length - 1;
1433-
int middle = (length - 1) / 2;
1434-
1435-
for (; head <= middle; head++, tail--) {
1436-
int temp = values[head];
1437-
values[head] = values[tail];
1438-
values[tail] = temp;
1439-
}
1440-
}
1428+
storage.reverse();
14411429
}
14421430

14431431
@Specialization
14441432
static void doDoubleStorage(DoubleSequenceStorage storage) {
1445-
final int length = storage.length();
1446-
final double[] values = storage.getInternalDoubleArray();
1447-
if (length > 0) {
1448-
int head = 0;
1449-
int tail = length - 1;
1450-
int middle = (length - 1) / 2;
1451-
1452-
for (; head <= middle; head++, tail--) {
1453-
double temp = values[head];
1454-
values[head] = values[tail];
1455-
values[tail] = temp;
1456-
}
1457-
}
1433+
storage.reverse();
14581434
}
14591435

14601436
@Specialization
14611437
static void doLongStorage(LongSequenceStorage storage) {
1462-
final int length = storage.length();
1463-
final long[] values = storage.getInternalLongArray();
1464-
if (length > 0) {
1465-
int head = 0;
1466-
int tail = length - 1;
1467-
int middle = (length - 1) / 2;
1468-
1469-
for (; head <= middle; head++, tail--) {
1470-
long temp = values[head];
1471-
values[head] = values[tail];
1472-
values[tail] = temp;
1473-
}
1474-
}
1438+
storage.reverse();
14751439
}
14761440

14771441
@Specialization
14781442
static void doByteStorage(ByteSequenceStorage storage) {
1479-
final int length = storage.length();
1480-
final byte[] values = storage.getInternalByteArray();
1481-
if (length > 0) {
1482-
int head = 0;
1483-
int tail = length - 1;
1484-
int middle = (length - 1) / 2;
1485-
1486-
for (; head <= middle; head++, tail--) {
1487-
byte temp = values[head];
1488-
values[head] = values[tail];
1489-
values[tail] = temp;
1490-
}
1491-
}
1443+
storage.reverse();
14921444
}
14931445

14941446
@Specialization
14951447
static void doObjectStorage(ObjectSequenceStorage storage) {
1496-
final int length = storage.length();
1497-
final Object[] values = storage.getInternalArray();
1498-
if (length > 0) {
1499-
int head = 0;
1500-
int tail = length - 1;
1501-
int middle = (length - 1) / 2;
1502-
1503-
for (; head <= middle; head++, tail--) {
1504-
Object temp = values[head];
1505-
values[head] = values[tail];
1506-
values[tail] = temp;
1507-
}
1508-
}
1448+
storage.reverse();
15091449
}
15101450

15111451
@Specialization
15121452
static void doBoolStorage(BoolSequenceStorage storage) {
1513-
final int length = storage.length();
1514-
final boolean[] values = storage.getInternalBoolArray();
1515-
if (length > 0) {
1516-
int head = 0;
1517-
int tail = length - 1;
1518-
int middle = (length - 1) / 2;
1519-
1520-
for (; head <= middle; head++, tail--) {
1521-
boolean temp = values[head];
1522-
values[head] = values[tail];
1523-
values[tail] = temp;
1524-
}
1525-
}
1453+
storage.reverse();
15261454
}
15271455

15281456
@Specialization
@@ -3182,7 +3110,6 @@ private static int computeNewCapacity(int cap) {
31823110
@GenerateUncached
31833111
@GenerateInline
31843112
@GenerateCached(false)
3185-
@ImportStatic(SequenceStorageBaseNode.class)
31863113
public abstract static class CopyNode extends Node {
31873114

31883115
public abstract SequenceStorage execute(Node node, SequenceStorage s);
@@ -3223,7 +3150,7 @@ static SequenceStorage doBoolean(BoolSequenceStorage storage) {
32233150

32243151
@Specialization
32253152
static SequenceStorage doObject(ObjectSequenceStorage storage) {
3226-
return new ObjectSequenceStorage(PythonUtils.arrayCopyOf(storage.getCopyOfInternalArray(), storage.length()));
3153+
return new ObjectSequenceStorage(PythonUtils.arrayCopyOf(storage.getInternalArray(), storage.length()));
32273154
}
32283155

32293156
@Specialization
@@ -3738,100 +3665,56 @@ public final SequenceStorage executeCached(ArrayBasedSequenceStorage storage, in
37383665
}
37393666

37403667
@Specialization
3741-
static SequenceStorage doIntStorage(Node inliningTarget, IntSequenceStorage storage, int idx, int value,
3742-
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3743-
final int length = storage.length();
3744-
ensureCapacityNode.execute(inliningTarget, storage, length + 1);
3745-
final int[] values = storage.getInternalIntArray();
3746-
3747-
// shifting tail to the right by one slot
3748-
for (int i = values.length - 1; i > idx; i--) {
3749-
values[i] = values[i - 1];
3750-
}
3751-
3752-
values[idx] = value;
3753-
storage.incLength();
3668+
static SequenceStorage doIntStorage(IntSequenceStorage storage, int idx, int value) {
3669+
storage.insertIntItem(idx, value);
37543670
return storage;
37553671
}
37563672

37573673
@Specialization
3758-
static SequenceStorage doDoubleStorage(Node inliningTarget, DoubleSequenceStorage storage, int idx, double value,
3759-
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3760-
final int length = storage.length();
3761-
ensureCapacityNode.execute(inliningTarget, storage, length + 1);
3762-
final double[] values = storage.getInternalDoubleArray();
3763-
3764-
// shifting tail to the right by one slot
3765-
for (int i = values.length - 1; i > idx; i--) {
3766-
values[i] = values[i - 1];
3767-
}
3768-
3769-
values[idx] = value;
3770-
storage.incLength();
3674+
static SequenceStorage doDoubleStorage(DoubleSequenceStorage storage, int idx, double value) {
3675+
storage.insertDoubleItem(idx, value);
37713676
return storage;
37723677
}
37733678

37743679
@Specialization
3775-
static SequenceStorage doLongWithLongStorage(Node inliningTarget, LongSequenceStorage storage, int idx, long value,
3776-
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3777-
return insertLong(inliningTarget, storage, idx, value, ensureCapacityNode);
3680+
static SequenceStorage doLongWithLongStorage(LongSequenceStorage storage, int idx, long value) {
3681+
storage.insertLongItem(idx, value);
3682+
return storage;
37783683
}
37793684

37803685
@Specialization
3781-
static SequenceStorage doIntWithLongStorage(Node inliningTarget, LongSequenceStorage storage, int idx, int value,
3782-
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3783-
return insertLong(inliningTarget, storage, idx, value, ensureCapacityNode);
3686+
static SequenceStorage doIntWithLongStorage(LongSequenceStorage storage, int idx, int value) {
3687+
storage.insertLongItem(idx, value);
3688+
return storage;
37843689
}
37853690

37863691
@Specialization
3787-
static SequenceStorage doBigIntWithLongStorage(Node inliningTarget, LongSequenceStorage storage, int idx, BigInteger value,
3788-
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3789-
return insertLong(inliningTarget, storage, idx, PInt.longValue(value), ensureCapacityNode);
3692+
static SequenceStorage doBigIntWithLongStorage(LongSequenceStorage storage, int idx, BigInteger value) {
3693+
storage.insertLongItem(idx, PInt.longValue(value));
3694+
return storage;
37903695
}
37913696

37923697
@Specialization
3793-
static SequenceStorage doByteWithByteStorage(Node inliningTarget, ByteSequenceStorage storage, int idx, byte value,
3794-
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3795-
return insertByte(inliningTarget, storage, idx, value, ensureCapacityNode);
3698+
static SequenceStorage doByteWithByteStorage(ByteSequenceStorage storage, int idx, byte value) {
3699+
storage.insertByteItem(idx, value);
3700+
return storage;
37963701
}
37973702

37983703
@Specialization
3799-
static SequenceStorage doIntWithByteStorage(Node inliningTarget, ByteSequenceStorage storage, int idx, int value,
3800-
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3801-
return insertByte(inliningTarget, storage, idx, (byte) value, ensureCapacityNode);
3704+
static SequenceStorage doIntWithByteStorage(ByteSequenceStorage storage, int idx, int value) {
3705+
storage.insertByteItem(idx, (byte) value);
3706+
return storage;
38023707
}
38033708

38043709
@Specialization
3805-
static SequenceStorage doBoolStorage(Node inliningTarget, BoolSequenceStorage storage, int idx, boolean value,
3806-
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3807-
final int length = storage.length();
3808-
ensureCapacityNode.execute(inliningTarget, storage, length + 1);
3809-
final boolean[] values = storage.getInternalBoolArray();
3810-
3811-
// shifting tail to the right by one slot
3812-
for (int i = values.length - 1; i > idx; i--) {
3813-
values[i] = values[i - 1];
3814-
}
3815-
3816-
values[idx] = value;
3817-
storage.incLength();
3710+
static SequenceStorage doBoolStorage(BoolSequenceStorage storage, int idx, boolean value) {
3711+
storage.insertBoolItem(idx, value);
38183712
return storage;
38193713
}
38203714

38213715
@Specialization
3822-
static SequenceStorage doObjectStorage(Node inliningTarget, ObjectSequenceStorage storage, int idx, Object value,
3823-
@Shared @Cached EnsureCapacityNode ensureCapacityNode) {
3824-
final int length = storage.length();
3825-
ensureCapacityNode.execute(inliningTarget, storage, length + 1);
3826-
final Object[] values = storage.getInternalArray();
3827-
3828-
// shifting tail to the right by one slot
3829-
for (int i = values.length - 1; i > idx; i--) {
3830-
values[i] = values[i - 1];
3831-
}
3832-
3833-
values[idx] = assertNoJavaString(value);
3834-
storage.incLength();
3716+
static SequenceStorage doObjectStorage(ObjectSequenceStorage storage, int idx, Object value) {
3717+
storage.insertItem(idx, value);
38353718
return storage;
38363719
}
38373720

@@ -3846,36 +3729,6 @@ static SequenceStorage doGeneralization(ArrayBasedSequenceStorage storage, int i
38463729
ObjectSequenceStorage newStorage = storage.generalize();
38473730
return recursiveNode.executeCached(newStorage, idx, value);
38483731
}
3849-
3850-
private static SequenceStorage insertLong(Node inliningTarget, LongSequenceStorage storage, int idx, long value, EnsureCapacityNode ensureCapacityNode) {
3851-
final int length = storage.length();
3852-
ensureCapacityNode.execute(inliningTarget, storage, length + 1);
3853-
final long[] values = storage.getInternalLongArray();
3854-
3855-
// shifting tail to the right by one slot
3856-
for (int i = values.length - 1; i > idx; i--) {
3857-
values[i] = values[i - 1];
3858-
}
3859-
3860-
values[idx] = value;
3861-
storage.incLength();
3862-
return storage;
3863-
}
3864-
3865-
private static SequenceStorage insertByte(Node inliningTarget, ByteSequenceStorage storage, int idx, byte value, EnsureCapacityNode ensureCapacityNode) {
3866-
final int length = storage.length();
3867-
ensureCapacityNode.execute(inliningTarget, storage, length + 1);
3868-
final byte[] values = storage.getInternalByteArray();
3869-
3870-
// shifting tail to the right by one slot
3871-
for (int i = values.length - 1; i > idx; i--) {
3872-
values[i] = values[i - 1];
3873-
}
3874-
3875-
values[idx] = value;
3876-
storage.incLength();
3877-
return storage;
3878-
}
38793732
}
38803733

38813734
@GenerateUncached

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
package com.oracle.graal.python.runtime.sequence.storage;
2727

2828
import com.oracle.truffle.api.CompilerAsserts;
29-
import com.oracle.truffle.api.CompilerDirectives;
3029

3130
public abstract class BasicSequenceStorage extends SequenceStorage {
3231

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,32 @@ public Object[] getInternalArray() {
8383
return boxed;
8484
}
8585

86+
public void insertBoolItem(int idx, boolean value) {
87+
ensureCapacity(length + 1);
88+
89+
// shifting tail to the right by one slot
90+
for (int i = values.length - 1; i > idx; i--) {
91+
values[i] = values[i - 1];
92+
}
93+
94+
values[idx] = value;
95+
length++;
96+
}
97+
98+
public void reverse() {
99+
if (length > 0) {
100+
int head = 0;
101+
int tail = length - 1;
102+
int middle = (length - 1) / 2;
103+
104+
for (; head <= middle; head++, tail--) {
105+
boolean temp = values[head];
106+
values[head] = values[tail];
107+
values[tail] = temp;
108+
}
109+
}
110+
}
111+
86112
public boolean[] getInternalBoolArray() {
87113
return values;
88114
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ public Object[] getInternalArray() {
9191
return boxed;
9292
}
9393

94+
public void reverse() {
95+
if (length > 0) {
96+
int head = 0;
97+
int tail = length - 1;
98+
int middle = (length - 1) / 2;
99+
100+
for (; head <= middle; head++, tail--) {
101+
byte temp = values[head];
102+
values[head] = values[tail];
103+
values[tail] = temp;
104+
}
105+
}
106+
}
107+
94108
@Override
95109
public Object getItemNormalized(int idx) {
96110
return getIntItemNormalized(idx);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ public Object[] getInternalArray() {
8585
return boxed;
8686
}
8787

88+
public void reverse() {
89+
if (length > 0) {
90+
int head = 0;
91+
int tail = length - 1;
92+
int middle = (length - 1) / 2;
93+
94+
for (; head <= middle; head++, tail--) {
95+
double temp = values[head];
96+
values[head] = values[tail];
97+
values[tail] = temp;
98+
}
99+
}
100+
}
101+
88102
public double[] getInternalDoubleArray() {
89103
return values;
90104
}

0 commit comments

Comments
 (0)