maui icon indicating copy to clipboard operation
maui copied to clipboard

[iOS] Improve background layer frame mapping performance

Open albyrock87 opened this issue 1 year ago • 15 comments

Important

This PR Obsoletes a couple of methods.

static Microsoft.Maui.Controls.Platform.BrushExtensions.UpdateBackgroundLayer(this UIKit.UIView view) -> void
static Microsoft.Maui.Platform.ViewExtensions.UpdateBackgroundLayerFrame(this UIKit.UIView! view) -> void

Description of Change

Using https://github.com/davidortinau/AllTheLists app, and scrolling through collection view with Border inside we can clearly see that a good amount of time is spent in UpdateBackgroundLayerFrame which is needed to sync the sublayer with the UIView.Layer.

image

This has an impact on both MappingFrame and ContentView.LayoutSubviews: image

Which translated to these timings image image

This PR gets rid of all mapping frame code and simply adds an observer on the sublayer.

As a result we get a lot of improvement (80%): image image

While the new observer is super fast image

Issues Fixed

Fixes #24847

albyrock87 avatar Sep 20 '24 14:09 albyrock87

/azp run

PureWeen avatar Sep 20 '24 18:09 PureWeen

Azure Pipelines successfully started running 3 pipeline(s).

azure-pipelines[bot] avatar Sep 20 '24 18:09 azure-pipelines[bot]

Failing tests definitely unrelated :)

albyrock87 avatar Sep 21 '24 15:09 albyrock87

I think this will need to retarget net9.0 , right @PureWeen ?

It doesn't really matter at this point since we aren't releasing anymore SRs from main

To make our lives easier though :-) targeting net9.0 is helpful so we don't have to deal with any surprises once we merge up

PureWeen avatar Sep 24 '24 21:09 PureWeen

I'll take the time to install .NET9 preview and retarget this one on top of that branch.

albyrock87 avatar Sep 24 '24 21:09 albyrock87

@rmarinho @PureWeen I've rebased this onto net9.0 branch and retargeted the PR. May you run /AZP again? thanks

albyrock87 avatar Sep 25 '24 12:09 albyrock87

/azp run

rmarinho avatar Sep 25 '24 12:09 rmarinho

Azure Pipelines successfully started running 3 pipeline(s).

azure-pipelines[bot] avatar Sep 25 '24 12:09 azure-pipelines[bot]

/rebase

PureWeen avatar Oct 03 '24 22:10 PureWeen

/azp run

PureWeen avatar Oct 03 '24 22:10 PureWeen

Azure Pipelines successfully started running 3 pipeline(s).

azure-pipelines[bot] avatar Oct 03 '24 22:10 azure-pipelines[bot]

/rebase

PureWeen avatar Oct 10 '24 17:10 PureWeen

/azp run

PureWeen avatar Oct 10 '24 19:10 PureWeen

Azure Pipelines successfully started running 3 pipeline(s).

azure-pipelines[bot] avatar Oct 10 '24 19:10 azure-pipelines[bot]

Hold on on this one, I have to verify something.

albyrock87 avatar Nov 11 '24 12:11 albyrock87

So I've come across random crashes in DeviceTests in base.RemoveFromSuperview() invocation and I found out that the problem was strangely caused by the observer.

After an in depth investigation I think I've found the root cause of the problem which I strongly believe lies in Action<NSObservedChange> cback; not being a weak reference too.

This brought me to implement a low-level solution which is now safe (I can't reproduce the crash anymore). https://github.com/dotnet/maui/pull/24848/commits/d8763bfb933a10ea8ba40cb3f872d8068e7b5e62

albyrock87 avatar Nov 11 '24 16:11 albyrock87

/azp run

jsuarezruiz avatar Nov 11 '24 16:11 jsuarezruiz

Azure Pipelines successfully started running 3 pipeline(s).

azure-pipelines[bot] avatar Nov 11 '24 16:11 azure-pipelines[bot]

I've added a device test to address memory leak / crash concerns.

albyrock87 avatar Nov 20 '24 14:11 albyrock87

/azp run

PureWeen avatar Nov 27 '24 04:11 PureWeen

Azure Pipelines successfully started running 3 pipeline(s).

azure-pipelines[bot] avatar Nov 27 '24 04:11 azure-pipelines[bot]

Failed test is unrelated.

albyrock87 avatar Nov 27 '24 19:11 albyrock87

I've included @Tamilarasan-Paranthaman UI test from https://github.com/dotnet/maui/pull/26222

albyrock87 avatar Dec 06 '24 09:12 albyrock87

Azure Pipelines successfully started running 3 pipeline(s).

azure-pipelines[bot] avatar Dec 09 '24 15:12 azure-pipelines[bot]

/rebase

PureWeen avatar Dec 09 '24 20:12 PureWeen

/azp run

PureWeen avatar Dec 10 '24 17:12 PureWeen

Azure Pipelines successfully started running 3 pipeline(s).

azure-pipelines[bot] avatar Dec 10 '24 17:12 azure-pipelines[bot]

  • failing test on iOS/Catalyst is a known issue on main and not related to this PR

PureWeen avatar Dec 10 '24 22:12 PureWeen