Skip to content

Commit 399f37c

Browse files
github-actions[bot]jonathanpepperseerhardt
authored
[release/8.0] [DependencyInjection] introduce feature switch to disable S.R.E (#91352)
* [DependencyInjection] introduce feature switch to disable S.R.E When recording a new AOT profile for .NET MAUI apps running on Android, we noticed that System.Reflection.Emit work was being done on a background thread. The call seen in `dotnet-trace` output: 11.32ms microsoft.extensions.dependencyinjection!Microsoft.Extensions.DependencyInjection.ServiceLookup.ILEmitResolverBuilder.GenerateMethodBody(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite,System.Reflection.Emit.ILGenerator) .NET Android apps are unique in that there is a JIT, `RuntimeFeature.IsDynamicCodeCompiled` is true, System.Reflection.Emit is possible -- S.R.E is however, not great for startup performance. Starting threads on Android during startup can also be slow, as Android will commonly put all but a single core to sleep for battery saving purposes. We try to avoid starting threads on startup for "hello world" applications on Android. To solve this for now, introduce a new feature flag: Microsoft.Extensions.DependencyInjection.DisableDynamicEngine Which, we will provide a value in either the Android or .NET MAUI optional workload via an MSBuild property. To test, I put this in my app's `.csproj` file: <RuntimeHostConfigurationOption Include="Microsoft.Extensions.DependencyInjection.DisableDynamicEngine" Condition="'$(DisableDynamicEngine)' != ''" Value="$(DisableDynamicEngine)" Trim="true" /> Customers *could* opt to change this flag, but we don't think it will particularly useful. An example of services realized by .NET MAUI at startup, via some logging added: 08-25 13:21:55.647 16530 16530 I DOTNET : RealizeService called: System.Collections.Generic.IEnumerable`1[Microsoft.Maui.Hosting.IMauiInitializeService] 08-25 13:21:55.664 16530 16530 I DOTNET : RealizeService called: System.Collections.Generic.IEnumerable`1[Microsoft.Maui.Hosting.IMauiInitializeScopedService] 08-25 13:21:55.665 16530 16530 I DOTNET : RealizeService called: Microsoft.Maui.Dispatching.IDispatcher 08-25 13:21:55.668 16530 16530 I DOTNET : RealizeService called: System.Collections.Generic.IEnumerable`1[Microsoft.Maui.LifecycleEvents.LifecycleEventRegistration] 08-25 13:21:56.057 16530 16530 I DOTNET : RealizeService called: System.Collections.Generic.IEnumerable`1[Microsoft.Maui.Hosting.HandlerMauiAppBuilderExtensions+HandlerRegistration] 08-25 13:21:56.115 16530 16530 I DOTNET : RealizeService called: Microsoft.Extensions.DependencyInjection.IServiceScopeFactory 08-25 13:21:56.670 16530 16530 I DOTNET : RealizeService called: Microsoft.Maui.Controls.HideSoftInputOnTappedChangedManager 08-25 13:21:56.712 16530 16530 I DOTNET : RealizeService called: System.Collections.Generic.IEnumerable`1[Microsoft.Maui.Hosting.ImageSourcesMauiAppBuilderExtensions+ImageSourceRegistration] 08-25 13:21:57.700 16530 16530 I DOTNET : RealizeService using S.R.E: Microsoft.Maui.Controls.HideSoftInputOnTappedChangedManager `HideSoftInputOnTappedChangedManager` would be realized once per page, which would not be a huge payoff to use S.R.E for. So the only way the S.R.E codepath could be useful on Android would be if the customer is registering lots of services themselves. They might be better off just using `new()` in that case? An example of the startup time Android reports with the new flag on/off: DisableDynamicEngine=false 08-25 14:31:37.462 2090 2330 I ActivityTaskManager: Displayed com.companyname.testmaui/crc643c09abdeec717b83.MainActivity: +733ms 08-25 14:31:39.394 2090 2330 I ActivityTaskManager: Displayed com.companyname.testmaui/crc643c09abdeec717b83.MainActivity: +737ms 08-25 14:31:41.326 2090 2330 I ActivityTaskManager: Displayed com.companyname.testmaui/crc643c09abdeec717b83.MainActivity: +730ms DisableDynamicEngine=true 08-25 14:32:20.233 2090 2330 I ActivityTaskManager: Displayed com.companyname.testmaui/crc643c09abdeec717b83.MainActivity: +724ms 08-25 14:32:22.137 2090 2330 I ActivityTaskManager: Displayed com.companyname.testmaui/crc643c09abdeec717b83.MainActivity: +727ms 08-25 14:32:24.042 2090 2330 I ActivityTaskManager: Displayed com.companyname.testmaui/crc643c09abdeec717b83.MainActivity: +716ms This was a `dotnet new maui` project, using dotnet/maui/main on a Pixel 5 device. * Update docs/workflow/trimming/feature-switches.md Co-authored-by: Eric Erhardt <[email protected]> --------- Co-authored-by: Jonathan Peppers <[email protected]> Co-authored-by: Jonathan Peppers <[email protected]> Co-authored-by: Eric Erhardt <[email protected]>
1 parent 9c422ed commit 399f37c

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

docs/workflow/trimming/feature-switches.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ configurations but their defaults might vary as any SDK can set the defaults dif
2727
| MetadataUpdaterSupport | System.Reflection.Metadata.MetadataUpdater.IsSupported | Metadata update related code to be trimmed when set to false |
2828
| _EnableConsumingManagedCodeFromNativeHosting | System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting | Getting a managed function from native hosting is disabled when set to false and related functionality can be trimmed. |
2929
| VerifyDependencyInjectionOpenGenericServiceTrimmability | Microsoft.Extensions.DependencyInjection.VerifyOpenGenericServiceTrimmability | When set to true, DependencyInjection will verify trimming annotations applied to open generic services are correct |
30+
| DisableDependencyInjectionDynamicEngine | Microsoft.Extensions.DependencyInjection.DisableDynamicEngine | When set to true, DependencyInjection will avoid using System.Reflection.Emit when realizing services |
3031
| NullabilityInfoContextSupport | System.Reflection.NullabilityInfoContext.IsSupported | Nullable attributes can be trimmed when set to false |
3132
| DynamicCodeSupport | System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported | Changes RuntimeFeature.IsDynamicCodeSupported to false to allow testing AOT-safe fallback code without publishing for Native AOT. |
3233
| _AggressiveAttributeTrimming | System.AggressiveAttributeTrimming | When set to true, aggressively trims attributes to allow for the most size savings possible, even if it could result in runtime behavior changes |

src/libraries/Microsoft.Extensions.DependencyInjection/src/ILLink/ILLink.Substitutions.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
<type fullname="Microsoft.Extensions.DependencyInjection.ServiceProvider">
44
<method signature="System.Boolean get_VerifyOpenGenericServiceTrimmability()" body="stub" value="true" feature="Microsoft.Extensions.DependencyInjection.VerifyOpenGenericServiceTrimmability" featurevalue="true" />
55
</type>
6+
<type fullname="Microsoft.Extensions.DependencyInjection.ServiceProvider">
7+
<method signature="System.Boolean get_DisableDynamicEngine()" body="stub" value="true" feature="Microsoft.Extensions.DependencyInjection.DisableDynamicEngine" featurevalue="true" />
8+
</type>
69
</assembly>
710
</linker>

src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceProvider.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public sealed class ServiceProvider : IServiceProvider, IKeyedServiceProvider, I
3737
internal static bool VerifyOpenGenericServiceTrimmability { get; } =
3838
AppContext.TryGetSwitch("Microsoft.Extensions.DependencyInjection.VerifyOpenGenericServiceTrimmability", out bool verifyOpenGenerics) ? verifyOpenGenerics : false;
3939

40+
internal static bool DisableDynamicEngine { get; } =
41+
AppContext.TryGetSwitch("Microsoft.Extensions.DependencyInjection.DisableDynamicEngine", out bool disableDynamicEngine) ? disableDynamicEngine : false;
42+
4043
internal static bool VerifyAotCompatibility =>
4144
#if NETFRAMEWORK || NETSTANDARD2_0
4245
false;
@@ -246,7 +249,7 @@ private ServiceProviderEngine GetEngine()
246249
#if NETFRAMEWORK || NETSTANDARD2_0
247250
engine = CreateDynamicEngine();
248251
#else
249-
if (RuntimeFeature.IsDynamicCodeCompiled)
252+
if (RuntimeFeature.IsDynamicCodeCompiled && !DisableDynamicEngine)
250253
{
251254
engine = CreateDynamicEngine();
252255
}

0 commit comments

Comments
 (0)