Skip to content

Commit 50ea6a2

Browse files
committed
UpdateSubscriptSpecial2 uses Truffle libraries to access vectors
1 parent 19da55e commit 50ea6a2

File tree

1 file changed

+42
-63
lines changed

1 file changed

+42
-63
lines changed

com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/infix/special/UpdateSubscriptSpecial2.java

Lines changed: 42 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import com.oracle.truffle.api.dsl.Fallback;
2727
import com.oracle.truffle.api.dsl.Specialization;
2828
import com.oracle.truffle.api.frame.VirtualFrame;
29+
import com.oracle.truffle.api.library.CachedLibrary;
30+
import com.oracle.truffle.api.profiles.ConditionProfile;
2931
import com.oracle.truffle.r.nodes.builtin.base.infix.special.ProfiledSpecialsUtilsFactory.ProfiledUpdateSubscriptSpecial2NodeGen;
3032
import com.oracle.truffle.r.nodes.builtin.base.infix.special.SpecialsUtils.ConvertIndex;
3133
import com.oracle.truffle.r.nodes.builtin.base.infix.special.SpecialsUtils.ConvertValue;
@@ -35,7 +37,7 @@
3537
import com.oracle.truffle.r.runtime.data.RIntVector;
3638
import com.oracle.truffle.r.runtime.data.RList;
3739
import com.oracle.truffle.r.runtime.data.RStringVector;
38-
import com.oracle.truffle.r.runtime.data.nodes.VectorAccess;
40+
import com.oracle.truffle.r.runtime.data.VectorDataLibrary;
3941
import com.oracle.truffle.r.runtime.nodes.RNode;
4042

4143
public abstract class UpdateSubscriptSpecial2 extends IndexingSpecial2Common {
@@ -46,88 +48,65 @@ protected UpdateSubscriptSpecial2(boolean inReplacement) {
4648

4749
protected abstract Object execute(VirtualFrame frame, Object vec, Object index1, Object index2, Object value);
4850

49-
@Specialization(guards = {"access.supports(vector)", "simpleVector(vector)", "!vector.isShared()", "isValidIndex(vector, index1, index2)", "vector.isMaterialized()"})
51+
@Specialization(guards = {"simpleVector(vector)", "!vector.isShared()", "isValidIndex(vector, index1, index2)", "dataLib.isWriteable(vector.getData())"}, limit = "getVectorAccessCacheSize()")
5052
protected RIntVector setInt(RIntVector vector, int index1, int index2, int value,
51-
@Cached("vector.access()") VectorAccess access) {
52-
try (VectorAccess.RandomIterator iter = access.randomAccess(vector)) {
53-
access.setInt(iter, matrixIndex(vector, index1, index2), value);
54-
if (RRuntime.isNA(value)) {
55-
vector.setComplete(false);
56-
}
57-
return vector;
53+
@CachedLibrary("vector.getData()") VectorDataLibrary dataLib) {
54+
Object vectorData = vector.getData();
55+
try (VectorDataLibrary.RandomAccessWriteIterator it = dataLib.randomAccessWriteIterator(vectorData)) {
56+
dataLib.setInt(vectorData, it, matrixIndex(vector, index1, index2), value);
57+
dataLib.commitRandomAccessWriteIterator(vectorData, it, !RRuntime.isNA(value));
5858
}
59+
return vector;
5960
}
6061

61-
@Specialization(replaces = "setInt", guards = {"simpleVector(vector)", "!vector.isShared()", "isValidIndex(vector, index1, index2)", "vector.isMaterialized()"})
62-
protected RIntVector setIntGeneric(RIntVector vector, int index1, int index2, int value) {
63-
return setInt(vector, index1, index2, value, vector.slowPathAccess());
64-
}
65-
66-
@Specialization(guards = {"access.supports(vector)", "simpleVector(vector)", "!vector.isShared()", "isValidIndex(vector, index1, index2)", "vector.isMaterialized()"})
62+
@Specialization(guards = {"simpleVector(vector)", "!vector.isShared()", "isValidIndex(vector, index1, index2)", "dataLib.isWriteable(vector.getData())"}, limit = "getVectorAccessCacheSize()")
6763
protected RDoubleVector setDouble(RDoubleVector vector, int index1, int index2, double value,
68-
@Cached("vector.access()") VectorAccess access) {
69-
try (VectorAccess.RandomIterator iter = access.randomAccess(vector)) {
70-
access.setDouble(iter, matrixIndex(vector, index1, index2), value);
71-
if (RRuntime.isNA(value)) {
72-
vector.setComplete(false);
73-
}
74-
return vector;
64+
@CachedLibrary("vector.getData()") VectorDataLibrary dataLib) {
65+
Object vectorData = vector.getData();
66+
try (VectorDataLibrary.RandomAccessWriteIterator it = dataLib.randomAccessWriteIterator(vectorData)) {
67+
dataLib.setDouble(vectorData, it, matrixIndex(vector, index1, index2), value);
68+
dataLib.commitRandomAccessWriteIterator(vectorData, it, !RRuntime.isNA(value));
7569
}
70+
return vector;
7671
}
7772

78-
@Specialization(replaces = "setDouble", guards = {"simpleVector(vector)", "!vector.isShared()", "isValidIndex(vector, index1, index2)", "vector.isMaterialized()"})
79-
protected RDoubleVector setDoubleGeneric(RDoubleVector vector, int index1, int index2, double value) {
80-
return setDouble(vector, index1, index2, value, vector.slowPathAccess());
81-
}
82-
83-
@Specialization(guards = {"access.supports(vector)", "simpleVector(vector)", "!vector.isShared()", "isValidIndex(vector, index1, index2)", "vector.isMaterialized()"})
73+
@Specialization(guards = {"simpleVector(vector)", "!vector.isShared()", "isValidIndex(vector, index1, index2)", "dataLib.isWriteable(vector.getData())"}, limit = "getVectorAccessCacheSize()")
8474
protected RStringVector setString(RStringVector vector, int index1, int index2, String value,
85-
@Cached("vector.access()") VectorAccess access) {
86-
try (VectorAccess.RandomIterator iter = access.randomAccess(vector)) {
87-
access.setString(iter, matrixIndex(vector, index1, index2), value);
88-
if (RRuntime.isNA(value)) {
89-
vector.setComplete(false);
90-
}
91-
return vector;
75+
@CachedLibrary("vector.getData()") VectorDataLibrary dataLib) {
76+
Object vectorData = vector.getData();
77+
try (VectorDataLibrary.RandomAccessWriteIterator it = dataLib.randomAccessWriteIterator(vectorData)) {
78+
dataLib.setString(vectorData, it, matrixIndex(vector, index1, index2), value);
79+
dataLib.commitRandomAccessWriteIterator(vectorData, it, !RRuntime.isNA(value));
9280
}
81+
return vector;
9382
}
9483

95-
@Specialization(replaces = "setString", guards = {"simpleVector(vector)", "!vector.isShared()", "isValidIndex(vector, index1, index2)", "vector.isMaterialized()"})
96-
protected RStringVector setStringGeneric(RStringVector vector, int index1, int index2, String value) {
97-
return setString(vector, index1, index2, value, vector.slowPathAccess());
98-
}
99-
100-
@Specialization(guards = {"access.supports(list)", "simpleVector(list)", "!list.isShared()", "isValidIndex(list, index1, index2)", "isSingleElement(value)"})
84+
@Specialization(guards = {"simpleVector(list)", "!list.isShared()", "isValidIndex(list, index1, index2)", "dataLib.isWriteable(list.getData())"}, limit = "getVectorAccessCacheSize()")
10185
protected Object setList(RList list, int index1, int index2, Object value,
102-
@Cached("list.access()") VectorAccess access) {
103-
try (VectorAccess.RandomIterator iter = access.randomAccess(list)) {
104-
access.setListElement(iter, matrixIndex(list, index1, index2), value);
105-
return list;
86+
@CachedLibrary("list.getData()") VectorDataLibrary dataLib) {
87+
Object vectorData = list.getData();
88+
try (VectorDataLibrary.RandomAccessWriteIterator it = dataLib.randomAccessWriteIterator(vectorData)) {
89+
dataLib.setElement(vectorData, it, matrixIndex(list, index1, index2), value);
90+
dataLib.commitRandomAccessWriteIterator(vectorData, it, false);
10691
}
92+
return list;
10793
}
10894

109-
@Specialization(replaces = "setList", guards = {"simpleVector(list)", "!list.isShared()", "isValidIndex(list, index1, index2)", "isSingleElement(value)"})
110-
protected Object setListGeneric(RList list, int index1, int index2, Object value) {
111-
return setList(list, index1, index2, value, list.slowPathAccess());
112-
}
113-
114-
@Specialization(guards = {"access.supports(vector)", "simpleVector(vector)", "!vector.isShared()", "isValidIndex(vector, index1, index2)", "vector.isMaterialized()"})
95+
@Specialization(guards = {"simpleVector(vector)", "!vector.isShared()", "isValidIndex(vector, index1, index2)", "dataLib.isWriteable(vector.getData())"}, limit = "getVectorAccessCacheSize()")
11596
protected RDoubleVector setDoubleIntIndexIntValue(RDoubleVector vector, int index1, int index2, int value,
116-
@Cached("vector.access()") VectorAccess access) {
117-
try (VectorAccess.RandomIterator iter = access.randomAccess(vector)) {
118-
if (RRuntime.isNA(value)) {
119-
access.setDouble(iter, matrixIndex(vector, index1, index2), RRuntime.DOUBLE_NA);
120-
vector.setComplete(false);
97+
@Cached("createBinaryProfile()") ConditionProfile naProfile,
98+
@CachedLibrary("vector.getData()") VectorDataLibrary dataLib) {
99+
Object vectorData = vector.getData();
100+
try (VectorDataLibrary.RandomAccessWriteIterator it = dataLib.randomAccessWriteIterator(vectorData)) {
101+
if (naProfile.profile(RRuntime.isNA(value))) {
102+
dataLib.setDouble(vectorData, it, matrixIndex(vector, index1, index2), RRuntime.DOUBLE_NA);
103+
dataLib.commitRandomAccessWriteIterator(vectorData, it, false);
121104
} else {
122-
access.setDouble(iter, matrixIndex(vector, index1, index2), value);
105+
dataLib.setDouble(vectorData, it, matrixIndex(vector, index1, index2), value);
106+
dataLib.commitRandomAccessWriteIterator(vectorData, it, true);
123107
}
124-
return vector;
125108
}
126-
}
127-
128-
@Specialization(replaces = "setDoubleIntIndexIntValue", guards = {"simpleVector(vector)", "!vector.isShared()", "isValidIndex(vector, index1, index2)", "vector.isMaterialized()"})
129-
protected RDoubleVector setDoubleIntIndexIntValueGeneric(RDoubleVector vector, int index1, int index2, int value) {
130-
return setDoubleIntIndexIntValue(vector, index1, index2, value, vector.slowPathAccess());
109+
return vector;
131110
}
132111

133112
@SuppressWarnings("unused")

0 commit comments

Comments
 (0)