-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] use factory methods for registering services
In .NET 8 Preview 7, we noticed a regression in startup around: .NET 8 Preview 6: 45.35ms microsoft.maui!Microsoft.Maui.Hosting.MauiAppBuilder.Build() .NET 8 Preview 7: 62.17ms microsoft.maui!Microsoft.Maui.Hosting.MauiAppBuilder.Build() This may have been related to: dotnet/runtime#87183 Which should be improved by: dotnet/runtime#89964 In reviewing the traces, we noticed several places where MAUI was registering services like: services.AddTransient<IFoo, Foo>(); Where we could instead do: services.AddTransient<IFoo>(_ => new Foo()); Where this registration avoids any System.Reflection work at startup. Microsoft.Extensions.DI can just call the factory method instead. I expanded upon `BannedSymbols.txt` like we did in e7812b0, to ban cases of `AddTransient` and `AddScoped` that might be used accidentally. This resulted in banning the slow versions of these APIs like: Microsoft.Maui.Hosting.ImageSourceServiceCollectionExtensions.AddService Microsoft.Maui.Hosting.MauiHandlersCollectionExtensions.AddHandler Microsoft.Maui.Hosting.MauiHandlersCollectionExtensions.TryAddHandler I also introduced public, fast versions of methods in `MauiHandlersCollectionExtensions`. With this change in place, I could see the difference in: Before: 11.24ms microsoft.extensions.dependencyinjection!Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine..ctor After: 6.29ms microsoft.extensions.dependencyinjection!Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine..ctor Which resulted in an overall faster startup of a `dotnet new maui` app on a Pixel 5: Average(ms): 691 Std Err(ms): 3.00370142028502 Std Dev(ms): 9.49853789918334 Average(ms): 687.8 Std Err(ms): 4.04365071576553 Std Dev(ms): 12.7871463239892 This is an average of 10 runs. Note that this was built with main, and so we have an out-of-date AOT profile compared to the `net8.0` branch.
- Loading branch information
1 parent
ff02f57
commit 7406958
Showing
20 changed files
with
221 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,18 @@ | ||
M:Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAddSingleton`2(Microsoft.Extensions.DependencyInjection.IServiceCollection);Use a Factory method to create the service instead | ||
M:Android.Content.Res.ColorStateList.#ctor(System.Int32[][],System.Int32[]);Use Microsoft.Maui.PlatformInterop.Get*ColorStateList() Java methods instead | ||
M:Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAddScoped(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type,System.Type);Use a Factory method to register the service instead | ||
M:Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAddScoped`2(Microsoft.Extensions.DependencyInjection.IServiceCollection);Use a Factory method to register the service instead | ||
M:Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAddSingleton(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type,System.Type);Use a Factory method to register the service instead | ||
M:Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAddSingleton`2(Microsoft.Extensions.DependencyInjection.IServiceCollection);Use a Factory method to register the service instead | ||
M:Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAddTransient(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type,System.Type);Use a Factory method to register the service instead | ||
M:Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAddTransient`2(Microsoft.Extensions.DependencyInjection.IServiceCollection);Use a Factory method to register the service instead | ||
M:Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddScoped(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type,System.Type);Use a Factory method to register the service instead | ||
M:Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddScoped`2(Microsoft.Extensions.DependencyInjection.IServiceCollection);Use a Factory method to register the service instead | ||
M:Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type,System.Type);Use a Factory method to register the service instead | ||
M:Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton`2(Microsoft.Extensions.DependencyInjection.IServiceCollection);Use a Factory method to register the service instead | ||
M:Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type,System.Type);Use a Factory method to register the service instead | ||
M:Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient`2(Microsoft.Extensions.DependencyInjection.IServiceCollection);Use a Factory method to register the service instead | ||
M:Microsoft.Maui.Hosting.ImageSourceServiceCollectionExtensions.AddService`2(Microsoft.Maui.Hosting.IImageSourceServiceCollection);Use a Factory method to register the service instead | ||
M:Microsoft.Maui.Hosting.MauiHandlersCollectionExtensions.AddHandler(Microsoft.Maui.Hosting.IMauiHandlersCollection,System.Type,System.Type);Use a Factory method to register the service instead | ||
M:Microsoft.Maui.Hosting.MauiHandlersCollectionExtensions.AddHandler`2(Microsoft.Maui.Hosting.IMauiHandlersCollection);Use a Factory method to register the service instead | ||
M:Microsoft.Maui.Hosting.MauiHandlersCollectionExtensions.TryAddHandler(Microsoft.Maui.Hosting.IMauiHandlersCollection,System.Type,System.Type);Use a Factory method to register the service instead | ||
M:Microsoft.Maui.Hosting.MauiHandlersCollectionExtensions.TryAddHandler`2(Microsoft.Maui.Hosting.IMauiHandlersCollection);Use a Factory method to register the service instead |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.