Skip to content

Commit 7df0ccf

Browse files
committed
[GR-70364] Use boolean sequence storage, always preallocate sequence storage.
PullRequest: graalpython/4040
2 parents 24a4515 + 4ed0668 commit 7df0ccf

File tree

10 files changed

+51
-19
lines changed

10 files changed

+51
-19
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_sequence_strategies.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ def test_appending_doubles():
7272
check_strategy(l, "DoubleSequenceStorage")
7373

7474

75+
def test_appending_bools():
76+
l = list()
77+
for i in range(10):
78+
l.append(i * 0.1)
79+
assert l[5] == 0.5
80+
check_strategy(l, "DoubleSequenceStorage")
81+
82+
7583
def test_generator_int():
7684
l = [x for x in range(10)]
7785
assert l[5] == 5
@@ -84,11 +92,10 @@ def test_generator_double():
8492
check_strategy(l, "DoubleSequenceStorage")
8593

8694

87-
# GR-70364
88-
# def test_generator_bool():
89-
# l = [x >= 5 for x in range(10)]
90-
# assert l[5] == True
91-
# check_strategy(l, "BoolSequenceStorage")
95+
def test_generator_bool():
96+
l = [x >= 5 for x in range(10)]
97+
assert l[5] == True
98+
check_strategy(l, "BoolSequenceStorage")
9299

93100

94101
def test_literal_int():
@@ -103,6 +110,12 @@ def test_literal_double():
103110
check_strategy(l, "DoubleSequenceStorage")
104111

105112

113+
def test_literal_bool():
114+
l = [True, False, True]
115+
assert l[0]
116+
check_strategy(l, "BoolSequenceStorage")
117+
118+
106119
def test_literal_mixed():
107120
l = [1,2,3,4,0.5]
108121
assert l[4] == 0.5

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,8 +2825,6 @@ public GeneralizationNode create() {
28252825
}
28262826
};
28272827

2828-
private static final int DEFAULT_CAPACITY = 8;
2829-
28302828
@Override
28312829
public final SequenceStorage executeCached(SequenceStorage toGeneralize, Object indicationValue) {
28322830
return execute(this, toGeneralize, indicationValue);
@@ -2842,19 +2840,24 @@ static ObjectSequenceStorage doObject(@SuppressWarnings("unused") ObjectSequence
28422840
@Specialization
28432841
static SequenceStorage doEmptyStorage(Node inliningTarget, @SuppressWarnings("unused") EmptySequenceStorage s, ArrayBasedSequenceStorage other,
28442842
@Exclusive @Cached InlinedExactClassProfile otherProfile) {
2845-
return otherProfile.profile(inliningTarget, other).createEmpty(DEFAULT_CAPACITY);
2843+
return otherProfile.profile(inliningTarget, other).createEmpty(ArrayBasedSequenceStorage.DEFAULT_CAPACITY);
28462844
}
28472845

28482846
@Specialization
28492847
static ByteSequenceStorage doEmptyByte(@SuppressWarnings("unused") EmptySequenceStorage s, @SuppressWarnings("unused") byte val) {
2850-
return new ByteSequenceStorage(DEFAULT_CAPACITY);
2848+
return new ByteSequenceStorage();
28512849
}
28522850

28532851
@Specialization
28542852
static IntSequenceStorage doEmptyInteger(@SuppressWarnings("unused") EmptySequenceStorage s, @SuppressWarnings("unused") int val) {
28552853
return new IntSequenceStorage();
28562854
}
28572855

2856+
@Specialization
2857+
static BoolSequenceStorage doEmptyBoolean(@SuppressWarnings("unused") EmptySequenceStorage s, @SuppressWarnings("unused") boolean val) {
2858+
return new BoolSequenceStorage();
2859+
}
2860+
28582861
@Specialization
28592862
static LongSequenceStorage doEmptyLong(@SuppressWarnings("unused") EmptySequenceStorage s, @SuppressWarnings("unused") long val) {
28602863
return new LongSequenceStorage();
@@ -2871,7 +2874,7 @@ protected static boolean isKnownType(Object val) {
28712874

28722875
@Specialization(guards = "!isKnownType(val)")
28732876
static ObjectSequenceStorage doEmptyObject(@SuppressWarnings("unused") EmptySequenceStorage s, @SuppressWarnings("unused") Object val) {
2874-
return new ObjectSequenceStorage(DEFAULT_CAPACITY);
2877+
return new ObjectSequenceStorage();
28752878
}
28762879

28772880
@Specialization

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -45,6 +45,8 @@
4545

4646
public abstract class ArrayBasedSequenceStorage extends SequenceStorage {
4747

48+
public static final int DEFAULT_CAPACITY = 8;
49+
4850
public abstract Object getInternalArrayObject();
4951

5052
public abstract Object getCopyOfInternalArrayObject();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public final class BoolSequenceStorage extends ArrayBasedSequenceStorage {
3333
private boolean[] values;
3434

3535
public BoolSequenceStorage() {
36-
values = new boolean[]{};
36+
this.values = new boolean[DEFAULT_CAPACITY];
37+
this.capacity = values.length;
3738
}
3839

3940
public BoolSequenceStorage(boolean[] elements) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public final class ByteSequenceStorage extends ArrayBasedSequenceStorage {
4040

4141
private byte[] values;
4242

43+
public ByteSequenceStorage() {
44+
this.values = new byte[DEFAULT_CAPACITY];
45+
this.capacity = values.length;
46+
}
47+
4348
public ByteSequenceStorage(byte[] elements) {
4449
this(elements, elements.length);
4550
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public final class DoubleSequenceStorage extends ArrayBasedSequenceStorage {
3535
private double[] values;
3636

3737
public DoubleSequenceStorage() {
38-
values = new double[]{};
38+
this.values = new double[DEFAULT_CAPACITY];
39+
this.capacity = values.length;
3940
}
4041

4142
public DoubleSequenceStorage(double[] elements) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2024, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2025, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -36,9 +36,9 @@ public ArrayBasedSequenceStorage generalizeFor(Object value) {
3636
final ArrayBasedSequenceStorage generalized;
3737

3838
if (value instanceof Byte) {
39-
generalized = new ByteSequenceStorage(16);
39+
generalized = new ByteSequenceStorage();
4040
} else if (value instanceof Boolean) {
41-
generalized = new BoolSequenceStorage(16);
41+
generalized = new BoolSequenceStorage();
4242
} else if (value instanceof Integer) {
4343
generalized = new IntSequenceStorage();
4444
} else if (value instanceof Long) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public final class IntSequenceStorage extends ArrayBasedSequenceStorage {
3535
private int[] values;
3636

3737
public IntSequenceStorage() {
38-
values = new int[]{};
38+
this.values = new int[DEFAULT_CAPACITY];
39+
this.capacity = values.length;
3940
}
4041

4142
public IntSequenceStorage(int[] elements) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public final class LongSequenceStorage extends ArrayBasedSequenceStorage {
3535
private long[] values;
3636

3737
public LongSequenceStorage() {
38-
values = new long[]{};
38+
this.values = new long[DEFAULT_CAPACITY];
39+
this.capacity = values.length;
3940
}
4041

4142
public LongSequenceStorage(long[] elements) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2024, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2025, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -37,6 +37,11 @@ public final class ObjectSequenceStorage extends ArrayBasedSequenceStorage {
3737

3838
private Object[] values;
3939

40+
public ObjectSequenceStorage() {
41+
this.values = new Object[DEFAULT_CAPACITY];
42+
this.capacity = values.length;
43+
}
44+
4045
public ObjectSequenceStorage(Object[] elements) {
4146
this.values = elements;
4247
this.capacity = elements.length;

0 commit comments

Comments
 (0)