You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Context: https://github.com/Redth/MauiCollectionViewGalleryFixes: #14222
This will conflict with:
* #20352
But I will rework this after it is merged.
Profiling the above sample while scrolling on a Pixel 5, a lot of time
is spent in `FormattedStringExtensions.RecalculateSpanPositions()`:
(20%) Microsoft.Maui.Controls!Microsoft.Maui.Controls.Platform.FormattedStringExtensions.RecalculateSpanPositions(Android.Widget.TextView,Microsoft.Maui.Controls.Label,Android.Text.SpannableString,Microsoft.Maui.SizeRequest)
(11%) Microsoft.Maui.Controls!Microsoft.Maui.Controls.Platform.FormattedStringExtensions.FontSpan.UpdateDrawState(Android.Text.TextPaint)
(11%) Microsoft.Maui.Controls!Microsoft.Maui.Controls.Platform.FormattedStringExtensions.FontSpan.Apply(Android.Text.TextPaint)
(6.3%) MauiCollectionViewGallery!PoolMathApp.Helpers.FormattedTextExtensions.ToFormattedString(PoolMathApp.Models.FormattedTex
The key contributors are `FontSpan` and `LineHeightSpan` which:
* Implement `MetricAffectingSpan`, an abstract class that allows to
change the metrics of the text.
* Implement `UpdateDrawState()` and `Apply()` methods that are called
during draw. Causing frequent Java -> C# interop.
To fix this, let's move the following types from C# to Java:
* `FontSpan` -> `PlatformFontSpan`
* `LetterSpacingSpan` -> `PlatformFontSpan` (use different ctor)
* `LineHeightSpan` -> `PlatformLineHeightSpan`
`PlatformLineHeightSpan` is similar, as it is an implementation of the
`LineHeightSpan` interface.
With these changes, I see a nice improvement while scrolling:
(5.5%) Microsoft.Maui.Controls!Microsoft.Maui.Controls.Platform.FormattedStringExtensions.RecalculateSpanPositions(Android.Widget.TextView,Microsoft.Maui.Controls.Label,Android.Text.SpannableString,Microsoft.Maui.SizeRequest)
(4.0%) MauiCollectionViewGallery!PoolMathApp.Helpers.FormattedTextExtensions.ToFormattedString(PoolMathApp.Models.FormattedText
`RecalculateSpanPositions` overall, went from 20% to 5.5%!
Comparing the new types, the times spent calling the constructors are
also improved:
Before:
(1.1%) Microsoft.Maui.Controls!Microsoft.Maui.Controls.Platform.FormattedStringExtensions.FontSpan..ctor(Microsoft.Maui.Font,M
(1.5%) Microsoft.Maui.Controls!Microsoft.Maui.Controls.Platform.FormattedStringExtensions.LetterSpacingSpan..ctor(double)
After:
(1.0%) Microsoft.Maui!Microsoft.Maui.PlatformFontSpan..ctor(Android.Content.Context,Android.Graphics.Typeface,bool,single)
(0.82%) Microsoft.Maui!Microsoft.Maui.PlatformFontSpan..ctor(single)
This should be reasonable for .NET 8servicing, as there should be no
behavior changes and no API changes.
In a future PR, it looks like `FormattedStringExtensions` could be
improved further, but this is a decent starting point that makes it a
lot better.
0 commit comments