Native View Hierarchy Optimizer
Class responsible for optimizing the native view hierarchy while still respecting the final UI product specified by JS. Basically, JS sends us a hierarchy of nodes that, while easy to reason about in JS, are very inefficient to translate directly to native views. This class sits in between UIManagerModule, which directly receives view commands from JS, and , 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