dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.27k stars 1.76k forks source link

Windows startup performance - time spent loading WindowsAppSdk `.xaml` #9179

Open jonathanpeppers opened 2 years ago

jonathanpeppers commented 2 years ago

Description

Profiling a customer's app, I see time spent in:

87.65ms (1.2%)  15.49ns (<0.01%)    microsoft.maui!Microsoft.Maui.Platform.ResourceDictionaryExtensions.AddLibraryResources(class Microsoft.UI.Xaml.Resou...

With the stack trace of:

image

https://github.com/dotnet/maui/blob/f1c358800c5276bc16290164eaf98c60f92da9fe/src/Core/src/Hosting/MauiAppBuilder.cs#L72

https://github.com/dotnet/maui/blob/f1c358800c5276bc16290164eaf98c60f92da9fe/src/Core/src/Platform/Windows/ResourceDictionaryExtensions.cs#L29-L32

Can we port parts of this .xaml to be fully C# instead?

https://github.com/dotnet/maui/blob/main/src/Core/src/Platform/Windows/Styles/Resources.xaml

I suspect this would improve startup times on Windows by some order of magnitude.

Public API Changes

None

Intended Use-Case

Save up to ~87ms of Windows startup time.

jonathanpeppers commented 2 years ago

I did a little research here, making a sample WinUI3 app without MAUI: App8.zip

The WinUI3 app does:

<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<ResourceDictionary Source="ClassLibrary1/ResourceDictionary1.xaml" />

If I change it to be like dotnet/maui:

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
    Resources.AddLibraryResources<XamlControlsResources>();
    Resources.AddLibraryResources("foo", "ms-appx:///ClassLibrary1/ResourceDictionary1.xbf");
    //...

Then I get additional time spent in: image

The startup time before/after:

Average(ms): 183.09134
Average(ms): 191.48126

We should investigate if dotnet/maui can do something like this in templates:

<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<ResourceDictionary Source="Microsoft.Maui/Platform/Windows/Styles/Resources.xaml" />