From 8a8dcd0187f8080ae3f692dac3f24774a2a3662c Mon Sep 17 00:00:00 2001 From: David Vacca Date: Fri, 10 Jan 2025 11:26:53 -0800 Subject: [PATCH] Cache ViewManagerDelegate on ViewManagers (#48550) Summary: Cache ViewManagerDelegate on ViewManagers changelog: [internal] internal Differential Revision: D67957883 --- .../com/facebook/react/uimanager/ViewManager.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java index 00e5d0a6501763..34e86842f8cf2e 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java @@ -41,6 +41,9 @@ public abstract class ViewManager private static final String NAME = ViewManager.class.getSimpleName(); + private boolean mIsDelegateLoaded = false; + private @Nullable ViewManagerDelegate mDelegate = null; + /** * For View recycling: we store a Stack of unused, dead Views. This is null by default, and when * null signals that View Recycling is disabled. `enableViewRecycling` must be explicitly called @@ -88,7 +91,7 @@ protected void setupViewRecycling() { * @param props {@link ReactStylesDiffMap} props to update the view with */ public void updateProperties(@NonNull T viewToUpdate, ReactStylesDiffMap props) { - final ViewManagerDelegate delegate = getDelegate(); + final ViewManagerDelegate delegate = getOrCreateViewManagerDelegate(); if (delegate != null) { ViewManagerPropertyUpdater.updateProps(delegate, viewToUpdate, props); } else { @@ -109,11 +112,14 @@ public void updateProperties(@NonNull T viewToUpdate, ReactStylesDiffMap props) * @return an instance of {@link ViewManagerDelegate} if the props of the view managed by this * view manager should be set via this delegate */ - @Nullable - protected ViewManagerDelegate getDelegate() { + protected @Nullable ViewManagerDelegate getDelegate() { return null; } + private @Nullable ViewManagerDelegate getOrCreateViewManagerDelegate() { + return mIsDelegateLoaded ? mDelegate : (mDelegate = getDelegate()); + } + /** Creates a view with knowledge of props and state. */ public @NonNull T createView( int reactTag, @@ -306,7 +312,7 @@ public void receiveCommand(@NonNull T root, int commandId, @Nullable ReadableArr * @param args optional arguments for the command */ public void receiveCommand(@NonNull T root, String commandId, @Nullable ReadableArray args) { - final ViewManagerDelegate delegate = getDelegate(); + final ViewManagerDelegate delegate = getOrCreateViewManagerDelegate(); if (delegate != null) { delegate.receiveCommand(root, commandId, args); }