.NET MAUI Efficiency—.NET Conf 2022

Study what Jonathan Peppers shared in his .NET Conf 2022 session on .NET MAUI efficiency updates with the discharge of .NET 7.

On November 8, 9 and 10 we had the .NET Conf 2022 occasion, through which the Microsoft group offered tremendous fascinating information and enhancements in net, cellular, cloud and lots of extra
features. This time we are going to summarize Jonathan Peppers’ presentation about Efficiency Enhancements in .NET MAUI (.NET 7 Version).

Screengrab of Jonathan Peppers beside his slide 'Performance Improvements in .NET MAUI

Targets for .NET 6 & 7

The Microsoft group had two primary points to resolve:

  • iOS began quick however the app was huge.
  • Whereas in Android, it was the other—the appliance was good however we had issues with the beginning.

That’s why the purpose was 800 milliseconds to show on a Pixel 5. They had been additionally aiming to get round 10 megabytes for the .NET MAUI template on iOS, and so they made some actually good strides towards these objectives in .NET 6.

Goals for .NET 6 & 7 slide, as noted below

However once they began engaged on .NET 7, they bought buyer suggestions to proceed fixing different issues like desktop startup, Android layouts and scrolling efficiency. The group centered on this suggestions, but additionally in .NET 7 they ended up making different enhancements
to make their apps smaller and in a position to begin quicker too.

Slide showing two app screenshots and two graphs. Bullet list says: A CollectionView with 10,000 items, each row contains two data-bound labels, it should scroll fast

This picture was already offered within the State of .NET MAUI speak by David Ortinau and Maddy Montaquila. Now let’s dig just a little deeper!

That is an app that was on Reddit just a few years in the past, the place labels with textual content studying “LOL” popped on the display in random colours and rotations. The app measures what number of instances it will possibly load one other label per second (LOLs per second).

Jonathan wrote his personal model of this app. He developed it in Java not in .NET, to later examine Xamarin and .NET MAUI.

When profiling this software, we had been in a position to see among the efficiency issues within the layouts in .NET MAUI, and the fixes made might be seen translated within the second picture. Right here the app is operating on a Pixel 4A and consists of:

  • A CollectionView with 10,000 gadgets.
  • Every row incorporates two data-bound labels.

That is one thing that it is best to have the ability to scroll via shortly. You possibly can see on the system within the first picture that as you scroll to the highest in .NET 6 you’ll be able to see the bar is up and in .NET 7 it’s decrease. It’s a bit difficult to get very actual numbers
right here, however the graph proven is an excellent instance to take a look at for efficiency of labor completed.

LOLs per second bar graph showing Xamarin Forms below 200, .NET 6 MAUI above 300, .NET 7 MAUI almost to 500, C# almost to 600, Java almost to 700.

These are LOLs per second. Within the earlier picture, it’s referenced how the app was made in Java, C#, .Web MAUI and Xamarin, right here we see the results of these exams.

  • Java is like the highest of .NET MAUI since you are utilizing the textual content view of the Android widget, which is the underlying management that may be a label in MAUI.
  • The C# model is identical, besides we now have the price of C# interop in Java (work will proceed to get higher and higher).
  • On the different excessive, we now have Xamarin Types and .NET MAUI with .NET 6. As you’ll be able to see from Xamarin Types to .NET 6, there may be appreciable progress.

🔗 If you wish to know extra data, you’ll be able to enter the LOLs per second documentation.

Desktop Startup Efficiency in .NET 7

Desktop Startup  in .NET MAUI slide showing the bullet points below and a line chart of startup trends that drops off signifcantly

The next factors are highlighted:

  • Home windows: Launch builds use ReadyToRun by default.
  • macOS/MacCatalyst: Launch builds goal each x64 and arm64 by default.
  • In a single case, the Apple Rosetta translation was taking 3-4 seconds for a .NET MAUI mission.

Every of those factors had been centered on making the expertise growing apps with .NET MAUI higher and higher. Some processes/steps that beforehand needed to be adjusted manually had been improved and are already built-in by default.

Profiling and Measuring Startup

Profiling in .NET MAUI Applications slide with bullet list: http://aka.ms/profile-maui; We're working on making this easier! Ideally in a future version of Visual Studio.; Example of dotnet-trace on Android: adb reverse tcp:9000 tcp:9001, adb shell setprop..., dotnet-dsrouter..., dotnet-trace...; https://speedscope.app

Jonathan included documentation known as Profiling .NET MAUI Apps in which you’ll be able to go deeper into the topic!

An instance of tips on how to do it on Android is to run a set of instructions that units the .NET Hint to the place it would lead your app to run on a distant as an Android system. Due to this fact, when you run these instructions, a pace scope file comes out and you’ll go to
your speedscope.app; it’s like a neighborhood net app that doesn’t add your file wherever however you’ll be able to have a look at your hint on that web site.

Measuring Startup Time in .NET MAUI slide with bullets: Always time Release builds!; https://aka.ms/profile-maui#measuring-startup-times; Android: ActivityTaskManager Displayed log message; iOS: Xcode & Instruments; Windows and macOS Desktop code.

For measuring Startup Time in .NET MAUI, you’ll be able to enter the next documentation. And you may also see in GitHub
the measure-startup documentation.

DateTimeOffset.Now and DateTime.Now

DateTimeOffset.Now and DateTime.Now slide bullets: Use UtcNow if you don't need local time; we found a significant amount of time spent in the very first DateTimeOffset.Now call: ~277ms; Event after recording a custom AOT profile, we still saw ~161ms; Instead: 1. Call an Android Java API for the current time offset. Our startup code in the Android workload is Java, so it can easily retrieve the value for use by the BCL. 2. Load timezone data on a background thread. This results in the original BCL behavior when complete, while using the offset from Java in the meantime.; This greatly improved DateTimeOffset.Now to a mere ~17.67ms.

Jonathan was speaking about an instance of a buyer who reported that they had been utilizing Datetime.Now of their app and you can see 277 milliseconds being added to the beginning simply by including this. Seems they may use UTCNow and that’s what they

Jonathan recommends doing this when you don’t actually need native time. However in the identical manner, DataTime.Now’s a quite common API that’s in all probability nonetheless getting used regardless of this, so the Microsoft group labored on bettering these instances and managed
to cut back them from 277 milliseconds to 17 or 18 milliseconds, which is a good enchancment.

Different highlighter factors:

  • Even after recording a customized AOT profile, they nonetheless noticed 161ms.
  • As a substitute:
    • Name an Android Java API for the present time offset. Our startup code within the Android workload is Java, so it will possibly simply retrieve the worth to be used by the BCL.
    • Load timezone knowledge on a background thread. This leads to the unique BCL habits when full, whereas utilizing the offset from Java within the meantime.

Some hyperlinks that Jonathan leaves us as references:

Keep away from ColorStateList (int[][], int[])

Avoid ColorStateList slide: dotnet-trace output of a .NET 6 'dotnet new maui' application shows time spent creating Andoird Color StateList objects; You can see details about this method, reviewing the C# binding for this Java API; Creates a Java array and copy each element; Solution - move more of this code into Java!

ColorStateList is an Android API that permits you to bypass a button. For instance, all of the completely different states of that button and what coloration they’re—so that is very helpful. This API passes a multidimensional array of integers. Within the instance proven,
Java copies all these parts with a view to entry the array. This took about 17 milliseconds.

The answer for us in .NET MAUI is that we are able to transfer the code to Java accurately. MAUI has an area for every platform, so we are able to put code in there after which we are able to create our personal inner APIs to do issues quicker by interoperating in Java.

ColorStateList Soluition slide has code blocks

That is an instance of what the answer would appear to be.

Wrapping Up

We’ve got reached the tip of this abstract. I highlighted a few of my favourite subjects however there are various extra!

If you wish to be taught extra details about this presentation, you’ll be able to watch the video Efficiency Enhancements in .NET MAUI (.NET 7 Version), and I additionally
suggest Jonathan Peppers’ article known as .NET 7 Efficiency Enhancements in .NET MAUI.

Nonetheless haven’t seen the content material that was provided on the State of .NET MAUI presentation? I like to recommend you learn the article Recap: State of .NET MAUI—.NET Conf 2022!
It speaks about all of the information at a basic stage of .NET MAUI.

Thanks for studying! 💚💕

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles