Skip to content

Commit 6fbcb83

Browse files
author
Pavel Marek
committed
Implement CharSXP-specific messages in VectorDataLibrary
(cherry picked from commit c2e4dcf)
1 parent 7e4bda7 commit 6fbcb83

File tree

7 files changed

+312
-22
lines changed

7 files changed

+312
-22
lines changed

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/SetStringEltNode.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ public static SetStringEltNode create() {
3737

3838
@Specialization(limit = "getTypedVectorDataLibraryCacheSize()")
3939
Object doIt(RStringVector vector, long index, CharSXPWrapper element,
40-
@CachedLibrary("vector.getData()") VectorDataLibrary dataLibrary) {
41-
dataLibrary.setStringAt(vector.getData(), (int) index, element.getContents());
40+
@CachedLibrary(limit = "getTypedVectorDataLibraryCacheSize()") VectorDataLibrary dataLibrary) {
41+
Object newCharSXPData = dataLibrary.materializeCharSXPStorage(vector.getData());
42+
vector.setData(newCharSXPData);
43+
dataLibrary.setCharSXPAt(vector.getData(), (int) index, element);
4244
return null;
4345
}
4446
}

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/StringEltNode.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
*/
2323
package com.oracle.truffle.r.ffi.impl.nodes;
2424

25-
import com.oracle.truffle.api.dsl.Cached;
25+
import com.oracle.truffle.api.CompilerDirectives;
2626
import com.oracle.truffle.api.dsl.GenerateUncached;
2727
import com.oracle.truffle.api.dsl.Specialization;
2828
import com.oracle.truffle.api.library.CachedLibrary;
29-
import com.oracle.truffle.api.profiles.ConditionProfile;
29+
import com.oracle.truffle.r.runtime.RError;
3030
import com.oracle.truffle.r.runtime.data.RStringVector;
3131
import com.oracle.truffle.r.runtime.data.VectorDataLibrary;
3232

@@ -36,17 +36,16 @@ public static StringEltNode create() {
3636
return StringEltNodeGen.create();
3737
}
3838

39-
// TODO: Make just one specialization that handles both altrep and non-altrep via
40-
// VectorDataLibrary
4139
@Specialization(limit = "1")
4240
Object doStringVector(RStringVector stringVector, long index,
43-
@CachedLibrary("stringVector.getData()") VectorDataLibrary dataLibrary,
44-
@Cached("createBinaryProfile()") ConditionProfile isAltrepProfile) {
45-
if (isAltrepProfile.profile(stringVector.isAltRep())) {
46-
return dataLibrary.getStringAt(stringVector, (int) index);
47-
} else {
48-
stringVector.wrapStrings();
49-
return stringVector.getWrappedDataAt((int) index);
41+
@CachedLibrary(limit = "getTypedVectorDataLibraryCacheSize()") VectorDataLibrary genericDataLib,
42+
@CachedLibrary("stringVector.getData()") VectorDataLibrary stringVecDataLib) {
43+
Object newCharSXPData = stringVecDataLib.materializeCharSXPStorage(stringVector.getData());
44+
stringVector.setData(newCharSXPData);
45+
if (index > Integer.MAX_VALUE) {
46+
CompilerDirectives.transferToInterpreter();
47+
throw RError.error(RError.NO_CALLER, RError.Message.LONG_VECTORS_NOT_SUPPORTED);
5048
}
49+
return genericDataLib.getCharSXPAt(stringVector.getData(), (int) index);
5150
}
5251
}

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RStringArrayVectorData.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
*/
2323
package com.oracle.truffle.r.runtime.data;
2424

25-
import static com.oracle.truffle.r.runtime.data.model.RAbstractVector.ENABLE_COMPLETE;
26-
27-
import java.util.Arrays;
28-
2925
import com.oracle.truffle.api.dsl.Cached;
3026
import com.oracle.truffle.api.dsl.Cached.Shared;
3127
import com.oracle.truffle.api.interop.TruffleObject;
@@ -43,6 +39,10 @@
4339
import com.oracle.truffle.r.runtime.data.VectorDataLibrary.SeqWriteIterator;
4440
import com.oracle.truffle.r.runtime.ops.na.NACheck;
4541

42+
import java.util.Arrays;
43+
44+
import static com.oracle.truffle.r.runtime.data.model.RAbstractVector.ENABLE_COMPLETE;
45+
4646
@ExportLibrary(VectorDataLibrary.class)
4747
class RStringArrayVectorData implements TruffleObject, ShareableVectorData {
4848
private final String[] data;
@@ -84,6 +84,11 @@ public RStringArrayVectorData materialize() {
8484
return this;
8585
}
8686

87+
@ExportMessage
88+
public RStringCharSXPData materializeCharSXPStorage() {
89+
return wrapStrings();
90+
}
91+
8792
@ExportMessage
8893
public boolean isWriteable() {
8994
return true;

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RStringCharSXPData.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import com.oracle.truffle.r.runtime.data.VectorDataLibrary.SeqWriteIterator;
3636
import com.oracle.truffle.r.runtime.ops.na.NACheck;
3737

38+
import java.util.Arrays;
39+
3840
@ExportLibrary(VectorDataLibrary.class)
3941
public class RStringCharSXPData implements ShareableVectorData {
4042
private final CharSXPWrapper[] data;
@@ -75,6 +77,11 @@ public RStringCharSXPData materialize() {
7577
return this;
7678
}
7779

80+
@ExportMessage
81+
public RStringCharSXPData materializeCharSXPStorage() {
82+
return this;
83+
}
84+
7885
@ExportMessage
7986
public boolean isWriteable() {
8087
return true;
@@ -96,6 +103,11 @@ public String[] getStringDataCopy() {
96103
return result;
97104
}
98105

106+
@ExportMessage
107+
public CharSXPWrapper[] getCharSXPDataCopy() {
108+
return Arrays.copyOf(data, data.length);
109+
}
110+
99111
// Data access:
100112

101113
@ExportMessage
@@ -140,6 +152,21 @@ public String getString(@SuppressWarnings("unused") RandomAccessIterator it, int
140152
return data[index].getContents();
141153
}
142154

155+
@ExportMessage
156+
public CharSXPWrapper getCharSXPAt(int index) {
157+
return data[index];
158+
}
159+
160+
@ExportMessage
161+
public CharSXPWrapper getNextCharSXP(SeqIterator it) {
162+
return data[it.getIndex()];
163+
}
164+
165+
@ExportMessage
166+
public CharSXPWrapper getCharSXP(@SuppressWarnings("unused") RandomAccessIterator it, int index) {
167+
return data[index];
168+
}
169+
143170
// Write access to the elements:
144171

145172
@ExportMessage
@@ -166,4 +193,19 @@ public void setNextString(SeqWriteIterator it, String value) {
166193
public void setString(@SuppressWarnings("unused") RandomAccessWriteIterator it, int index, String value) {
167194
data[index] = CharSXPWrapper.create(value);
168195
}
196+
197+
@ExportMessage
198+
public void setCharSXPAt(int index, CharSXPWrapper value) {
199+
data[index] = value;
200+
}
201+
202+
@ExportMessage
203+
public void setNextCharSXP(SeqWriteIterator it, CharSXPWrapper value) {
204+
data[it.getIndex()] = value;
205+
}
206+
207+
@ExportMessage
208+
public void setCharSXP(@SuppressWarnings("unused") RandomAccessWriteIterator it, int index, CharSXPWrapper value) {
209+
data[index] = value;
210+
}
169211
}

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RStringSeqVectorData.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ public RStringArrayVectorData materialize() {
117117
return new RStringArrayVectorData(getStringDataCopy(), true);
118118
}
119119

120+
@ExportMessage
121+
public RStringCharSXPData materializeCharSXPStorage() {
122+
return new RStringCharSXPData(getCharSXPDataCopy());
123+
}
124+
120125
@ExportMessage
121126
public RStringSeqVectorData copy(@SuppressWarnings("unused") boolean deep) {
122127
return new RStringSeqVectorData(prefix, suffix, start, stride, length);
@@ -136,6 +141,15 @@ public String[] getStringDataCopy() {
136141
return result;
137142
}
138143

144+
@ExportMessage
145+
public CharSXPWrapper[] getCharSXPDataCopy() {
146+
CharSXPWrapper[] result = new CharSXPWrapper[length];
147+
for (int i = 0; i < result.length; i++) {
148+
result[i] = getCharSXPImpl(i);
149+
}
150+
return result;
151+
}
152+
139153
// Read access to the elements:
140154

141155
@ExportMessage
@@ -179,11 +193,30 @@ public String getString(@SuppressWarnings("unused") RandomAccessIterator it, int
179193
return getStringImpl(index);
180194
}
181195

196+
@ExportMessage
197+
public CharSXPWrapper getCharSXPAt(int index) {
198+
return getCharSXPImpl(index);
199+
}
200+
201+
@ExportMessage
202+
public CharSXPWrapper getNextCharSXP(SeqIterator it) {
203+
return getCharSXPImpl(it.getIndex());
204+
}
205+
206+
@ExportMessage
207+
public CharSXPWrapper getCharSXP(@SuppressWarnings("unused") RandomAccessIterator it, int index) {
208+
return getCharSXPImpl(index);
209+
}
210+
182211
// Utility methods:
183212

184213
@TruffleBoundary
185214
private String getStringImpl(int index) {
186215
assert index >= 0 && index < getLength();
187216
return prefix + (start + stride * index) + suffix;
188217
}
218+
219+
private CharSXPWrapper getCharSXPImpl(int index) {
220+
return CharSXPWrapper.create(getStringImpl(index));
221+
}
189222
}

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/VectorDataClosure.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,43 @@ public String getString(RandomAccessIterator it, int index,
352352
return dataLib.getString(data, it, index);
353353
}
354354

355+
// CharSXP
356+
357+
@ExportMessage
358+
public CharSXPWrapper[] getCharSXPDataCopy(@CachedLibrary("this.data") VectorDataLibrary dataLib) {
359+
assert getTargetType() == RType.Char || getTargetType() == RType.Character;
360+
CharSXPWrapper[] result = new CharSXPWrapper[getLength(dataLib)];
361+
SeqIterator it = dataLib.iterator(data);
362+
while (dataLib.next(data, it)) {
363+
result[it.getIndex()] = dataLib.getNextCharSXP(data, it);
364+
}
365+
return result;
366+
}
367+
368+
@ExportMessage
369+
public RStringCharSXPData materializeCharSXPStorage(@CachedLibrary("this.data") VectorDataLibrary dataLib) {
370+
assert getTargetType() == RType.Char || getTargetType() == RType.Character;
371+
return new RStringCharSXPData(getCharSXPDataCopy(dataLib));
372+
}
373+
374+
@ExportMessage
375+
public CharSXPWrapper getCharSXPAt(int index,
376+
@CachedLibrary("this.data") VectorDataLibrary dataLib) {
377+
return dataLib.getCharSXPAt(data, index);
378+
}
379+
380+
@ExportMessage
381+
public CharSXPWrapper getNextCharSXP(SeqIterator it,
382+
@CachedLibrary("this.data") VectorDataLibrary dataLib) {
383+
return dataLib.getNextCharSXP(data, it);
384+
}
385+
386+
@ExportMessage
387+
public CharSXPWrapper getCharSXP(RandomAccessIterator it, int index,
388+
@CachedLibrary("this.data") VectorDataLibrary dataLib) {
389+
return dataLib.getCharSXP(data, it, index);
390+
}
391+
355392
// List
356393

357394
@ExportMessage

0 commit comments

Comments
 (0)