Consider copying MultiChildRenderObjectWidget.children #48321
Comments
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment has been hidden.
This comment has been hidden.
|
@icatalud The experiment is to determine whether copying child lists will improve or hurt performance. We had previously assumed it would hurt performance (because memory copying is expensive) but @rakudrama observed that it's conceivable that it would have other benefits that would actually outweigh the copying, and we should measure it to see. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
|
Labelling proposal! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One of the problems with the Flutter model is that developers can accidentally or intentionally mutate the Widget tree via mutating MultiChildRenderObjectWidget.children. See dart-lang/sdk#27755.
I think there might be a performance reason to copy the children. It depends on how often the framework walks the
children.Listis an interface that has many implementations.The VM has
_Listfor fixed-length lists and_GrowableListfor 'regular' lists, and another implementation (I don't recall the name) for unmodifiable and constLists.In a small Flutter app, all the children are likely from
Listliterals or to calls to.toList(), so thechildrenfield is likely to have just two or three implementation classes. The JITing VM and AOT try to discover this, and the three implementation classes are carefully crafted to be as alike as possible. This makes it possible to compilechildren[i]to some indexing code.It is possible that in a large Flutter app that there are other implementations of
Listthat are passed in as the children, meaning it is no longer possible to compilechildren[i]to indexing code since some of the implementations have custom indexers and.lengthgetters that must be called.My hypothesis is that as the inner loop in the framework becomes more polymorphic, the performance of this code shared by all multi-child widgets degrades.
One could test this hypothesis by taking a benchmark that is where walking the children is 'hot'. I expect the benchmark would walk the 'the same' a large Widget tree over and over, with no actual appearance change. Alter one of the constructors to pass in an
UnmodifiableListView([....])instead of[....]. This will cause the walking ofchildren[i]to become less efficient. Does this show up in the benchmark?If you decide to protect the performance by copying (which ensures there is only one kind of List), I would suggest copying with
List<Widget>.unmodifiable(input).A small fixed-length or unmodifiable list can be is half the size of a growable list or less. If the growable input is no longer referenced, it can be GC-ed. The copy will do more allocation, but have a smaller size post-GC. This may be a reasonable tradeoff.
Making the children unmodifiable will prevent users from modifying the trees they create which will help them avoid a class of bugs.
It will break the problematic pattern of
widget.children.add(w).Even if the above hypothesis does not pan out, copying the list to an unmodifiable list in debug mode would help developers avoid bugs.
The text was updated successfully, but these errors were encountered: