Bug 24009 - Measure invalidation causes way too much work because it usually remeasures the whole page and should be redesigned
Summary: Measure invalidation causes way too much work because it usually remeasures t...
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.0
Hardware: Macintosh Mac OS
: Normal enhancement
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2014-10-23 00:16 UTC by luca.barbieri
Modified: 2016-03-25 18:06 UTC (History)
9 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description luca.barbieri 2014-10-23 00:16:13 UTC
The current design of MeasureInvalidated causes Xamarin.Forms to potentially remeasure the whole UI on trivial changes.

For example, changing the text of a Label seems to always result in the whole UI being remeasured, at least if it's composed of StackLayouts!

This is because changing Label.Text ALWAYS raises MeasureInvalidated, which calls Layout.OnChildMeasureInvalidated, which raises MeasureInvalidated, etc. until the Page is reached.

And the layouts that are invalidated then call OnSizeRequest which calls StackLayout.SumOfSizeRequests which calls GetSizeRequest which calls OnSizeRequest and so on with no conditionals and no caching at all.

This results in the whole UI being remeasured, as far as I can tell!

This is bad in theory and can in practice result in serious performance issues with things like Android ListViews that seem to have a tendency to try to recreate views on measuring, when creating such views is also expensive.

Here are some possible ideas on how to fix this:

1. Width/height constraints should be properties rather than being parameters to OnSizeRequest, or should otherwise be remembered

2. Size requests should be cached and only invalidated on width/height constraints changes or InvalidateMeasure changes

3. MeasureInvalidated should not be raised if InvalidateMeasure() is called, but the size request did not actually change

4. Layouts should be smarter: a StackLayout in vertical orientation should not relayout itself if only the width request of a child changed

5. Size requests caching should mean that for instance if you call OnSizeRequest on a StackLayout because a child's size request changed, the size requests of all other children are not recomputed (unless the width
Comment 1 luca.barbieri 2014-10-23 00:41:19 UTC
I think I should point out more clearly the situation in which I discovered this issue and in which it has the most impact:

1. You have a Slider bound to a Label text, which is thus changed every frame when the slider is dragged
2. InvalidateMeasure() is called, which invalidates all ascendant layouts up to the page (as described in the previous message)
3. The whole page is remeasured as a resulted of the stack layouts measuring themselves (as described in the previous message)
4. All Android ListViews are measured, and they seem to be generally measured with MeasureSpec.AT_MOST
5. This calls ListView.measureHeightOfChildren, which then proceeds to call getView on all cells
6. This means that all ViewCells in the page are reinstantiated every frame
7. The rebinding of the ViewCells can be expensive

Note that there seems to be a further issue that exacerbates this where ListView.getView somehow recycles views into items at different positions but I'm not sure if this is the result of bad design or a bug and whether it's an Android or a Xamarin.Forms issue.
Comment 2 luca.barbieri 2014-10-23 00:56:57 UTC
Or to put it even more briefly, the major practical problem is that ListView.measure is extremely expensive because it always instantiates views for all visible elements even if they already exist (which is IMHO insane, but not your fault).

This clearly shows that Android design assumes that View.measure() is called infrequently, while the Xamarin.Forms design makes the opposite assumption that measuring is cheap and it can easily measure everything every frame; obviously this creates a problem.
Comment 3 George Cook 2015-05-29 17:34:59 UTC
can I add my voice to this. I think it should be redesigned is way too strong a name for this bug; but it is way too keen to invalidate siblngs.

We should have a mechanism which easily allows us to opt out of certain invalidations. I guess this can be provided by our own custom classes; but perhaps Xamarin could aid us with this a bit for some of their own layouts?
Comment 4 Jason Smith [MSFT] 2016-03-25 18:06:48 UTC
The new optimization system now prevents upward propagation of invalidation when possible.