Skip to content

Commit 60822c2

Browse files
committed
[GR-17457] Use unsharedAllocator() in places which do not reassign the RubyArray store
PullRequest: truffleruby/3459
2 parents 2031318 + c627343 commit 60822c2

File tree

5 files changed

+25
-28
lines changed

5 files changed

+25
-28
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ protected void appendCompatibleType(BuilderState state, int index, Object value,
202202
final int capacity = ArrayUtils.capacityForOneMore(getLanguage(), length);
203203
state.store = arrays.expand(state.store, capacity);
204204
state.capacity = capacity;
205-
replaceNodes(arrays.allocator(state.store), capacity);
205+
replaceNodes(arrays.unsharedAllocator(state.store), capacity);
206206
}
207207
arrays.write(state.store, index, value);
208208
state.nextIndex++;
@@ -264,7 +264,7 @@ protected void appendCompatibleStrategy(BuilderState state, int index, RubyArray
264264
int length = arrays.capacity(state.store);
265265
if (neededSize > length) {
266266
CompilerDirectives.transferToInterpreterAndInvalidate();
267-
replaceNodes(arrays.allocator(state.store), neededSize);
267+
replaceNodes(arrays.unsharedAllocator(state.store), neededSize);
268268
final int capacity = ArrayUtils.capacity(getLanguage(), length, neededSize);
269269
state.store = arrays.expand(state.store, capacity);
270270
state.capacity = capacity;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,7 @@ protected RubyArray rotate(RubyArray array, int rotation,
19301930
rotation = rotationProfile.profile(rotation);
19311931
assert 0 < rotation && rotation < size;
19321932

1933-
final Object rotated = stores.allocator(store).allocate(size);
1933+
final Object rotated = stores.unsharedAllocator(store).allocate(size);
19341934
rotateArrayCopy(rotation, size, stores, store, rotated);
19351935
return createArray(rotated, size);
19361936
}
@@ -2230,7 +2230,7 @@ protected Object sortPrimitiveArrayNoBlock(RubyArray array, Nil block,
22302230
@CachedLibrary(limit = "1") ArrayStoreLibrary mutableStores,
22312231
@Cached IntValueProfile arraySizeProfile) {
22322232
final int size = arraySizeProfile.profile(array.size);
2233-
Object newStore = stores.allocator(store).allocate(size);
2233+
Object newStore = stores.unsharedAllocator(store).allocate(size);
22342234
stores.copyContents(store, 0, newStore, 0, size);
22352235
mutableStores.sort(newStore, size);
22362236
return createArray(newStore, size);

src/main/java/org/truffleruby/core/array/library/ArrayStoreLibrary.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ public Object unsharedAllocateForNewStore(Object store, Object newStore, int len
220220
return allocateForNewStore(store, newStore, length);
221221
}
222222

223-
/** Return an allocator for a mutable version of {@code store}. */
223+
/** Return an allocator for a mutable version of {@code store}. Only use this if the new store is reassigned to the
224+
* same RubyArray. Otherwise, {@link #unsharedAllocator(Object)} should be used. */
224225
public abstract ArrayAllocator allocator(Object store);
225226

226227
/** Return an allocator for a mutable, unshared version of {@code store}. */

src/main/java/org/truffleruby/core/array/library/DelegatedArrayStorage.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,23 @@
3131
@ExportLibrary(ArrayStoreLibrary.class)
3232
@GenerateUncached
3333
@ImportStatic(ArrayGuards.class)
34-
public class DelegatedArrayStorage implements ObjectGraphNode {
34+
public final class DelegatedArrayStorage implements ObjectGraphNode {
3535

3636
public final Object storage;
3737
public final int offset;
3838
public final int length;
3939

40+
public DelegatedArrayStorage(Object storage, int offset, int length) {
41+
assert offset >= 0;
42+
assert length >= 0;
43+
assert !(storage instanceof DelegatedArrayStorage);
44+
assert !(storage instanceof NativeArrayStorage);
45+
assert !(storage instanceof SharedArrayStorage);
46+
this.storage = storage;
47+
this.offset = offset;
48+
this.length = length;
49+
}
50+
4051
@ExportMessage
4152
protected static boolean accepts(DelegatedArrayStorage store,
4253
@CachedLibrary(limit = "1") ArrayStoreLibrary backingStores) {
@@ -126,7 +137,7 @@ protected void clear(int start, int length) {
126137
@ExportMessage
127138
protected Object toJavaArrayCopy(int length,
128139
@CachedLibrary(limit = "1") ArrayStoreLibrary stores) {
129-
Object newStore = stores.allocator(storage).allocate(length);
140+
Object newStore = stores.unsharedAllocator(storage).allocate(length);
130141
stores.copyContents(storage, offset, newStore, 0, length);
131142
return newStore;
132143
}
@@ -182,18 +193,7 @@ protected boolean isDefaultValue(Object value,
182193
@ExportMessage
183194
protected ArrayAllocator allocator(
184195
@CachedLibrary(limit = "1") ArrayStoreLibrary stores) {
185-
return stores.allocator(storage);
186-
}
187-
188-
public DelegatedArrayStorage(Object storage, int offset, int length) {
189-
assert offset >= 0;
190-
assert length >= 0;
191-
assert !(storage instanceof DelegatedArrayStorage);
192-
assert !(storage instanceof NativeArrayStorage);
193-
assert !(storage instanceof SharedArrayStorage);
194-
this.storage = storage;
195-
this.offset = offset;
196-
this.length = length;
196+
return stores.unsharedAllocator(storage);
197197
}
198198

199199
public boolean hasObjectArrayStorage() {
@@ -214,8 +214,4 @@ public void getAdjacentObjects(Set<Object> reachable) {
214214
}
215215
}
216216

217-
public boolean isEquivalentTo(DelegatedArrayStorage other) {
218-
return storage == other.storage && offset == other.offset && length == other.length;
219-
}
220-
221217
}

src/main/java/org/truffleruby/core/array/library/SharedArrayStorage.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
@ExportLibrary(ArrayStoreLibrary.class)
3737
@GenerateUncached
3838
@ImportStatic(ArrayGuards.class)
39-
public class SharedArrayStorage implements ObjectGraphNode {
39+
public final class SharedArrayStorage implements ObjectGraphNode {
4040

4141
public final Object storage;
4242

@@ -242,13 +242,13 @@ protected Iterable<Object> getIterable(int from, int length,
242242
@ExportMessage
243243
protected ArrayAllocator generalizeForValue(Object newValue,
244244
@CachedLibrary(limit = "1") ArrayStoreLibrary stores) {
245-
return new SharedArrayAllocator(stores.generalizeForValue(storage, newValue));
245+
return stores.generalizeForValue(storage, newValue);
246246
}
247247

248248
@ExportMessage
249249
protected ArrayAllocator generalizeForStore(Object newStore,
250250
@CachedLibrary(limit = "1") ArrayStoreLibrary stores) {
251-
return new SharedArrayAllocator(stores.generalizeForStore(newStore, storage));
251+
return stores.generalizeForStore(newStore, storage);
252252
}
253253

254254
@ExportMessage
@@ -284,13 +284,13 @@ protected boolean isDefaultValue(Object value,
284284
@ExportMessage
285285
protected ArrayAllocator allocator(
286286
@CachedLibrary(limit = "1") ArrayStoreLibrary stores) {
287-
return new SharedArrayAllocator(stores.allocator(storage));
287+
return new SharedArrayAllocator(stores.unsharedAllocator(storage));
288288
}
289289

290290
@ExportMessage
291291
protected ArrayAllocator unsharedAllocator(
292292
@CachedLibrary(limit = "1") ArrayStoreLibrary stores) {
293-
return stores.allocator(storage);
293+
return stores.unsharedAllocator(storage);
294294
}
295295

296296
public boolean hasObjectArrayStorage() {

0 commit comments

Comments
 (0)