Skip to content

Commit a3a7fc2

Browse files
amir-shalemfacebook-github-bot
authored andcommitted
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
1 parent f3ac981 commit a3a7fc2

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,7 @@ static void jni_YGNodeCalculateLayoutJNI(
367367
void* layoutContext = nullptr;
368368
auto map = PtrJNodeMapVanilla{};
369369
if (nativePointers) {
370-
size_t nativePointersSize = env->GetArrayLength(nativePointers);
371-
jlong result[nativePointersSize];
372-
env->GetLongArrayRegion(nativePointers, 0, nativePointersSize, result);
373-
374-
map = PtrJNodeMapVanilla{result, nativePointersSize, javaNodes};
370+
map = PtrJNodeMapVanilla{nativePointers, javaNodes};
375371
layoutContext = ↦
376372
}
377373

ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJTypesVanilla.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ class PtrJNodeMapVanilla {
2020

2121
public:
2222
PtrJNodeMapVanilla() : ptrsToIdxs_{}, javaNodes_{} {}
23-
PtrJNodeMapVanilla(
24-
jlong* nativePointers,
25-
size_t nativePointersSize,
26-
jobjectArray javaNodes)
23+
PtrJNodeMapVanilla(jlongArray javaNativePointers, jobjectArray javaNodes)
2724
: javaNodes_{javaNodes} {
25+
26+
JNIEnv* env = getCurrentEnv();
27+
size_t nativePointersSize = env->GetArrayLength(javaNativePointers);
28+
std::vector<jlong> nativePointers(nativePointersSize);
29+
env->GetLongArrayRegion(
30+
javaNativePointers, 0, nativePointersSize, nativePointers.data());
31+
2832
for (size_t i = 0; i < nativePointersSize; ++i) {
2933
ptrsToIdxs_[(YGNodeRef) nativePointers[i]] = i;
3034
}

0 commit comments

Comments
 (0)