Skip to content

Commit

Permalink
Prevent View Preallocation (#45163)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #45163

Avoid view preallocation when rendering on the main thread

Changelog: [Internal]

Reviewed By: rubennorte

Differential Revision: D58833983

fbshipit-source-id: a942d1fac684be5a8073941dbf043ba1d738e3a0
  • Loading branch information
Thomas Nardone authored and facebook-github-bot committed Jul 2, 2024
1 parent d999607 commit 0ba2e9a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,14 @@ private void preallocateView(
isLayoutable));
}

@SuppressLint("NotInvokedPrivateMethod")
@SuppressWarnings("unused")
@AnyThread
@ThreadConfined(ANY)
private boolean isOnMainThread() {
return UiThreadUtil.isOnUiThread();
}

@SuppressWarnings("unused")
@AnyThread
@ThreadConfined(ANY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ void Binding::schedulerDidRequestPreliminaryViewAllocation(
if (!mountingManager) {
return;
}
mountingManager->preallocateShadowView(shadowNode);
mountingManager->maybePreallocateShadowView(shadowNode);
}

void Binding::schedulerDidDispatchCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,11 +784,16 @@ void FabricMountingManager::executeMount(
env->DeleteLocalRef(intBufferArray);
}

void FabricMountingManager::preallocateShadowView(
void FabricMountingManager::maybePreallocateShadowView(
const ShadowNode& shadowNode) {
if (!shadowNode.getTraits().check(ShadowNodeTraits::Trait::FormsView)) {
return;
}
static thread_local bool onMainThread = isOnMainThread();
if (onMainThread) {
// View preallocation is not beneficial when rendering on the main thread
return;
}

SystraceSection section("FabricMountingManager::preallocateShadowView");

Expand Down Expand Up @@ -844,6 +849,13 @@ void FabricMountingManager::preallocateShadowView(
isLayoutableShadowNode);
}

bool FabricMountingManager::isOnMainThread() {
static auto isOnMainThread =
JFabricUIManager::javaClassStatic()->getMethod<jboolean()>(
"isOnMainThread");
return isOnMainThread(javaUIManager_);
}

void FabricMountingManager::dispatchCommand(
const ShadowView& shadowView,
const std::string& commandName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FabricMountingManager final {

void onSurfaceStop(SurfaceId surfaceId);

void preallocateShadowView(const ShadowNode& shadowNode);
void maybePreallocateShadowView(const ShadowNode& shadowNode);

void executeMount(const MountingTransaction& transaction);

Expand All @@ -55,6 +55,8 @@ class FabricMountingManager final {
void onAllAnimationsComplete();

private:
bool isOnMainThread();

jni::global_ref<JFabricUIManager::javaobject> javaUIManager_;

std::recursive_mutex commitMutex_;
Expand Down

0 comments on commit 0ba2e9a

Please sign in to comment.