From a3a7fc2c940681ef111edf680f1ab64b54e9d2bf Mon Sep 17 00:00:00 2001 From: Amir Shalem Date: Thu, 3 Jun 2021 07:02:49 -0700 Subject: [PATCH] Don't allocate large arrays on stack when copying native pointers, use heap based array Summary: Don't allocate large arrays on stack when copying native pointers, use heap based array. Today the code copies the native pointers on the stack, since it may be too big, lets make sure to use heap based allocating using std::vector. This array is afterwards converted into a reversed map from index to pointer, so it is heap based anyhow. Changelog: [Internal] Don't allocate large arrays on stack when copying native pointers, use heap based array Reviewed By: Andrey-Mishanin Differential Revision: D28747213 fbshipit-source-id: da69b4b2d0960fdade9f07f44654b30d6dacc43a --- .../jni/first-party/yogajni/jni/YGJNIVanilla.cpp | 6 +----- .../jni/first-party/yogajni/jni/YGJTypesVanilla.h | 12 ++++++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp index 3503be401f4..71fe98f3e75 100644 --- a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp +++ b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp @@ -367,11 +367,7 @@ static void jni_YGNodeCalculateLayoutJNI( void* layoutContext = nullptr; auto map = PtrJNodeMapVanilla{}; if (nativePointers) { - size_t nativePointersSize = env->GetArrayLength(nativePointers); - jlong result[nativePointersSize]; - env->GetLongArrayRegion(nativePointers, 0, nativePointersSize, result); - - map = PtrJNodeMapVanilla{result, nativePointersSize, javaNodes}; + map = PtrJNodeMapVanilla{nativePointers, javaNodes}; layoutContext = ↦ } diff --git a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJTypesVanilla.h b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJTypesVanilla.h index 8f8c778609b..d1202b755fa 100644 --- a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJTypesVanilla.h +++ b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJTypesVanilla.h @@ -20,11 +20,15 @@ class PtrJNodeMapVanilla { public: PtrJNodeMapVanilla() : ptrsToIdxs_{}, javaNodes_{} {} - PtrJNodeMapVanilla( - jlong* nativePointers, - size_t nativePointersSize, - jobjectArray javaNodes) + PtrJNodeMapVanilla(jlongArray javaNativePointers, jobjectArray javaNodes) : javaNodes_{javaNodes} { + + JNIEnv* env = getCurrentEnv(); + size_t nativePointersSize = env->GetArrayLength(javaNativePointers); + std::vector nativePointers(nativePointersSize); + env->GetLongArrayRegion( + javaNativePointers, 0, nativePointersSize, nativePointers.data()); + for (size_t i = 0; i < nativePointersSize; ++i) { ptrsToIdxs_[(YGNodeRef) nativePointers[i]] = i; }