[controls] improve perf of "merged" ResourceDictionary lookups #21334
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Applies to: #18505
Context: https://github.com/dotnet/maui/files/13251041/MauiCollectionView.zip
I profiled the above sample with
dotnet-trace
with the following PRs applied locally:OnLayout()
forLabel
#21291While scrolling, a lot of time is spent in
ResourceDictionary
lookups on an Android Pixel 5 device:Drilling in, I can see System.Linq's
Reverse()
method:Reverse()
can be problematic as it can sometimes create a copy of the entire collection, in order to sort in reverse. We can just use a reversefor
-loop instead.The indexer, we can also avoid a double-lookup:
And instead do:
The MAUI project template seems to setup a few "merged"
ResourceDictionary
as it containsStyles.xaml
, so this is why this code path is being hit.I wrote a BenchmarkDotNet benchmark, and it indicates the collection is being copied, as the 872 bytes of allocation occur:
With these changes in place, I see less time spent inside:
The benchmark no longer allocates either:
This should improve the performance "parenting" of any MAUI view on all platforms -- as well as scrolling
CollectionView
.