text stringlengths 1 474 |
|---|
the element need only compare the object identity of the two widget |
references in order to establish that the new widget is the same as |
the old widget. Developers exploit this optimization to implement the |
reprojection pattern, in which a widget includes a prebuilt child |
widget stored as a member variable in its build.During build, Flutter also avoids walking the parent chain using |
InheritedWidgets. If widgets commonly walked their parent chain, |
for example to determine the current theme color, the build phase |
would become O(N²) in the depth of the tree, which can be quite |
large due to aggressive composition. To avoid these parent walks, |
the framework pushes information down the element tree by maintaining |
a hash table of InheritedWidgets at each element. Typically, many |
elements will reference the same hash table, which changes only at |
elements that introduce a new InheritedWidget.<topic_end> |
<topic_start> |
Linear reconciliation |
Contrary to popular belief, Flutter does not employ a tree-diffing |
algorithm. Instead, the framework decides whether to reuse elements by |
examining the child list for each element independently using an O(N) |
algorithm. The child list reconciliation algorithm optimizes for the |
following cases:The general approach is to match up the beginning and end of both child |
lists by comparing the runtime type and key of each widget, |
potentially finding a non-empty range in the middle of each list |
that contains all the unmatched children. The framework then places |
the children in the range in the old child list into a hash table |
based on their keys. Next, the framework walks the range in the new |
child list and queries the hash table by key for matches. Unmatched |
children are discarded and rebuilt from scratch whereas matched children |
are rebuilt with their new widgets.<topic_end> |
<topic_start> |
Tree surgery |
Reusing elements is important for performance because elements own |
two critical pieces of data: the state for stateful widgets and the |
underlying render objects. When the framework is able to reuse an element, |
the state for that logical part of the user interface is preserved |
and the layout information computed previously can be reused, |
often avoiding entire subtree walks. In fact, reusing elements is |
so valuable that Flutter supports non-local tree mutations that |
preserve state and layout information.Developers can perform a non-local tree mutation by associating a GlobalKey |
with one of their widgets. Each global key is unique throughout the |
entire application and is registered with a thread-specific hash table. |
During the build phase, the developer can move a widget with a global |
key to an arbitrary location in the element tree. Rather than building |
a fresh element at that location, the framework will check the hash |
table and reparent the existing element from its previous location to |
its new location, preserving the entire subtree.The render objects in the reparented subtree are able to preserve |
their layout information because the layout constraints are the only |
information that flows from parent to child in the render tree. |
The new parent is marked dirty for layout because its child list has |
changed, but if the new parent passes the child the same layout |
constraints the child received from its old parent, the child can |
return immediately from layout, cutting off the walk.Global keys and non-local tree mutations are used extensively by |
developers to achieve effects such as hero transitions and navigation.<topic_end> |
<topic_start> |
Constant-factor optimizations |
In addition to these algorithmic optimizations, achieving aggressive |
composability also relies on several important constant-factor |
optimizations. These optimizations are most important at the leaves of |
the major algorithms discussed above.Child-model agnostic. Unlike most toolkits, which use child lists, |
Flutter’s render tree does not commit to a specific child model. |
For example, the RenderBox class has an abstract visitChildren() |
method rather than a concrete firstChild and nextSibling interface. |
Many subclasses support only a single child, held directly as a member |
variable, rather than a list of children. For example, RenderPadding |
supports only a single child and, as a result, has a simpler layout |
method that takes less time to execute.Visual render tree, logical widget tree. In Flutter, the render |
tree operates in a device-independent, visual coordinate system, |
which means smaller values in the x coordinate are always towards |
the left, even if the current reading direction is right-to-left. |
The widget tree typically operates in logical coordinates, meaning |
with start and end values whose visual interpretation depends |
on the reading direction. The transformation from logical to visual |
coordinates is done in the handoff between the widget tree and the |
render tree. This approach is more efficient because layout and |
painting calculations in the render tree happen more often than the |
widget-to-render tree handoff and can avoid repeated coordinate conversions.Text handled by a specialized render object. The vast majority |
of render objects are ignorant of the complexities of text. Instead, |
text is handled by a specialized render object, RenderParagraph, |
which is a leaf in the render tree. Rather than subclassing a |
text-aware render object, developers incorporate text into their |
user interface using composition. This pattern means RenderParagraph |
can avoid recomputing its text layout as long as its parent supplies |
the same layout constraints, which is common, even during tree surgery.Observable objects. Flutter uses both the model-observation and |
the reactive paradigms. Obviously, the reactive paradigm is dominant, |
but Flutter uses observable model objects for some leaf data structures. |
For example, Animations notify an observer list when their value changes. |
Flutter hands off these observable objects from the widget tree to the |
render tree, which observes them directly and invalidates only the |
appropriate stage of the pipeline when they change. For example, |
a change to an Animation<Color> might trigger only the paint phase |
rather than both the build and paint phases.Taken together and summed over the large trees created by aggressive |
composition, these optimizations have a substantial effect on performance.<topic_end> |
<topic_start> |
Separation of the Element and RenderObject trees |
The RenderObject and Element (Widget) trees in Flutter are isomorphic |
(strictly speaking, the RenderObject tree is a subset of the Element |
tree). An obvious simplification would be to combine these trees into |
one tree. However, in practice there are a number of benefits to having |
these trees be separate:Performance. When the layout changes, only the relevant parts of |
the layout tree need to be walked. Due to composition, the element |
tree frequently has many additional nodes that would have to be skipped.Clarity. The clearer separation of concerns allows the widget |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.