Investigate layout behavior related to TextBlock
This suggestion came up during a perf trace review. We should check if TextBlock is measuring itself multiple times (e.g. once outside of the Xaml frame and again later). Measuring text is expensive so we need to ensure we're only doing it when necessary.
The current RNW implementation also has support for fast path text (which the native platform supports). We might want to consider surfacing that in vnext as well for advanced apps to take advantage of in Windows.
I chatted with codendone about the question of whether or not text will measure itself twice.
kmelmon says: One topic that came up in a recent meeting: During yoga layout (which happens on the UI thread outside the tick) we explicitly call TextBlock::Measure for text elements, and feed the resulting desired size into the yoga layout engine. We then absolutely position everything and let XAML tick from there. Question: Will XAML measure TextBlock again during its layout pass? I’m thinking most likely we are in fact measuring twice. Not sure if the answer varies between fast-path and slow path as well.
In response, codendone says: Measuring twice is possible, but not guaranteed. Obviously if it gets marked as measure dirty after your Measure call then it will certainly measure again in the next layout pass. Or it will measure again if the availableSize which you pass in to Measure differs from the availableSize which gets passed from its parent panel during the layout pass.
Secondary measure calls are usually far less expensive than the first, due to taking advantage of data cached on the first render. We take advantage of this for wrapping text, since Arrange ends up doing a second layout with the new layout width which is frequently not known during Measure (assuming a hard-coded Width isn't involved). I didn't readily have any perf numbers for this, so I just did a quick test with my recent RichEditBox perf test app, testing first and second measure of 1000 RichTextBlocks:
First measure time: 00:00:00.2839353 Second measure time: 00:00:00.0016841
Here are numbers for TextBlock on the fast path:
First measure time: 00:00:00.0509704 Second measure time: 00:00:00.0007370
And a run from TextBlock on the slow path:
First measure time: 00:00:00.1550874 Second measure time: 00:00:00.0004143
@kmelmon were you able to get a RNW apps to hit the XAML fast path for text measurements? If so could you share the app? For our prototype we are hitting the slow path, and I'm looking for any suggestions to help figure out why.
My main concern is that all RNW text is hitting slow path.
@stecrain I'm looking into this now... XAML automatically detects when it can use fast-path, and falls back to slow-path under several conditions: -If CharacterSpacing not set to the default: letterSpacing/characterSpacing are the RN properties -TextTrimming is set to "Clip": ellipsizeMode is the RN property -If Language requires number substitution: I don't know if there's an RN property for this. I don't believe XAML will compute it. Probably not critical for this investigation. -If a custom font is being used: I don't think there's any way to do this in RN. -Using "Complex Content": This is the most likely case that will cause us issues. The easy way to think about this is if TextBlock has multiple inlines then this is complex. This will happen if you nest Text within Text. However there's also a more subtle detail... care needs to be taken not to use the TextBlock's InlineCollection for the simple case of a single run of text, as this counts as complex. This is what I'm digging into now, should have an update soon.
@stecrain Confirmed, our current code is always using the TextBlock's InlineCollection, even in the simple case of just having a single run of text. This means we currently always use slow path. I filed this issue to track: https://github.com/microsoft/react-native-windows/issues/2620
@kmelmon, should we close this issue in favor of tracking a fix for #2620 or is there more investigation that you believe is warranted?
Thanks for looking into this
Based on Mike's numbers, I don't see double-Measure being a problem. This just leaves #2620, closing this.