Skip to content

Commit a4624b6

Browse files
committed
Array mirror refactor
PullRequest: truffleruby/510
2 parents 06e5dd9 + 1ef3376 commit a4624b6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2006
-1105
lines changed

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
import org.truffleruby.cext.CExtNodesFactory.StringToNativeNodeGen;
4141
import org.truffleruby.core.CoreLibrary;
4242
import org.truffleruby.core.array.ArrayHelpers;
43-
import org.truffleruby.core.array.ArrayOperations;
43+
import org.truffleruby.core.array.ArrayOperationNodes;
44+
import org.truffleruby.core.array.ArrayStrategy;
4445
import org.truffleruby.core.encoding.EncodingOperations;
4546
import org.truffleruby.core.hash.HashNode;
4647
import org.truffleruby.core.module.MethodLookupResult;
@@ -1117,21 +1118,24 @@ public Object executeThrow(CapturedException captured,
11171118
public abstract static class LinkerNode extends CoreMethodArrayArgumentsNode {
11181119

11191120
@TruffleBoundary
1120-
@Specialization(guards = { "isRubyString(outputFileName)", "isRubyArray(libraries)", "isRubyArray(bitcodeFiles)" })
1121-
public Object linker(DynamicObject outputFileName, DynamicObject libraries, DynamicObject bitcodeFiles) {
1121+
@Specialization(guards = { "isRubyString(outputFileName)", "isRubyArray(libraries)", "isRubyArray(bitcodeFiles)", "libraryStrategy.matches(libraries)", "fileStrategy.matches(bitcodeFiles)" })
1122+
public Object linker(DynamicObject outputFileName, DynamicObject libraries, DynamicObject bitcodeFiles,
1123+
@Cached("of(libraries)") ArrayStrategy libraryStrategy,
1124+
@Cached("of(bitcodeFiles)") ArrayStrategy fileStrategy,
1125+
@Cached("libraryStrategy.boxedCopyNode()") ArrayOperationNodes.ArrayBoxedCopyNode libBoxCopyNode,
1126+
@Cached("fileStrategy.boxedCopyNode()") ArrayOperationNodes.ArrayBoxedCopyNode fileBoxCopyNode) {
11221127
try {
11231128
Linker.link(
11241129
StringOperations.getString(outputFileName),
1125-
array2StringList(libraries),
1126-
array2StringList(bitcodeFiles));
1130+
array2StringList(libBoxCopyNode.execute(Layouts.ARRAY.getStore(libraries), Layouts.ARRAY.getSize(libraries))),
1131+
array2StringList(fileBoxCopyNode.execute(Layouts.ARRAY.getStore(bitcodeFiles), Layouts.ARRAY.getSize(bitcodeFiles))));
11271132
} catch (IOException e) {
11281133
throw new JavaException(e);
11291134
}
11301135
return outputFileName;
11311136
}
11321137

1133-
private static List<String> array2StringList(DynamicObject array) {
1134-
Object[] objectArray = ArrayOperations.toObjectArray(array);
1138+
private static List<String> array2StringList(Object[] objectArray) {
11351139
List<String> list = new ArrayList<>(objectArray.length);
11361140
for (int i = 0; i < objectArray.length; i++) {
11371141
list.add(StringOperations.getString((DynamicObject) objectArray[i]));

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

Lines changed: 0 additions & 70 deletions
This file was deleted.

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import static org.truffleruby.core.array.ArrayHelpers.setSize;
2121

22+
import org.truffleruby.Layouts;
23+
2224
@ImportStatic(ArrayGuards.class)
2325
public abstract class ArrayAppendManyNode extends RubyBaseNode {
2426

@@ -32,20 +34,24 @@ public DynamicObject appendManySameType(DynamicObject array, DynamicObject other
3234
@Cached("of(array)") ArrayStrategy strategy,
3335
@Cached("of(other)") ArrayStrategy otherStrategy,
3436
@Cached("strategy.generalize(otherStrategy)") ArrayStrategy generalized,
37+
@Cached("strategy.capacityNode()") ArrayOperationNodes.ArrayCapacityNode capacityNode,
38+
@Cached("generalized.copyStoreNode()") ArrayOperationNodes.ArrayCopyStoreNode copyStoreNode,
39+
@Cached("otherStrategy.copyToNode()") ArrayOperationNodes.ArrayCopyToNode copyToNode,
3540
@Cached("createBinaryProfile()") ConditionProfile extendProfile) {
3641
final int oldSize = strategy.getSize(array);
3742
final int otherSize = otherStrategy.getSize(other);
3843
final int newSize = oldSize + otherSize;
39-
final ArrayMirror storeMirror = strategy.newMirror(array);
40-
final ArrayMirror otherStoreMirror = otherStrategy.newMirror(other);
44+
final Object store = Layouts.ARRAY.getStore(array);
45+
final Object otherStore = Layouts.ARRAY.getStore(other);
4146

42-
if (extendProfile.profile(newSize > storeMirror.getLength())) {
43-
final int capacity = ArrayUtils.capacity(getContext(), storeMirror.getLength(), newSize);
44-
final ArrayMirror newStoreMirror = storeMirror.copyArrayAndMirror(capacity);
45-
otherStoreMirror.copyTo(newStoreMirror, 0, oldSize, otherSize);
46-
strategy.setStoreAndSize(array, newStoreMirror.getArray(), newSize);
47+
final int length = capacityNode.execute(store);
48+
if (extendProfile.profile(newSize > length)) {
49+
final int capacity = ArrayUtils.capacity(getContext(), length, newSize);
50+
Object newStore = copyStoreNode.execute(store, capacity);
51+
copyToNode.execute(otherStore, newStore, 0, oldSize, otherSize);
52+
strategy.setStoreAndSize(array, newStore, newSize);
4753
} else {
48-
otherStoreMirror.copyTo(storeMirror, 0, oldSize, otherSize);
54+
copyToNode.execute(otherStore, store, 0, oldSize, otherSize);
4955
setSize(array, newSize);
5056
}
5157
return array;
@@ -59,14 +65,19 @@ public DynamicObject appendManyGeneralize(DynamicObject array, DynamicObject oth
5965
@Cached("of(array)") ArrayStrategy strategy,
6066
@Cached("of(other)") ArrayStrategy otherStrategy,
6167
@Cached("strategy.generalize(otherStrategy)") ArrayStrategy generalized,
68+
@Cached("generalized.newStoreNode()") ArrayOperationNodes.ArrayNewStoreNode newStoreNode,
69+
@Cached("strategy.copyToNode()") ArrayOperationNodes.ArrayCopyToNode copyToNode,
70+
@Cached("otherStrategy.copyToNode()") ArrayOperationNodes.ArrayCopyToNode otherCopyToNode,
6271
@Cached("createBinaryProfile()") ConditionProfile extendProfile) {
6372
final int oldSize = strategy.getSize(array);
6473
final int otherSize = otherStrategy.getSize(other);
6574
final int newSize = oldSize + otherSize;
66-
final ArrayMirror newStoreMirror = generalized.newArray(newSize);
67-
strategy.newMirror(array).copyTo(newStoreMirror, 0, 0, oldSize);
68-
otherStrategy.newMirror(other).copyTo(newStoreMirror, 0, oldSize, otherSize);
69-
generalized.setStoreAndSize(array, newStoreMirror.getArray(), newSize);
75+
final Object store = Layouts.ARRAY.getStore(array);
76+
final Object otherStore = Layouts.ARRAY.getStore(other);
77+
final Object newStore = newStoreNode.execute(newSize);
78+
copyToNode.execute(store, newStore, 0, 0, oldSize);
79+
otherCopyToNode.execute(otherStore, newStore, 0, oldSize, otherSize);
80+
generalized.setStoreAndSize(array, newStore, newSize);
7081
return array;
7182
}
7283

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,23 @@ public static ArrayAppendOneNode create() {
3939
@Specialization(guards = { "strategy.matches(array)", "strategy.accepts(value)" }, limit = "STORAGE_STRATEGIES")
4040
public DynamicObject appendOneSameType(DynamicObject array, Object value,
4141
@Cached("of(array)") ArrayStrategy strategy,
42+
@Cached("strategy.capacityNode()") ArrayOperationNodes.ArrayCapacityNode capacityNode,
43+
@Cached("strategy.copyStoreNode()") ArrayOperationNodes.ArrayCopyStoreNode copyStoreNode,
44+
@Cached("strategy.setNode()") ArrayOperationNodes.ArraySetNode setNode,
4245
@Cached("createCountingProfile()") ConditionProfile extendProfile) {
43-
final ArrayMirror storeMirror = strategy.newMirror(array);
46+
final Object store = Layouts.ARRAY.getStore(array);
4447
final int oldSize = Layouts.ARRAY.getSize(array);
4548
final int newSize = oldSize + 1;
49+
final int length = capacityNode.execute(store);
4650

47-
if (extendProfile.profile(newSize > storeMirror.getLength())) {
48-
final int capacity = ArrayUtils.capacityForOneMore(getContext(), storeMirror.getLength());
49-
final ArrayMirror newStoreMirror = storeMirror.copyArrayAndMirror(capacity);
50-
newStoreMirror.set(oldSize, value);
51-
strategy.setStore(array, newStoreMirror.getArray());
51+
if (extendProfile.profile(newSize > length)) {
52+
final int capacity = ArrayUtils.capacityForOneMore(getContext(), length);
53+
final Object newStore = copyStoreNode.execute(store, capacity);
54+
setNode.execute(newStore, oldSize, value);
55+
strategy.setStore(array, newStore);
5256
setSize(array, newSize);
5357
} else {
54-
storeMirror.set(oldSize, value);
58+
setNode.execute(store, oldSize, value);
5559
setSize(array, newSize);
5660
}
5761
return array;
@@ -65,17 +69,21 @@ public DynamicObject appendOneSameType(DynamicObject array, Object value,
6569
public DynamicObject appendOneGeneralizeNonMutable(DynamicObject array, Object value,
6670
@Cached("of(array)") ArrayStrategy strategy,
6771
@Cached("forValue(value)") ArrayStrategy valueStrategy,
68-
@Cached("strategy.generalize(valueStrategy)") ArrayStrategy generalizedStrategy) {
72+
@Cached("strategy.generalize(valueStrategy)") ArrayStrategy generalizedStrategy,
73+
@Cached("strategy.capacityNode()") ArrayOperationNodes.ArrayCapacityNode capacityNode,
74+
@Cached("strategy.copyToNode()") ArrayOperationNodes.ArrayCopyToNode copyToNode,
75+
@Cached("generalizedStrategy.setNode()") ArrayOperationNodes.ArraySetNode setNode,
76+
@Cached("generalizedStrategy.newStoreNode()") ArrayOperationNodes.ArrayNewStoreNode newStoreNode) {
6977
assert strategy != valueStrategy;
7078
final int oldSize = strategy.getSize(array);
7179
final int newSize = oldSize + 1;
72-
final ArrayMirror currentMirror = strategy.newMirror(array);
73-
final int oldCapacity = currentMirror.getLength();
80+
final Object currentStore = Layouts.ARRAY.getStore(array);
81+
final int oldCapacity = capacityNode.execute(currentStore);
7482
final int newCapacity = newSize > oldCapacity ? ArrayUtils.capacityForOneMore(getContext(), oldCapacity) : oldCapacity;
75-
final ArrayMirror storeMirror = generalizedStrategy.newArray(newCapacity);
76-
currentMirror.copyTo(storeMirror, 0, 0, oldSize);
77-
storeMirror.set(oldSize, value);
78-
generalizedStrategy.setStore(array, storeMirror.getArray());
83+
final Object newStore = newStoreNode.execute(newCapacity);
84+
copyToNode.execute(currentStore, newStore, 0, 0, oldSize);
85+
setNode.execute(newStore, oldSize, value);
86+
generalizedStrategy.setStore(array, newStore);
7987
setSize(array, newSize);
8088
return array;
8189
}

0 commit comments

Comments
 (0)