Skip to content

Commit 2a22e2f

Browse files
committed
[GR-40015] Move ArrayHelpers.{setStoreAndSize,setStore) to RubyArray
PullRequest: truffleruby/4512
2 parents 5b1bb31 + a5fd4cd commit 2a22e2f

8 files changed

+44
-57
lines changed

src/main/java/org/truffleruby/core/array/ArrayAppendManyNode.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
*/
1010
package org.truffleruby.core.array;
1111

12-
import static org.truffleruby.core.array.ArrayHelpers.setSize;
13-
import static org.truffleruby.core.array.ArrayHelpers.setStoreAndSize;
14-
1512
import org.truffleruby.core.array.library.ArrayStoreLibrary;
1613
import org.truffleruby.language.RubyBaseNode;
1714

@@ -54,10 +51,10 @@ RubyArray appendManySameType(RubyArray array, RubyArray other,
5451
final int capacity = ArrayUtils.capacity(getLanguage(), length, newSize);
5552
Object newStore = stores.expand(store, capacity);
5653
otherStores.copyContents(otherStore, 0, newStore, oldSize, otherSize);
57-
setStoreAndSize(array, newStore, newSize);
54+
array.setStoreAndSize(newStore, newSize);
5855
} else {
5956
otherStores.copyContents(otherStore, 0, store, oldSize, otherSize);
60-
setSize(array, newSize);
57+
array.setSize(newSize);
6158
}
6259
return array;
6360
}
@@ -79,7 +76,7 @@ RubyArray appendManyGeneralize(RubyArray array, RubyArray other,
7976

8077
stores.copyContents(store, 0, newStore, 0, oldSize);
8178
otherStores.copyContents(otherStore, 0, newStore, oldSize, otherSize);
82-
setStoreAndSize(array, newStore, newSize);
79+
array.setStoreAndSize(newStore, newSize);
8380
return array;
8481
}
8582
}

src/main/java/org/truffleruby/core/array/ArrayAppendOneNode.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
*/
1010
package org.truffleruby.core.array;
1111

12-
import static org.truffleruby.core.array.ArrayHelpers.setSize;
13-
import static org.truffleruby.core.array.ArrayHelpers.setStoreAndSize;
14-
1512
import com.oracle.truffle.api.dsl.NeverDefault;
1613
import com.oracle.truffle.api.dsl.ReportPolymorphism;
1714
import com.oracle.truffle.api.profiles.CountingConditionProfile;
@@ -61,10 +58,10 @@ RubyArray appendOneSameType(RubyArray array, Object value,
6158
final int capacity = ArrayUtils.capacityForOneMore(getLanguage(), length);
6259
final Object newStore = stores.expand(store, capacity);
6360
stores.write(newStore, oldSize, value);
64-
setStoreAndSize(array, newStore, newSize);
61+
array.setStoreAndSize(newStore, newSize);
6562
} else {
6663
stores.write(store, oldSize, value);
67-
setSize(array, newSize);
64+
array.setSize(newSize);
6865
}
6966
return array;
7067
}
@@ -82,7 +79,7 @@ RubyArray appendOneToEmptyArray(RubyArray array, Object value,
8279
final int newCapacity = ArrayUtils.capacityForOneMore(getLanguage(), 0);
8380
final Object newStore = currentStores.allocateForNewValue(currentStore, value, newCapacity);
8481
newStores.write(newStore, 0, value);
85-
setStoreAndSize(array, newStore, 1);
82+
array.setStoreAndSize(newStore, 1);
8683
return array;
8784
}
8885

@@ -106,7 +103,7 @@ RubyArray appendOneGeneralizeNonMutable(RubyArray array, Object value,
106103
final Object newStore = currentStores.allocateForNewValue(currentStore, value, newCapacity);
107104
currentStores.copyContents(currentStore, 0, newStore, 0, oldSize);
108105
newStores.write(newStore, oldSize, value);
109-
setStoreAndSize(array, newStore, newSize);
106+
array.setStoreAndSize(newStore, newSize);
110107
return array;
111108
}
112109

src/main/java/org/truffleruby/core/array/ArrayHelpers.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,6 @@
1616

1717
public abstract class ArrayHelpers {
1818

19-
public static void setStoreAndSize(RubyArray array, Object store, int size) {
20-
assert (store instanceof SharedArrayStorage) == (array.getShape().isShared());
21-
array.setStore(store);
22-
setSize(array, size);
23-
}
24-
25-
/** Sets the size of the given array
26-
*
27-
* Asserts that the size is valid for the current store of the array. If setting both size and store, use
28-
* setStoreAndSize or be sure to setStore before setSize as this assertion may fail. */
29-
public static void setSize(RubyArray array, int size) {
30-
assert ArrayOperations.getStoreCapacity(array) >= size;
31-
array.size = size;
32-
}
33-
3419
private static boolean assertValidElements(Object store, int size) {
3520
return !(store instanceof Object[]) || ArrayUtils.assertValidElements((Object[]) store, 0, size);
3621
}

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
*/
1010
package org.truffleruby.core.array;
1111

12-
import static org.truffleruby.core.array.ArrayHelpers.setSize;
13-
import static org.truffleruby.core.array.ArrayHelpers.setStoreAndSize;
1412
import static org.truffleruby.language.dispatch.DispatchConfiguration.PUBLIC;
1513

1614
import java.util.Arrays;
@@ -473,7 +471,7 @@ public abstract static class ClearNode extends ArrayCoreMethodNode {
473471
@Specialization
474472
RubyArray clear(RubyArray array,
475473
@Cached IsSharedNode isSharedNode) {
476-
setStoreAndSize(array,
474+
array.setStoreAndSize(
477475
ArrayStoreLibrary.initialStorage(isSharedNode.execute(this, array)),
478476
0);
479477
return array;
@@ -571,7 +569,7 @@ Object compactObjects(RubyArray array,
571569

572570
stores.clear(oldStore, m, size - m);
573571

574-
setStoreAndSize(array, newStore, m);
572+
array.setStoreAndSize(newStore, m);
575573

576574
if (m == size) {
577575
return nil;
@@ -738,7 +736,7 @@ private Object delete(RubyArray array, Object value, Object maybeBlock,
738736
if (sameStores) {
739737
oldStores.clear(oldStore, i, size - i);
740738
}
741-
setStoreAndSize(array, newStore, i);
739+
array.setStoreAndSize(newStore, i);
742740
return found;
743741
} else {
744742
if (maybeBlock == nil) {
@@ -780,14 +778,14 @@ static Object doDelete(RubyArray array, Object indexObject,
780778
final Object value = stores.read(store, i);
781779
stores.copyContents(store, i + 1, store, i, size - i - 1);
782780
stores.clear(store, size - 1, 1);
783-
setStoreAndSize(array, store, size - 1);
781+
array.setStoreAndSize(store, size - 1);
784782
return value;
785783
} else {
786784
final Object mutableStore = stores.allocator(store).allocate(size - 1);
787785
stores.copyContents(store, 0, mutableStore, 0, i);
788786
final Object value = stores.read(store, i);
789787
stores.copyContents(store, i + 1, mutableStore, i, size - i - 1);
790-
setStoreAndSize(array, mutableStore, size - 1);
788+
array.setStoreAndSize(mutableStore, size - 1);
791789
return value;
792790
}
793791
}
@@ -1121,7 +1119,7 @@ protected abstract RubyArray executeInitialize(RubyArray array, Object size, Obj
11211119
@Specialization
11221120
RubyArray initializeNoArgs(RubyArray array, NotProvided size, NotProvided fillingValue, Nil block,
11231121
@Cached @Shared IsSharedNode isSharedNode) {
1124-
setStoreAndSize(array,
1122+
array.setStoreAndSize(
11251123
ArrayStoreLibrary.initialStorage(isSharedNode.execute(this, array)),
11261124
0);
11271125
return array;
@@ -1136,7 +1134,7 @@ RubyArray initializeOnlyBlock(RubyArray array, NotProvided size, NotProvided fil
11361134
warningNode.warningMessage(sourceSection, "given block not used");
11371135
}
11381136

1139-
setStoreAndSize(array,
1137+
array.setStoreAndSize(
11401138
ArrayStoreLibrary.initialStorage(isSharedNode.execute(this, array)),
11411139
0);
11421140
return array;
@@ -1174,7 +1172,7 @@ RubyArray initializeWithSizeNoValue(RubyArray array, int size, NotProvided filli
11741172
store = new Object[size];
11751173
}
11761174
stores.fill(store, 0, size, nil);
1177-
setStoreAndSize(array, store, size);
1175+
array.setStoreAndSize(store, size);
11781176
return array;
11791177
}
11801178

@@ -1199,7 +1197,7 @@ RubyArray initializeWithSizeAndValue(RubyArray array, int size, Object fillingVa
11991197
profileAndReportLoopCount(loopProfile, i);
12001198
}
12011199
}
1202-
setStoreAndSize(array, allocatedStore, size);
1200+
array.setStoreAndSize(allocatedStore, size);
12031201
return array;
12041202
}
12051203

@@ -1236,7 +1234,7 @@ static Object initializeBlock(RubyArray array, int size, Object unusedFillingVal
12361234
if (isSharedNode.execute(node, array)) {
12371235
store = stores.makeShared(store, n);
12381236
}
1239-
setStoreAndSize(array, store, n);
1237+
array.setStoreAndSize(store, n);
12401238
}
12411239

12421240
return array;
@@ -1688,7 +1686,7 @@ RubyArray popNotEmptySharedStorage(RubyArray array, int n,
16881686
final Object prefix = stores.extractRange(store, 0, size - numPop); // copy on write
16891687
// NOTE(norswap): if one of these two arrays outlives the other, you get a memory leak
16901688

1691-
setStoreAndSize(array, prefix, size - numPop);
1689+
array.setStoreAndSize(prefix, size - numPop);
16921690
return createArray(popped, numPop);
16931691
}
16941692

@@ -1709,7 +1707,7 @@ RubyArray popNotEmptyUnsharedStorage(RubyArray array, int n,
17091707

17101708
// Remove the end from the original array.
17111709
stores.clear(store, size - numPop, numPop);
1712-
setSize(array, size - numPop);
1710+
array.setSize(size - numPop);
17131711

17141712
return createArray(popped, numPop);
17151713
}
@@ -1837,7 +1835,7 @@ RubyArray replace(RubyArray array, Object otherObject,
18371835
if (isSharedNode.execute(this, array)) {
18381836
store = stores.makeShared(store, size);
18391837
}
1840-
setStoreAndSize(array, store, size);
1838+
array.setStoreAndSize(store, size);
18411839
return array;
18421840
}
18431841

@@ -1922,7 +1920,7 @@ RubyArray rotateStorageNotMutable(RubyArray array, int rotation,
19221920

19231921
final Object rotated = stores.allocator(store).allocate(size);
19241922
rotateArrayCopy(rotation, size, stores, store, rotated);
1925-
setStoreAndSize(array, rotated, size);
1923+
array.setStoreAndSize(rotated, size);
19261924
return array;
19271925
}
19281926

@@ -2042,7 +2040,7 @@ Object shiftOther(RubyArray array, NotProvided n,
20422040
final int size = array.size;
20432041
final Object value = stores.read(store, 0);
20442042
stores.clear(store, 0, 1);
2045-
setStoreAndSize(array, stores.extractRange(store, 1, size), size - 1);
2043+
array.setStoreAndSize(stores.extractRange(store, 1, size), size - 1);
20462044
return value;
20472045
}
20482046

@@ -2074,7 +2072,7 @@ Object shiftMany(RubyArray array, int n,
20742072
final int numShift = minProfile.profile(size < n) ? size : n;
20752073
final Object result = stores.extractRangeAndUnshare(store, 0, numShift);
20762074
final Object newStore = stores.extractRange(store, numShift, size);
2077-
setStoreAndSize(array, newStore, size - numShift);
2075+
array.setStoreAndSize(newStore, size - numShift);
20782076
return createArray(result, numShift);
20792077
}
20802078

@@ -2238,8 +2236,8 @@ static RubyArray stealStorage(RubyArray array, RubyArray other,
22382236

22392237
final int size = other.size;
22402238
final Object store = other.getStore();
2241-
setStoreAndSize(array, store, size);
2242-
setStoreAndSize(other, stores.initialStore(store), 0);
2239+
array.setStoreAndSize(store, size);
2240+
other.setStoreAndSize(stores.initialStore(store), 0);
22432241

22442242
return array;
22452243
}

src/main/java/org/truffleruby/core/array/ArrayPopOneNode.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
*/
1010
package org.truffleruby.core.array;
1111

12-
import static org.truffleruby.core.array.ArrayHelpers.setSize;
13-
1412
import org.truffleruby.core.array.library.ArrayStoreLibrary;
1513
import org.truffleruby.language.RubyBaseNode;
1614

@@ -40,7 +38,7 @@ Object popOne(RubyArray array,
4038
final int size = array.size;
4139
final Object value = stores.read(store, size - 1);
4240
stores.clear(store, size - 1, 1);
43-
setSize(array, size - 1);
41+
array.setSize(size - 1);
4442
return value;
4543
}
4644

src/main/java/org/truffleruby/core/array/ArrayTruncateNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void truncateCopy(RubyArray array, int size,
5050

5151
final Object newStore = stores.allocateForNewStore(store, store, size);
5252
stores.copyContents(store, 0, newStore, 0, size);
53-
ArrayHelpers.setStoreAndSize(array, newStore, size);
53+
array.setStoreAndSize(newStore, size);
5454
}
5555

5656
@ReportPolymorphism.Exclude

src/main/java/org/truffleruby/core/array/ArrayWriteNormalizedNode.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
*/
1010
package org.truffleruby.core.array;
1111

12-
import static org.truffleruby.core.array.ArrayHelpers.setSize;
13-
import static org.truffleruby.core.array.ArrayHelpers.setStoreAndSize;
14-
1512
import com.oracle.truffle.api.TruffleSafepoint;
1613
import com.oracle.truffle.api.profiles.LoopConditionProfile;
1714
import org.truffleruby.core.array.library.ArrayStoreLibrary;
@@ -98,7 +95,7 @@ Object writeBeyondPrimitive(RubyArray array, int index, Object value,
9895
profileAndReportLoopCount(loopProfile, n - oldSize);
9996
}
10097
newStores.write(objectStore, index, value);
101-
setStoreAndSize(array, objectStore, newSize);
98+
array.setStoreAndSize(objectStore, newSize);
10299
return value;
103100
}
104101

@@ -125,7 +122,7 @@ Object writeBeyondObject(RubyArray array, int index, Object value,
125122
profileAndReportLoopCount(loopProfile, n - array.size);
126123
}
127124
newStores.write(newStore, index, value);
128-
setSize(array, index + 1);
125+
array.setSize(index + 1);
129126
return value;
130127
}
131128

src/main/java/org/truffleruby/core/array/RubyArray.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ public void setStore(Object store) {
5858
this.store = store;
5959
}
6060

61+
public void setStoreAndSize(Object store, int size) {
62+
assert (store instanceof SharedArrayStorage) == (this.getShape().isShared());
63+
this.setStore(store);
64+
setSize(size);
65+
}
66+
67+
/** Sets the size of the given array
68+
*
69+
* Asserts that the size is valid for the current store of the array. If setting both size and store, use
70+
* {@link #setStoreAndSize} or be sure to {@link #setStore} before {@link #setSize} as this assertion may fail. */
71+
public void setSize(int size) {
72+
assert ArrayOperations.getStoreCapacity(this) >= size;
73+
this.size = size;
74+
}
75+
6176
@TruffleBoundary
6277
public void getAdjacentObjects(Set<Object> reachable) {
6378
ObjectGraph.addProperty(reachable, store);

0 commit comments

Comments
 (0)