public class NativeViewHierarchyOptimizer
extends java.lang.Object
UIManagerModule
, which directly receives view commands from JS, and UIViewOperationQueue
, which enqueues actual operations on the native view hierarchy. It is able
to take instructions from UIManagerModule and output instructions to the native view hierarchy
that achieve the same displayed UI but with fewer views.
Currently this class is only used to remove layout-only views, that is to say views that only affect the positions of their children but do not draw anything themselves. These views are fairly common because 1) containers are used to do layouting via flexbox and 2) the return of each Component#render() call in JS must be exactly one view, which means views are often wrapped in a unnecessary layer of hierarchy.
This optimization is implemented by keeping track of both the unoptimized JS hierarchy and the
optimized native hierarchy in ReactShadowNode
.
This optimization is important for view hierarchy depth (which can cause stack overflows during view traversal for complex apps), memory usage, amount of time spent during GCs, and time-to-display.
Some examples of the optimizations this class will do based on commands from JS: - Create a
view with only layout props: a description of that view is created as a ReactShadowNode
in UIManagerModule, but this class will not output any commands to create the view in the native
view hierarchy. - Update a layout-only view to have non-layout props: before issuing the
updateShadowNode call to the native view hierarchy, issue commands to create the view we
optimized away move it into the view hierarchy - Manage the children of a view: multiple
manageChildren calls for various parent views may be issued to the native view hierarchy
depending on where the views being added/removed are attached in the optimized hierarchy
Constructor and Description |
---|
NativeViewHierarchyOptimizer(UIViewOperationQueue uiViewOperationQueue,
ShadowNodeRegistry shadowNodeRegistry) |
Modifier and Type | Method and Description |
---|---|
static void |
assertNodeSupportedWithoutOptimizer(ReactShadowNode node) |
void |
handleCreateView(ReactShadowNode node,
ThemedReactContext themedContext,
ReactStylesDiffMap initialProps)
Handles a createView call.
|
void |
handleForceViewToBeNonLayoutOnly(ReactShadowNode node) |
void |
handleManageChildren(ReactShadowNode nodeToManage,
int[] indicesToRemove,
int[] tagsToRemove,
ViewAtIndex[] viewsToAdd,
int[] tagsToDelete)
Handles a manageChildren call.
|
static void |
handleRemoveNode(ReactShadowNode node)
Handles native children cleanup when css node is removed from hierarchy
|
void |
handleSetChildren(ReactShadowNode nodeToManage,
ReadableArray childrenTags)
Handles a setChildren call.
|
void |
handleUpdateLayout(ReactShadowNode node)
Handles an updateLayout call.
|
void |
handleUpdateView(ReactShadowNode node,
java.lang.String className,
ReactStylesDiffMap props)
Handles an updateView call.
|
void |
onBatchComplete()
Processes the shadow hierarchy to dispatch all necessary updateLayout calls to the native
hierarchy.
|
public NativeViewHierarchyOptimizer(UIViewOperationQueue uiViewOperationQueue, ShadowNodeRegistry shadowNodeRegistry)
public static void assertNodeSupportedWithoutOptimizer(ReactShadowNode node)
public void handleCreateView(ReactShadowNode node, ThemedReactContext themedContext, ReactStylesDiffMap initialProps)
public static void handleRemoveNode(ReactShadowNode node)
public void handleUpdateView(ReactShadowNode node, java.lang.String className, ReactStylesDiffMap props)
public void handleManageChildren(ReactShadowNode nodeToManage, int[] indicesToRemove, int[] tagsToRemove, ViewAtIndex[] viewsToAdd, int[] tagsToDelete)
NB: the assumption for calling this method is that all corresponding ReactShadowNodes have been updated **but tagsToDelete have NOT been deleted yet**. This is because we need to use the metadata from those nodes to figure out the correct commands to dispatch. This is unlike all other calls on this class where we assume all operations on the shadow hierarchy have already completed by the time a corresponding method here is called.
public void handleSetChildren(ReactShadowNode nodeToManage, ReadableArray childrenTags)
public void handleUpdateLayout(ReactShadowNode node)
public void handleForceViewToBeNonLayoutOnly(ReactShadowNode node)
public void onBatchComplete()