Skip to content

Commit 8880fe5

Browse files
committed
[GR-22670] Interop implementation in R vectors delegates to vector data library
(cherry picked from commit 2a4d434)
1 parent 0963b32 commit 8880fe5

File tree

10 files changed

+165
-129
lines changed

10 files changed

+165
-129
lines changed

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,6 @@ public boolean isMaterialized() {
124124
return VectorDataLibrary.getFactory().getUncached().isWriteable(this.data);
125125
}
126126

127-
@Override
128-
protected boolean isScalarNA() {
129-
assert getLength() == 1;
130-
return RRuntime.isNA(getDataAt(0));
131-
}
132-
133127
@Override
134128
public boolean isForeignWrapper() {
135129
return data instanceof RDoubleForeignObjData;

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,6 @@ public boolean isForeignWrapper() {
151151

152152
// ---------------------
153153

154-
@Override
155-
protected boolean isScalarNA() {
156-
assert getLength() == 1;
157-
return RRuntime.isNA(getDataAt(0));
158-
}
159-
160154
@Override
161155
public RType getRType() {
162156
return RType.Integer;

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,31 @@ static RLogicalVector fromNative(long address, int length) {
107107
}
108108

109109
@ExportMessage
110-
boolean isNull(@Cached.Exclusive @Cached("createBinaryProfile()") ConditionProfile isScalar) {
111-
if (!isScalar.profile(isScalar())) {
110+
boolean isNull(
111+
@CachedLibrary(limit = DATA_LIB_LIMIT) VectorDataLibrary dataLib,
112+
@Cached.Exclusive @Cached("createBinaryProfile()") ConditionProfile isScalar) {
113+
if (!isScalar.profile(isScalar(dataLib))) {
112114
return false;
113115
}
114-
return RRuntime.isNA(getDataAt(0));
116+
return RRuntime.isNA(dataLib.getLogicalAt(getData(), 0));
115117
}
116118

117119
@ExportMessage
118-
boolean isBoolean() {
119-
if (!isScalar()) {
120+
boolean isBoolean(@CachedLibrary(limit = DATA_LIB_LIMIT) VectorDataLibrary dataLib) {
121+
if (!isScalar(dataLib)) {
120122
return false;
121123
}
122-
return !RRuntime.isNA(getDataAt(0));
124+
return !RRuntime.isNA(dataLib.getLogicalAt(getData(), 0));
123125
}
124126

125127
@ExportMessage
126-
boolean asBoolean(@Cached.Exclusive @Cached("createBinaryProfile()") ConditionProfile isBoolean) throws UnsupportedMessageException {
127-
if (isBoolean.profile(!isBoolean())) {
128+
boolean asBoolean(
129+
@CachedLibrary(limit = DATA_LIB_LIMIT) VectorDataLibrary dataLib,
130+
@Cached.Exclusive @Cached("createBinaryProfile()") ConditionProfile isBoolean) throws UnsupportedMessageException {
131+
if (isBoolean.profile(!isBoolean(dataLib))) {
128132
throw UnsupportedMessageException.create();
129133
}
130-
return RRuntime.fromLogical(getDataAt(0));
134+
return RRuntime.fromLogical(dataLib.getLogicalAt(getData(), 0));
131135
}
132136

133137
@Override

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,9 @@ public RType getRType() {
9494
}
9595

9696
@Override
97-
protected boolean isScalarNA() {
97+
protected Object getScalarValue(VectorDataLibrary dataLib) {
9898
assert getLength() == 1;
99-
return false;
100-
}
101-
102-
@Override
103-
protected Object getScalarValue() {
104-
assert getLength() == 1;
105-
return getRawDataAt(0);
99+
return dataLib.getRawAt(getData(), 0);
106100
}
107101

108102
@Override

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/model/RAbstractAtomicVector.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.oracle.truffle.r.runtime.data.RDoubleVector;
2727
import com.oracle.truffle.r.runtime.data.RLogicalVector;
2828
import com.oracle.truffle.r.runtime.data.RRawVector;
29+
import com.oracle.truffle.r.runtime.data.VectorDataLibrary;
2930

3031
/**
3132
* Distinguishes what R considers an "atomic" vector, e.g. {@code integer()} from other "vectors",
@@ -45,8 +46,8 @@ public RAbstractAtomicVector(boolean complete) {
4546
super(complete);
4647
}
4748

48-
public final boolean isScalar() {
49-
return getLength() == 1;
49+
protected final boolean isScalar(VectorDataLibrary dataLib) {
50+
return dataLib.getLength(getData()) == 1;
5051
}
5152

5253
@Override

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/model/RAbstractComplexVector.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.oracle.truffle.api.interop.InteropLibrary;
2929
import com.oracle.truffle.api.interop.UnknownIdentifierException;
3030
import com.oracle.truffle.api.interop.UnsupportedMessageException;
31+
import com.oracle.truffle.api.library.CachedLibrary;
3132
import com.oracle.truffle.api.library.ExportLibrary;
3233
import com.oracle.truffle.api.library.ExportMessage;
3334
import com.oracle.truffle.api.library.ExportMessage.Ignore;
@@ -63,55 +64,61 @@ public RAbstractComplexVector(boolean complete) {
6364
}
6465

6566
@ExportMessage
66-
public final boolean isNull(@Cached.Exclusive @Cached("createBinaryProfile()") ConditionProfile isScalar) {
67-
if (!isScalar.profile(isScalar())) {
67+
public final boolean isNull(
68+
@CachedLibrary(limit = DATA_LIB_LIMIT) VectorDataLibrary dataLib,
69+
@Cached.Exclusive @Cached("createBinaryProfile()") ConditionProfile isScalar) {
70+
if (!isScalar.profile(isScalar(dataLib))) {
6871
return false;
6972
}
70-
return RRuntime.isNA(getDataAt(0));
73+
return RRuntime.isNA(dataLib.getComplexAt(getData(), 0));
7174
}
7275

7376
@ExportMessage
74-
boolean hasMembers() {
75-
return isScalar() && !RRuntime.isNA(getDataAt(0));
77+
boolean hasMembers(@CachedLibrary(limit = DATA_LIB_LIMIT) VectorDataLibrary dataLib) {
78+
return isScalar(dataLib) && !RRuntime.isNA(dataLib.getComplexAt(getData(), 0));
7679
}
7780

7881
@ExportMessage
7982
Object getMembers(@SuppressWarnings("unused") boolean includeInternal,
83+
@CachedLibrary(limit = DATA_LIB_LIMIT) VectorDataLibrary dataLib,
8084
@Cached.Shared("noMembers") @Cached("createBinaryProfile()") ConditionProfile noMembers) throws UnsupportedMessageException {
81-
if (noMembers.profile(!hasMembers())) {
85+
if (noMembers.profile(!hasMembers(dataLib))) {
8286
throw UnsupportedMessageException.create();
8387
}
8488
return RDataFactory.createStringVector(new String[]{MEMBER_RE, MEMBER_IM}, RDataFactory.COMPLETE_VECTOR);
8589
}
8690

8791
@ExportMessage
8892
boolean isMemberReadable(String member,
93+
@CachedLibrary(limit = DATA_LIB_LIMIT) VectorDataLibrary dataLib,
8994
@Cached.Shared("noMembers") @Cached("createBinaryProfile()") ConditionProfile noMembers) {
90-
if (noMembers.profile(!hasMembers())) {
95+
if (noMembers.profile(!hasMembers(dataLib))) {
9196
return false;
9297
}
9398
return MEMBER_RE.equals(member) || MEMBER_IM.equals(member);
9499
}
95100

96101
@ExportMessage
97102
Object readMember(String member,
103+
@CachedLibrary(limit = DATA_LIB_LIMIT) VectorDataLibrary dataLib,
98104
@Cached.Shared("noMembers") @Cached("createBinaryProfile()") ConditionProfile noMembers,
99105
@Cached.Exclusive @Cached("createBinaryProfile()") ConditionProfile unknownIdentifier,
100106
@Cached.Exclusive @Cached("createBinaryProfile()") ConditionProfile isNullIdentifier,
101107
@Cached.Exclusive @Cached("createBinaryProfile()") ConditionProfile isNA) throws UnknownIdentifierException, UnsupportedMessageException {
102-
if (noMembers.profile(!hasMembers())) {
108+
if (noMembers.profile(!hasMembers(dataLib))) {
103109
throw UnsupportedMessageException.create();
104110
}
105-
if (unknownIdentifier.profile(!isMemberReadable(member, noMembers))) {
111+
if (unknownIdentifier.profile(!isMemberReadable(member, dataLib, noMembers))) {
106112
throw UnknownIdentifierException.create(member);
107113
}
108-
if (isNullIdentifier.profile(isNull(isNA))) {
109-
return new RInteropComplexNA(getDataAt(0));
114+
RComplex singleValue = dataLib.getComplexAt(getData(), 0);
115+
if (isNullIdentifier.profile(isNull(dataLib, isNA))) {
116+
return new RInteropComplexNA(singleValue);
110117
}
111118
if (MEMBER_RE.equals(member)) {
112-
return getDataAt(0).getRealPart();
119+
return singleValue.getRealPart();
113120
} else {
114-
return getDataAt(0).getImaginaryPart();
121+
return singleValue.getImaginaryPart();
115122
}
116123
}
117124

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/model/RAbstractListBaseVector.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.oracle.truffle.api.interop.InteropLibrary;
2828
import com.oracle.truffle.api.interop.UnknownIdentifierException;
2929
import com.oracle.truffle.api.interop.UnsupportedMessageException;
30+
import com.oracle.truffle.api.library.CachedLibrary;
3031
import com.oracle.truffle.api.library.ExportLibrary;
3132
import com.oracle.truffle.api.library.ExportMessage;
3233
import com.oracle.truffle.api.library.ExportMessage.Ignore;
@@ -87,34 +88,38 @@ public Object getMembers(@SuppressWarnings("unused") boolean includeInternal) {
8788
}
8889

8990
@ExportMessage
90-
public boolean isMemberReadable(String member) {
91+
public boolean isMemberReadable(String member,
92+
@CachedLibrary(limit = DATA_LIB_LIMIT) VectorDataLibrary dataLib) {
9193
int idx = getElementIndexByName(member);
92-
return isArrayElementReadable(idx);
94+
return isArrayElementReadable(idx, dataLib);
9395
}
9496

9597
@ExportMessage
96-
public boolean isMemberInvocable(String member) {
98+
public boolean isMemberInvocable(String member,
99+
@CachedLibrary(limit = DATA_LIB_LIMIT) VectorDataLibrary dataLib) {
97100
int idx = getElementIndexByName(member);
98-
return isArrayElementReadable(idx) && getDataAt(idx) instanceof RFunction;
101+
return isArrayElementReadable(idx, dataLib) && getDataAt(idx) instanceof RFunction;
99102
}
100103

101104
@ExportMessage
102105
public Object readMember(String member,
106+
@CachedLibrary(limit = DATA_LIB_LIMIT) VectorDataLibrary dataLib,
103107
@Cached.Exclusive @Cached() R2Foreign r2Foreign,
104108
@Cached.Shared("unknownIdentifier") @Cached("createBinaryProfile()") ConditionProfile unknownIdentifier) throws UnknownIdentifierException {
105109
int idx = getElementIndexByName(member);
106-
if (unknownIdentifier.profile(!isArrayElementReadable(idx))) {
110+
if (unknownIdentifier.profile(!isArrayElementReadable(idx, dataLib))) {
107111
throw UnknownIdentifierException.create(member);
108112
}
109113
return r2Foreign.convert(getDataAt(idx));
110114
}
111115

112116
@ExportMessage
113117
public Object invokeMember(String member, Object[] arguments,
118+
@CachedLibrary(limit = DATA_LIB_LIMIT) VectorDataLibrary dataLib,
114119
@Cached.Shared("unknownIdentifier") @Cached("createBinaryProfile()") ConditionProfile unknownIdentifier,
115120
@Cached() RFunction.ExplicitCall c) throws UnknownIdentifierException, UnsupportedMessageException {
116121
int idx = getElementIndexByName(member);
117-
if (unknownIdentifier.profile(!isArrayElementReadable(idx))) {
122+
if (unknownIdentifier.profile(!isArrayElementReadable(idx, dataLib))) {
118123
throw UnknownIdentifierException.create(member);
119124
}
120125
Object f = getDataAt(idx);

0 commit comments

Comments
 (0)