text
stringlengths
1
372
flows from child to parent is the geometry. these invariants can reduce
the amount of work required during layout:
if the child has not marked its own layout as dirty, the child can
return immediately from layout, cutting off the walk, as long as the
parent gives the child the same constraints as the child received
during the previous layout.
whenever a parent calls a child’s layout method, the parent indicates
whether it uses the size information returned from the child. if,
as often happens, the parent does not use the size information,
then the parent need not recompute its layout if the child selects
a new size because the parent is guaranteed that the new size will
conform to the existing constraints.
tight constraints are those that can be satisfied by exactly one
valid geometry. for example, if the min and max widths are equal to
each other and the min and max heights are equal to each other,
the only size that satisfies those constraints is one with that
width and height. if the parent provides tight constraints,
then the parent need not recompute its layout whenever the child
recomputes its layout, even if the parent uses the child’s size
in its layout, because the child cannot change size without new
constraints from its parent.
a render object can declare that it uses the constraints provided
by the parent only to determine its geometry. such a declaration
informs the framework that the parent of that render object does
not need to recompute its layout when the child recomputes its layout
even if the constraints are not tight and even if the parent’s
layout depends on the child’s size, because the child cannot change
size without new constraints from its parent.
as a result of these optimizations, when the render object tree contains
dirty nodes, only those nodes and a limited part of the subtree around
them are visited during layout.
<topic_end>
<topic_start>
sublinear widget building
similar to the layout algorithm, flutter’s widget building algorithm
is sublinear. after being built, the widgets are held by the element
tree, which retains the logical structure of the user interface.
the element tree is necessary because the widgets themselves are
immutable, which means (among other things), they cannot remember their
parent or child relationships with other widgets. the element tree also
holds the state objects associated with stateful widgets.
in response to user input (or other stimuli), an element can become dirty,
for example if the developer calls setState() on the associated state
object. the framework keeps a list of dirty elements and jumps directly
to them during the build phase, skipping over clean elements. during
the build phase, information flows unidirectionally down the element
tree, which means each element is visited at most once during the build
phase. once cleaned, an element cannot become dirty again because,
by induction, all its ancestor elements are also
clean4.
because widgets are immutable, if an element has not marked itself as
dirty, the element can return immediately from build, cutting off the walk,
if the parent rebuilds the element with an identical widget. moreover,
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