From 724e4f6166546f21f106ca84817489324747b4be Mon Sep 17 00:00:00 2001
From: Lukas Stadler <lukas.stadler@oracle.com>
Date: Wed, 22 Mar 2017 09:39:53 +0100
Subject: [PATCH] cache last successful index in findNativeArray

---
 .../fficall/src/jni/rffiutils.c                      | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.c b/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.c
index 07f347eec3..bec561670d 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.c
@@ -69,6 +69,7 @@ typedef struct nativeArrayTable_struct {
 static NativeArrayElem *nativeArrayTable;
 // hwm of nativeArrayTable
 static int nativeArrayTableHwm;
+static int nativeArrayTableLastIndex;
 static int nativeArrayTableLength;
 static void releaseNativeArray(JNIEnv *env, int index);
 
@@ -209,6 +210,16 @@ void updateNativeArrays(JNIEnv *env) {
 
 
 static void *findNativeArray(JNIEnv *env, SEXP x) {
+    if (nativeArrayTableLastIndex < nativeArrayTableHwm) {
+        NativeArrayElem cv = nativeArrayTable[nativeArrayTableLastIndex];
+        if (cv.obj != NULL && (cv.obj == x || (*env)->IsSameObject(env, cv.obj, x))) {
+            void *data = cv.data;
+#if TRACE_NATIVE_ARRAYS
+            fprintf(traceFile, "findNativeArray(%p): found %p (cached)\n", x, data);
+#endif
+            return data;
+        }
+    }
     int i;
     assert(isValidJNIRef(env, x));
     for (i = 0; i < nativeArrayTableHwm; i++) {
@@ -216,6 +227,7 @@ static void *findNativeArray(JNIEnv *env, SEXP x) {
         if (cv.obj != NULL) {
             assert(isValidJNIRef(env, cv.obj));
             if ((*env)->IsSameObject(env, cv.obj, x)) {
+                nativeArrayTableLastIndex = i;
                 void *data = cv.data;
 #if TRACE_NATIVE_ARRAYS
                 fprintf(traceFile, "findNativeArray(%p): found %p\n", x, data);
-- 
GitLab