Description
Description
In profiling the .NET Podcasts application startup time on an Android Pixel 5a, I'm seeing its usage of a named HttpClient using builder.Services.AddHttpClient<ShowsService>(client => {})
as taking up 5% of the startup cost of the app.
Configuration
- Which version of .NET is the code running on?
6.0
- What OS version, and what distro if applicable?
Android 12
- What is the architecture (x64, x86, ARM, ARM64)?
ARM64
- If relevant, what are the specs of the machine?
Google Pixel 5a
Regression?
No
Data
Using the startup profiling script at https://github.com/jonathanpeppers/maui-profiling#faq, I'm getting the following results:
(using a full AOT of the app, so to eliminate any JIT time)
Startup time with AddHttpClient
Average(ms): 838.2
Average(ms): 832.4
Average(ms): 835.2
Startup time with just creating a shared HttpClient without DI
Average(ms): 795.5
Average(ms): 799.8
Average(ms): 797.9
Analysis
dotnet-podcasts.speedscope.zip
- Looking through the attached speedscope, a large chunk of time is spent creating the named Http client service in AddTransientHelper:
A big chunk of that time is due to calling ActivatorUtilities.CreateFactory, which uses System.Linq.Expressions and IL Ref.Emit.
- The Logging in Microsoft.Extensions.Http also seems to be adding a decent amount of overhead:
Discussion
I wonder if using Microsoft.Extensions.Http on a mobile application is recommended at all. Or if a mobile app should just be following the guidance we give in our docs:
HttpClient is intended to be instantiated once and re-used throughout the life of an application.
@davidfowl @jonathanpeppers @dotnet/ncl