Skip to content

Commit fd0a007

Browse files
author
Pavel Marek
committed
Remove RStringVector.getWrappedDataAt and setWrappedDataAt methods
(cherry picked from commit 2d176b2)
1 parent 1bdbda7 commit fd0a007

File tree

8 files changed

+66
-55
lines changed

8 files changed

+66
-55
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,17 +26,19 @@
2626
import com.oracle.truffle.api.dsl.Fallback;
2727
import com.oracle.truffle.api.dsl.Specialization;
2828
import com.oracle.truffle.api.dsl.TypeSystemReference;
29+
import com.oracle.truffle.api.library.CachedLibrary;
2930
import com.oracle.truffle.api.profiles.ConditionProfile;
3031
import com.oracle.truffle.r.nodes.unary.CastStringNode;
3132
import com.oracle.truffle.r.runtime.RInternalError;
3233
import com.oracle.truffle.r.runtime.RRuntime;
3334
import com.oracle.truffle.r.runtime.data.CharSXPWrapper;
35+
import com.oracle.truffle.r.runtime.data.RComplexVector;
3436
import com.oracle.truffle.r.runtime.data.RDoubleVector;
37+
import com.oracle.truffle.r.runtime.data.RStringVector;
3538
import com.oracle.truffle.r.runtime.data.RSymbol;
3639
import com.oracle.truffle.r.runtime.data.RTypes;
40+
import com.oracle.truffle.r.runtime.data.VectorDataLibrary;
3741
import com.oracle.truffle.r.runtime.data.model.RAbstractAtomicVector;
38-
import com.oracle.truffle.r.runtime.data.RComplexVector;
39-
import com.oracle.truffle.r.runtime.data.RStringVector;
4042

4143
@TypeSystemReference(RTypes.class)
4244
public abstract class AsCharNode extends FFIUpCallNode.Arg1 {
@@ -51,14 +53,16 @@ protected CharSXPWrapper asChar(CharSXPWrapper obj) {
5153
}
5254

5355
@Specialization
54-
protected CharSXPWrapper asChar(RStringVector obj, @Cached("createBinaryProfile()") ConditionProfile profile, @Cached("createBinaryProfile()") ConditionProfile naProfile,
55-
@Cached("createBinaryProfile()") ConditionProfile isNativized,
56-
@Cached("createBinaryProfile()") ConditionProfile wrapProfile) {
57-
if (profile.profile(obj.getLength() == 0)) {
56+
protected CharSXPWrapper asChar(RStringVector obj,
57+
@Cached("createBinaryProfile()") ConditionProfile zeroLengthProfile,
58+
@Cached("createBinaryProfile()") ConditionProfile naProfile,
59+
@CachedLibrary(limit = "getTypedVectorDataLibraryCacheSize()") VectorDataLibrary dataLib) {
60+
if (zeroLengthProfile.profile(obj.getLength() == 0)) {
5861
return CharSXPWrapper_NA;
5962
} else {
60-
obj.wrapStrings(isNativized, wrapProfile);
61-
CharSXPWrapper result = obj.getWrappedDataAt(0);
63+
Object newCharSXPData = dataLib.materializeCharSXPStorage(obj.getData());
64+
obj.setData(newCharSXPData);
65+
CharSXPWrapper result = dataLib.getCharSXPAt(obj.getData(), 0);
6266
return naProfile.profile(RRuntime.isNA(result.getContents())) ? CharSXPWrapper_NA : result;
6367
}
6468
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -62,8 +62,6 @@
6262
import java.util.concurrent.atomic.AtomicInteger;
6363
import java.util.function.Function;
6464

65-
import static com.oracle.truffle.r.runtime.RInternalError.unimplemented;
66-
6765
public final class RDataFactory {
6866
private abstract static class StaticVectorFactory extends BaseVectorFactory {
6967

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@ public CharSXPWrapper[] getData() {
4949
return data;
5050
}
5151

52-
public CharSXPWrapper getWrappedAt(int index) {
53-
return data[index];
54-
}
55-
56-
public void setWrappedAt(int index, CharSXPWrapper value) {
57-
data[index] = value;
58-
}
59-
6052
@ExportMessage
6153
public NACheck getNACheck() {
6254
return NACheck.getEnabled();

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public RStringVecNativeData materialize() {
8383
return this;
8484
}
8585

86+
@ExportMessage
87+
public RStringVecNativeData materializeCharSXPStorage() {
88+
return this;
89+
}
90+
8691
@ExportMessage
8792
public boolean isWriteable() {
8893
return true;
@@ -148,6 +153,21 @@ public String getString(@SuppressWarnings("unused") RandomAccessIterator it, int
148153
return NativeDataAccess.getData(vec, null, index);
149154
}
150155

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

153173
@ExportMessage
@@ -174,4 +194,19 @@ public void setNextString(SeqWriteIterator it, String value) {
174194
public void setString(@SuppressWarnings("unused") RandomAccessWriteIterator it, int index, String value) {
175195
NativeDataAccess.setData(vec, data, index, CharSXPWrapper.create(value));
176196
}
197+
198+
@ExportMessage
199+
public void setCharSXPAt(int index, CharSXPWrapper value) {
200+
data[index] = value;
201+
}
202+
203+
@ExportMessage
204+
public void setNextCharSXP(SeqWriteIterator it, CharSXPWrapper value) {
205+
data[it.getIndex()] = value;
206+
}
207+
208+
@ExportMessage
209+
public void setCharSXP(@SuppressWarnings("unused") RandomAccessWriteIterator it, int index, CharSXPWrapper value) {
210+
data[index] = value;
211+
}
177212
}

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

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

25-
import java.util.Arrays;
26-
2725
import com.oracle.truffle.api.dsl.Cached;
2826
import com.oracle.truffle.api.interop.InteropLibrary;
2927
import com.oracle.truffle.api.interop.TruffleObject;
@@ -53,6 +51,8 @@
5351
import com.oracle.truffle.r.runtime.data.nodes.VectorAccess;
5452
import com.oracle.truffle.r.runtime.ops.na.NACheck;
5553

54+
import java.util.Arrays;
55+
5656
@ExportLibrary(InteropLibrary.class)
5757
@ExportLibrary(AbstractContainerLibrary.class)
5858
public final class RStringVector extends RAbstractAtomicVector implements RMaterializedVector, Shareable {
@@ -122,8 +122,7 @@ public static RStringVector createAltString(AltStringClassDescriptor descriptor,
122122
altStringVector.setAltRep();
123123
altStringVector.data = altStringVecData;
124124
// This is a workaround, because we already have to invoke some altrep methods in
125-
// getLengthMethodUncached
126-
// and for that we need non-null owner.
125+
// getLengthMethodUncached and for that we need non-null owner.
127126
altStringVecData.setOwner(altStringVector);
128127
int length = AltrepUtilities.getLengthUncached(altStringVector);
129128
altStringVector.setData(altStringVecData, length);
@@ -390,7 +389,7 @@ public Object getDataAtAsObject(int index) {
390389
public void setElement(int i, Object value) {
391390
assert value instanceof CharSXPWrapper;
392391
wrapStrings();
393-
setWrappedDataAt(i, (CharSXPWrapper) value);
392+
VectorDataLibrary.getFactory().getUncached().setCharSXPAt(data, i, (CharSXPWrapper) value);
394393
}
395394

396395
/**
@@ -424,39 +423,19 @@ public void wrapStrings() {
424423
* wrapped.
425424
*/
426425
@SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", justification = "intentional")
427-
public void wrapStrings(ConditionProfile isNativized, ConditionProfile needsWrapping) {
426+
public void wrapStrings(ConditionProfile isNativized, ConditionProfile isWrapped) {
428427
if (isNativized.profile(!isNativized())) {
429-
Object oldData = data;
430-
if (needsWrapping.profile(oldData instanceof RStringCharSXPData)) {
428+
VectorDataLibrary dataLib = getUncachedDataLib();
429+
Object newCharSXPData = dataLib.materializeCharSXPStorage(data);
430+
if (isWrapped.profile(newCharSXPData == data)) {
431431
return;
432432
}
433-
oldData = getUncachedDataLib().materialize(oldData);
434-
Object newData = ((RStringArrayVectorData) oldData).wrapStrings();
435433
fence = 42; // make sure the array is really initialized before we set it to this.data
436-
setData(newData, getUncachedDataLib().getLength(newData));
434+
setData(newCharSXPData, dataLib.getLength(newCharSXPData));
437435
}
438436
verifyData();
439437
}
440438

441-
public CharSXPWrapper getWrappedDataAt(int index) {
442-
if (!isNativized()) {
443-
assert data instanceof RStringCharSXPData : "wrap the string vector data with wrapStrings() before using getWrappedDataAt(int)";
444-
return ((RStringCharSXPData) data).getWrappedAt(index);
445-
} else {
446-
return NativeDataAccess.getStringNativeMirrorData(getNativeMirror(), index);
447-
}
448-
}
449-
450-
public void setWrappedDataAt(int index, CharSXPWrapper elem) {
451-
if (!isNativized()) {
452-
wrapStrings();
453-
assert data instanceof RStringCharSXPData : "wrap the string vector data with wrapStrings() before using getWrappedDataAt(int)";
454-
((RStringCharSXPData) data).setWrappedAt(index, elem);
455-
} else {
456-
((RStringVecNativeData) data).setWrappedStringAt(index, elem);
457-
}
458-
}
459-
460439
protected static RStringVector createStringVector(Object[] vecData, boolean isComplete, int[] dims) {
461440
if (vecData instanceof String[]) {
462441
return RDataFactory.createStringVector((String[]) vecData, isComplete, dims);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -373,19 +373,19 @@ public RStringCharSXPData materializeCharSXPStorage(@CachedLibrary("this.data")
373373

374374
@ExportMessage
375375
public CharSXPWrapper getCharSXPAt(int index,
376-
@CachedLibrary("this.data") VectorDataLibrary dataLib) {
376+
@CachedLibrary("this.data") VectorDataLibrary dataLib) {
377377
return dataLib.getCharSXPAt(data, index);
378378
}
379379

380380
@ExportMessage
381381
public CharSXPWrapper getNextCharSXP(SeqIterator it,
382-
@CachedLibrary("this.data") VectorDataLibrary dataLib) {
382+
@CachedLibrary("this.data") VectorDataLibrary dataLib) {
383383
return dataLib.getNextCharSXP(data, it);
384384
}
385385

386386
@ExportMessage
387387
public CharSXPWrapper getCharSXP(RandomAccessIterator it, int index,
388-
@CachedLibrary("this.data") VectorDataLibrary dataLib) {
388+
@CachedLibrary("this.data") VectorDataLibrary dataLib) {
389389
return dataLib.getCharSXP(data, it, index);
390390
}
391391

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ public boolean isWriteable(Object data) {
150150
* Similarly to {@link #materialize(Object)}, returns an instance of object that implements
151151
* {@link VectorDataLibrary}. Moreover, the receiver should transform into an object containing
152152
* {@link CharSXPWrapper} data, if possible.
153+
*
154+
* If the result is the same as the {@code data}, it means that the receiver already has
155+
* materialized CharSXP storage.
153156
*
154157
* @see #materialize(Object)
155158
*/

0 commit comments

Comments
 (0)