-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
In measuring the size of a NativeAOT published ASP.NET app, a considerable amount of size is added when using Console logging.
dotnet new api -aot and dotnet publish on win-x64: 13.2 MB. Removing the line builder.Logging.AddConsole(): 12.1 MB.
From my investigation, I've determined that ~860KB of size comes from using the ConfigurationBinder, which also pulls in Sytem.ComponentModel.TypeConverter. One reason this is so big is because a bunch of methods on ICollection/IList can be trimmed if we don't include ConfigurationBinder/TypeConverter. There are a lot of generic instantiations of collections (Dictionary, List, etc), and all these interface methods across scores of collection types add up. The methods in the System.Collections.Generic namespace drop by 343 KB alone.
For a full picture of all the code that can be trimmed here, diff the following mstat dumps:
no-configbinder-console.txt was created using eerhardt@af48374.
configbinder-console.txt was created from main
To eliminate this code from the application, we should remove the ConfigurationBinder usages in Logging.Console. They come from 2 places:
runtime/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerExtensions.cs
Line 43 in 46cb4ed
| LoggerProviderOptions.RegisterProviderOptions<ConsoleLoggerOptions, ConsoleLoggerProvider>(builder.Services); |
runtime/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerExtensions.cs
Line 141 in 46cb4ed
| builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<TOptions>, ConsoleLoggerFormatterConfigureOptions<TFormatter, TOptions>>()); |
And instead we can hand-write the "configuration => options" deserialization code. This can also improve startup performance, since we won't be using Reflection here.
Another option would be to use #44493, but that isn't available yet. We can always hand-write the code for now, and use the source generator later once available.