Skip to content

Commit a01391b

Browse files
committed
[GR-22670] Rewrite Identical builtin to use VectorDataLibrary
(cherry picked from commit e72915d)
1 parent 71f8ee8 commit a01391b

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Identical.java

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@
3636
import com.oracle.truffle.api.dsl.TypeSystemReference;
3737
import com.oracle.truffle.api.frame.VirtualFrame;
3838
import com.oracle.truffle.api.interop.TruffleObject;
39+
import com.oracle.truffle.api.library.CachedLibrary;
3940
import com.oracle.truffle.api.object.DynamicObject;
4041
import com.oracle.truffle.api.profiles.ConditionProfile;
42+
import com.oracle.truffle.api.profiles.ValueProfile;
43+
import com.oracle.truffle.r.runtime.data.VectorDataLibrary;
4144
import com.oracle.truffle.r.runtime.data.nodes.attributes.GetAttributeNode;
4245
import com.oracle.truffle.r.runtime.data.nodes.attributes.IterableAttributeNode;
4346
import com.oracle.truffle.r.runtime.data.nodes.attributes.IterableAttributeNodeGen;
@@ -314,19 +317,28 @@ private static byte doInternalIdenticalSlowpath(RFunction x, RFunction y, boolea
314317
return identicalRecursiveAttrNode.execute(x, y, numEq, singleNA, attribAsSet, ignoreBytecode, ignoreEnvironment, ignoreSrcref, depth + 1);
315318
}
316319

317-
@Specialization(guards = "!vectorsLists(x, y)")
320+
@Specialization(guards = "!vectorsLists(x, y)", limit = "getGenericDataLibraryCacheSize()")
318321
protected static byte doInternalIdenticalGeneric(RAbstractVector x, RAbstractVector y, boolean numEq, boolean singleNA, boolean attribAsSet, boolean ignoreBytecode, boolean ignoreEnvironment,
319322
boolean ignoreSrcref, int depth,
323+
@CachedLibrary("x.getData()") VectorDataLibrary xDataLib,
324+
@CachedLibrary("y.getData()") VectorDataLibrary yDataLib,
325+
@Cached("createClassProfile()") ValueProfile xClassProfile,
326+
@Cached("createClassProfile()") ValueProfile yClassProfile,
320327
@Cached("createBinaryProfile()") ConditionProfile vecLengthProfile,
321328
@Cached("createBinaryProfile()") ConditionProfile differentTypesProfile,
322329
@Cached("createBinaryProfile()") ConditionProfile isDoubleProfile,
323330
@Cached(value = "createOrGet(depth)", uncached = "getUncached()") IdenticalRecursiveAttrNode identicalRecursiveAttrNode) {
324-
if (vecLengthProfile.profile(x.getLength() != y.getLength()) || differentTypesProfile.profile(x.getRType() != y.getRType())) {
331+
RAbstractVector xProfiled = xClassProfile.profile(x);
332+
RAbstractVector yProfiled = yClassProfile.profile(y);
333+
Object xData = xProfiled.getData();
334+
Object yData = yProfiled.getData();
335+
int xLen = xDataLib.getLength(xData);
336+
if (vecLengthProfile.profile(xLen != yDataLib.getLength(yData)) || differentTypesProfile.profile(xProfiled.getRType() != yProfiled.getRType())) {
325337
return RRuntime.LOGICAL_FALSE;
326338
} else {
327-
for (int i = 0; i < x.getLength(); i++) {
328-
Object xValue = x.getDataAtAsObject(i);
329-
Object yValue = y.getDataAtAsObject(i);
339+
for (int i = 0; i < xLen; i++) {
340+
Object xValue = xDataLib.getDataAtAsObject(xData, i);
341+
Object yValue = yDataLib.getDataAtAsObject(yData, i);
330342
if (isDoubleProfile.profile(xValue instanceof Double)) {
331343
if (identical((double) xValue, (double) yValue, numEq, singleNA) == RRuntime.LOGICAL_FALSE) {
332344
return RRuntime.LOGICAL_FALSE;
@@ -336,28 +348,40 @@ protected static byte doInternalIdenticalGeneric(RAbstractVector x, RAbstractVec
336348
}
337349
}
338350
}
339-
return identicalRecursiveAttrNode.execute(x, y, numEq, singleNA, attribAsSet, ignoreBytecode, ignoreEnvironment, ignoreSrcref, depth + 1);
351+
return identicalRecursiveAttrNode.execute(xProfiled, yProfiled, numEq, singleNA, attribAsSet, ignoreBytecode, ignoreEnvironment, ignoreSrcref, depth + 1);
340352
}
341353

342354
static IdenticalInternal createOrGet(int depth) {
343355
return isCached(depth) ? IdenticalInternal.create() : IdenticalInternalNodeGen.getUncached();
344356
}
345357

346-
@Specialization
358+
@Specialization(limit = "getTypedVectorDataLibraryCacheSize()")
347359
protected byte doInternalIdenticalGeneric(RAbstractListBaseVector x, RAbstractListBaseVector y, boolean numEq, boolean singleNA, boolean attribAsSet, boolean ignoreBytecode,
348360
boolean ignoreEnvironment, boolean ignoreSrcref, int depth,
361+
@CachedLibrary("x.getData()") VectorDataLibrary xDataLib,
362+
@CachedLibrary("y.getData()") VectorDataLibrary yDataLib,
363+
@Cached("createClassProfile()") ValueProfile xClassProfile,
364+
@Cached("createClassProfile()") ValueProfile yClassProfile,
365+
@Cached("createBinaryProfile()") ConditionProfile vecLengthProfile,
366+
@Cached("createBinaryProfile()") ConditionProfile differentTypesProfile,
349367
@Cached(value = "createOrGet(depth)", uncached = "getUncached()") IdenticalInternal identicalRecursiveNode,
350368
@Cached(value = "createOrGet(depth)", uncached = "getUncached()") IdenticalRecursiveAttrNode identicalRecursiveAttrNode) {
351-
if (x.getLength() != y.getLength()) {
369+
RAbstractVector xProfiled = xClassProfile.profile(x);
370+
RAbstractVector yProfiled = yClassProfile.profile(y);
371+
Object xData = xProfiled.getData();
372+
Object yData = yProfiled.getData();
373+
int xLen = xDataLib.getLength(xData);
374+
if (vecLengthProfile.profile(xLen != yDataLib.getLength(yData)) || differentTypesProfile.profile(xProfiled.getRType() != yProfiled.getRType())) {
352375
return RRuntime.LOGICAL_FALSE;
353376
}
354-
for (int i = 0; i < x.getLength(); i++) {
355-
byte res = identicalRecursiveNode.executeByte(x.getDataAt(i), y.getDataAt(i), numEq, singleNA, attribAsSet, ignoreBytecode, ignoreEnvironment, ignoreSrcref, depth + 1);
377+
for (int i = 0; i < xLen; i++) {
378+
byte res = identicalRecursiveNode.executeByte(xDataLib.getElementAt(xData, i), yDataLib.getElementAt(yData, i), numEq, singleNA, attribAsSet, ignoreBytecode, ignoreEnvironment,
379+
ignoreSrcref, depth + 1);
356380
if (res == RRuntime.LOGICAL_FALSE) {
357381
return RRuntime.LOGICAL_FALSE;
358382
}
359383
}
360-
return identicalRecursiveAttrNode.execute(x, y, numEq, singleNA, attribAsSet, ignoreBytecode, ignoreEnvironment, ignoreSrcref, depth + 1);
384+
return identicalRecursiveAttrNode.execute(xProfiled, yProfiled, numEq, singleNA, attribAsSet, ignoreBytecode, ignoreEnvironment, ignoreSrcref, depth + 1);
361385
}
362386

363387
@Specialization

com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37133,6 +37133,30 @@ attr(,"Rd_tag")
3713337133
[1] TRUE
3713437134
[1] FALSE
3713537135

37136+
##com.oracle.truffle.r.test.builtins.TestBuiltin_identical.testIdentical#
37137+
#identical(as.expression(list(quote(a), 33)), as.expression(list(quote(a), 33)))
37138+
[1] TRUE
37139+
37140+
##com.oracle.truffle.r.test.builtins.TestBuiltin_identical.testIdentical#
37141+
#identical(as.expression(list(quote(a), 33)), list(quote(a), 33))
37142+
[1] FALSE
37143+
37144+
##com.oracle.truffle.r.test.builtins.TestBuiltin_identical.testIdentical#
37145+
#identical(as.expression(quote(a)), list(quote(a)))
37146+
[1] FALSE
37147+
37148+
##com.oracle.truffle.r.test.builtins.TestBuiltin_identical.testIdentical#
37149+
#identical(list(quote(a), 33), list(quote(a), 33))
37150+
[1] TRUE
37151+
37152+
##com.oracle.truffle.r.test.builtins.TestBuiltin_identical.testIdentical#
37153+
#identical(list(quote(a), 33), list(quote(a), 33, 42L))
37154+
[1] FALSE
37155+
37156+
##com.oracle.truffle.r.test.builtins.TestBuiltin_identical.testIdentical#
37157+
#identical(list(quote(a), 33), list(quote(a), 33L))
37158+
[1] FALSE
37159+
3713637160
##com.oracle.truffle.r.test.builtins.TestBuiltin_identical.testIdentical#
3713737161
#identical(pairlist(1, pairlist('foo')), pairlist(1, pairlist('bar')))
3713837162
[1] FALSE

com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_identical.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,15 @@ public void testIdentical() {
282282
assertEval("a <- quote(a(100)); b <- quote(a(100)); attr(b[[2]], 'foo') <- 'baz'; attr(a[[2]], 'foo') <- 'bar'; identical(a,b)");
283283
assertEval("a <- quote(a(100)); b <- quote(a(100)); attr(b[[2]], 'foo') <- 'bar'; attr(a[[2]], 'foo') <- 'bar'; identical(a,b)");
284284
assertEval("e1 <- quote(a+1); e2 <- quote(a+2); identical(e1, e2); e2[[3]] <- c(1,2,3); identical(e1, e2); e1[[3]] <- c(1,2,3); identical(e1, e2); attr(e2[[3]], 'foo') <- 'bar'; identical(e1, e2)");
285+
286+
// lists and expressions
287+
288+
assertEval("identical(as.expression(quote(a)), list(quote(a)))");
289+
assertEval("identical(as.expression(list(quote(a), 33)), list(quote(a), 33))");
290+
assertEval("identical(list(quote(a), 33), list(quote(a), 33))");
291+
assertEval("identical(list(quote(a), 33), list(quote(a), 33L))");
292+
assertEval("identical(list(quote(a), 33), list(quote(a), 33, 42L))");
293+
assertEval("identical(as.expression(list(quote(a), 33)), as.expression(list(quote(a), 33)))");
285294
}
286295

287296
@Test

0 commit comments

Comments
 (0)