Join our Discord server for community support and discussions Icon description

We’re pleased to announce the support for .NET Profile Guided AOT (PG-AOT) for the Uno Platform, which allows the creation of faster and smaller apps. When compared with the Full AOT support we’ve added support for last year, the package size is cut in half. It provides better performance with the Mixed Mode support we announced support for earlier by making a smarter use of the IL Interpreter.

We’ve updated the Uno Calculator, the Uno Playground, the XAML Controls Gallery, the RayTracer benchmark and the Roslyn Quoter applications using PG-AOT. These size improvements allow for these apps to run on Safari for iOS.

About Uno Platform

For those new to Uno Platform – it enables for creation of single-source C# and XAML apps which run natively on iOS and Android, macOS and Web via WebAssembly. Uno Platform is Open Source (Apache 2.0) and available on GitHub. To learn more about Uno Platform, see how it works, or create a small sample app.

What is .NET AOT ?

The .NET runtime supports three modes of execution:

Up until recently, the Mixed Mode was having the ability to choose which part of the code to compile to WebAssembly at the assembly level. This meant that for application that used a small portion of an assembly and wanted to have good performance, the whole assembly had to be AOT compiled as well.

This could cause lots of trouble to run on Safari for iOS, as there’s a hard limit on the size of WebAssembly modules that can be loaded.

Introducing Profile Guided AOT (PG-AOT)

This compilation and runtime mode allow for the AOT engine to selectively optimize methods to WebAssembly by recording which methods are used, and keep the rest as interpreted. This gives a very good balance when choosing between performance and payload size. It also has the advantage of reducing the build time, as less code needs to be compiled down to WebAssembly.

This feature is used in two passes:

In the example above, using AOT for the about menu may not be particularly interesting as it is not executed often.

This mode gives very good results, where the RayTracer sample goes from an uncompressed size of 5.5MB down to 2.9MB.

For the Uno Playground, the results are quite interesting as well:The startup time for the playground is also divided by two when using PG-AOT when compared with the interpreter-based version.

Another aspect of using this mode when compared to FullAOT is that there’s less code to compile and load in the browser, resulting in faster load time. This is particularly visible in the Uno Calculator which was updated to use PG-AOT.

The choice between execution modes can be viewed as follows:

Where each mode has its strengths and weaknesses.

How does PG-AOT compare to Xamarin.Android’s Custom Startup Tracing?

Xamarin has been providing the Custom Startup Tracing feature in recent builds, and this allows for significantly smaller applications.

While both features share the same profiled execution technique, the biggest difference with .NET’s PG-AOT is the fact that Android can executed JIT (Just in Time) compiled code. JIT code can run at decent native speed, which is why tracing only the startup of an application makes sense on Android. In the case of the Uno Calculator, the size of the application is also cut in half and the startup time is a bit faster.

With WebAssembly, where JITing code is not supported, profiling more than the startup of the application is a good choice to get better performance throughout the application, keeping lots of less used or unused code running through the interpreter. Having UI Tests automatically run through the application and generate a PG-AOT profile when the application is about to be released can be a good strategy to get an up-to-date profile.

How to use Profile Guided AOT in Uno Platform Applications

An up-to-date content of this section can be found in the Uno.Wasm.Boostrap documentation.

Note that using PG-AOT requires the use of WSL on Windows.

<WasmShellGenerateAOTProfile>true</WasmShellGenerateAOTProfile>
<ItemGroup>
         <WasmShellEnableAotProfile Include="aot.profile" />
</ItemGroup>
<WasmShellMonoRuntimeExecutionMode>InterpreterAndAOT</WasmShellMonoRuntimeExecutionMode>

Note that the AOT profile is a snapshot of the current set of assemblies and methods in your application. If that set changes significantly, you’ll need to re-create the AOT profile to get optimal results.

Next Steps

We hope you will take advantage of these new benefits to your Uno Apps. Or, if you are new to Uno Platform please give it a try via our official Getting Started tutorial.

 

Jerome Laban, on behalf of Uno Platform team

Uno Platform 5.2 LIVE Webinar – Today at 3 PM EST – Watch