Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] feature switch defaults for fast startup (
Browse files Browse the repository at this point in the history
#8347)

Context: dotnet/runtime@fecf3ee
Context: dotnet/runtime@3aeefbd
Context: dotnet/runtime#89880

In dotnet/runtime two new feature switches were added in order to
improve startup performance on Android:

  * `System.Diagnostics.Metrics.Meter.IsSupported`: disables
    `System.Diagnostics.Metrics` support for `HttpClient`, etc.

  * `Microsoft.Extensions.DependencyInjection.DisableDynamicEngine`:
    after the second call to retrieve a service from a dependency
    injection container, `System.Reflection.Emit` is used to improve
    performance of repeated calls.

We also have the existing switch:

  * `Switch.System.Reflection.ForceInterpretedInvoke`: after the
    second call to `MethodInfo.Invoke()` or `ConstructorInfo.Invoke()`,
    `System.Reflection.Emit` is used to improve performance of
    repeated calls.

Introduce two new MSBuild properties:

  * `$(AndroidAvoidEmitForPerformance)` to toggle both
    `Microsoft.Extensions.DependencyInjection.DisableDynamicEngine` and
    `Switch.System.Reflection.ForceInterpretedInvoke`. This is a public
    property that defaults to `true`.

  * `$(_SystemDiagnosticsMetricsMeterIsSupported)` to toggle
    `System.Diagnostics.Metrics.Meter.IsSupported`.  This is a private
    property that defaults to `false`.

I added a test to check `System.Diagnostics.Metrics.Meter.IsSupported`
at runtime.  I did not do this with Microsoft.Extensions, as we'd have
to add a reference to the NuGet package and track its version for
nightly builds.

I also updated the AOT profile, and some
`System.Diagnostics.Metrics.Meter` API calls are now gone:

	bool System.Diagnostics.Metrics.Meter:AddInstrument (System.Diagnostics.Metrics.Instrument)
	object System.Diagnostics.Metrics.Instrument:get_SyncObject ()
	System.Diagnostics.Metrics.Counter`1<long> System.Diagnostics.Metrics.Meter:CreateCounter (string,string,string,System.Collections.Generic.IEnumerable`1<System.Collections.Generic.KeyValuePair`2<string, object>>)
	System.Diagnostics.Metrics.Counter`1<long> System.Diagnostics.Metrics.Meter:CreateCounter (string,string,string)
	void System.Diagnostics.Metrics.MeterListener:.cctor ()
	void System.Diagnostics.Metrics.MetricsEventSource:.cctor ()
	void System.Diagnostics.Metrics.MetricsEventSource:.ctor ()
  • Loading branch information
jonathanpeppers authored Sep 21, 2023
1 parent c2d0c7c commit eb6397d
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 93 deletions.
26 changes: 26 additions & 0 deletions Documentation/guides/building-apps/build-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,32 @@ The `$(AndroidApplicationJavaClass)` property is generally set by

Added in Xamarin.Android 6.1.

## AndroidAvoidEmitForPerformance

A boolean property that determines whether or not `System.Reflection.Emit` is
"avoided" to improve startup performance. This property is `True` by default.

Usage of `System.Reflection.Emit` has a noticeable impact on startup performance
on Android. This behavior is disabled by default for the following feature
switches:

* `Switch.System.Reflection.ForceInterpretedInvoke`: after the second call to
`MethodInfo.Invoke()` or `ConstructorInfo.Invoke()`, code is emitted to
improve performance of repeated calls.

* `Microsoft.Extensions.DependencyInjection.DisableDynamicEngine`: after the
second call to retrieve a service from a dependency injection container, code
is emitted to improve performance of repeated calls.

It is desirable in most Android applications to disable this behavior.

See the [Base Class Libraries Feature Switches documentation][feature-switches]
for details about available feature switches.

Added in .NET 8.

[feature-switches]: https://github.com/dotnet/runtime/blob/main/docs/workflow/trimming/feature-switches.md

## AndroidBinUtilsPath

A path to a directory containing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@
<NullabilityInfoContextSupport Condition="'$(NullabilityInfoContextSupport)' == ''">false</NullabilityInfoContextSupport>
<BuiltInComInteropSupport Condition="'$(BuiltInComInteropSupport)' == ''">false</BuiltInComInteropSupport>
<JsonSerializerIsReflectionEnabledByDefault Condition="'$(JsonSerializerIsReflectionEnabledByDefault)' == '' and '$(TrimMode)' == 'partial'">true</JsonSerializerIsReflectionEnabledByDefault>
<_SystemReflectionForceInterpretedInvoke Condition="'$(_SystemReflectionForceInterpretedInvoke)' == ''">true</_SystemReflectionForceInterpretedInvoke>
<_SystemDiagnosticsMetricsMeterIsSupported Condition="'$(_SystemDiagnosticsMetricsMeterIsSupported)' == ''">false</_SystemDiagnosticsMetricsMeterIsSupported>
<AndroidAvoidEmitForPerformance Condition="'$(AndroidAvoidEmitForPerformance)' == ''">$(AvoidEmitForPerformance)</AndroidAvoidEmitForPerformance>
<AndroidAvoidEmitForPerformance Condition="'$(AndroidAvoidEmitForPerformance)' == ''">true</AndroidAvoidEmitForPerformance>
<!-- Verify DI trimmability at development-time, but turn the validation off for production/trimmed builds. -->
<VerifyDependencyInjectionOpenGenericServiceTrimmability Condition="'$(VerifyDependencyInjectionOpenGenericServiceTrimmability)' == '' and '$(PublishTrimmed)' == 'true'">false</VerifyDependencyInjectionOpenGenericServiceTrimmability>
<VerifyDependencyInjectionOpenGenericServiceTrimmability Condition="'$(VerifyDependencyInjectionOpenGenericServiceTrimmability)' == ''">true</VerifyDependencyInjectionOpenGenericServiceTrimmability>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ See: https://github.com/dotnet/runtime/blob/b13715b6984889a709ba29ea8a1961db469f
Trim="true" />
<!-- https://github.com/dotnet/runtime/blob/211cdd011f19a51b7092d8365e11e774a8280afb/src/libraries/System.Private.CoreLib/src/System/LocalAppContextSwitches.cs#L52 -->
<RuntimeHostConfigurationOption Include="Switch.System.Reflection.ForceInterpretedInvoke"
Value="$(_SystemReflectionForceInterpretedInvoke)"
Value="$(AndroidAvoidEmitForPerformance)"
Trim="true"
/>
<!-- https://github.com/dotnet/runtime/commit/fecf3eeffd3650566555e15292f9df0d3abcdfc6 -->
<RuntimeHostConfigurationOption Include="Microsoft.Extensions.DependencyInjection.DisableDynamicEngine"
Value="$(AndroidAvoidEmitForPerformance)"
Trim="true"
/>
<!-- https://github.com/dotnet/runtime/commit/3aeefbdd5e975e287effaa8670422837dc661d68 -->
<RuntimeHostConfigurationOption Include="System.Diagnostics.Metrics.Meter.IsSupported"
Value="$(_SystemDiagnosticsMetricsMeterIsSupported)"
Trim="true"
/>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,136 +5,136 @@
"Size": 3572
},
"assemblies/_Microsoft.Android.Resource.Designer.dll": {
"Size": 2101
"Size": 2102
},
"assemblies/FormsViewGroup.dll": {
"Size": 7112
},
"assemblies/Java.Interop.dll": {
"Size": 70026
"Size": 70036
},
"assemblies/Mono.Android.dll": {
"Size": 469836
"Size": 471216
},
"assemblies/Mono.Android.Runtime.dll": {
"Size": 5509
"Size": 5506
},
"assemblies/mscorlib.dll": {
"Size": 3859
"Size": 3852
},
"assemblies/netstandard.dll": {
"Size": 5575
"Size": 5565
},
"assemblies/rc.bin": {
"Size": 1395
"Size": 1512
},
"assemblies/System.Collections.Concurrent.dll": {
"Size": 11508
"Size": 11502
},
"assemblies/System.Collections.dll": {
"Size": 15408
"Size": 15398
},
"assemblies/System.Collections.NonGeneric.dll": {
"Size": 7451
"Size": 7442
},
"assemblies/System.ComponentModel.dll": {
"Size": 1935
"Size": 1925
},
"assemblies/System.ComponentModel.Primitives.dll": {
"Size": 2549
"Size": 2538
},
"assemblies/System.ComponentModel.TypeConverter.dll": {
"Size": 6031
"Size": 6020
},
"assemblies/System.Console.dll": {
"Size": 6572
"Size": 6565
},
"assemblies/System.Core.dll": {
"Size": 1984
"Size": 1977
},
"assemblies/System.Diagnostics.DiagnosticSource.dll": {
"Size": 12520
"Size": 9057
},
"assemblies/System.Diagnostics.TraceSource.dll": {
"Size": 6548
"Size": 6540
},
"assemblies/System.dll": {
"Size": 2341
"Size": 2332
},
"assemblies/System.Drawing.dll": {
"Size": 1934
"Size": 1923
},
"assemblies/System.Drawing.Primitives.dll": {
"Size": 11971
"Size": 11967
},
"assemblies/System.IO.Compression.Brotli.dll": {
"Size": 11188
"Size": 11187
},
"assemblies/System.IO.Compression.dll": {
"Size": 15864
"Size": 15856
},
"assemblies/System.IO.IsolatedStorage.dll": {
"Size": 9869
"Size": 9862
},
"assemblies/System.Linq.dll": {
"Size": 19560
"Size": 19555
},
"assemblies/System.Linq.Expressions.dll": {
"Size": 164682
"Size": 165111
},
"assemblies/System.Net.Http.dll": {
"Size": 69544
"Size": 67637
},
"assemblies/System.Net.Primitives.dll": {
"Size": 22431
"Size": 22426
},
"assemblies/System.Net.Requests.dll": {
"Size": 3599
"Size": 3589
},
"assemblies/System.ObjectModel.dll": {
"Size": 8113
"Size": 8106
},
"assemblies/System.Private.CoreLib.dll": {
"Size": 845111
"Size": 845653
},
"assemblies/System.Private.DataContractSerialization.dll": {
"Size": 192906
"Size": 193645
},
"assemblies/System.Private.Uri.dll": {
"Size": 43477
"Size": 42849
},
"assemblies/System.Private.Xml.dll": {
"Size": 215983
"Size": 215960
},
"assemblies/System.Private.Xml.Linq.dll": {
"Size": 16636
"Size": 16631
},
"assemblies/System.Runtime.dll": {
"Size": 2748
"Size": 2740
},
"assemblies/System.Runtime.InteropServices.dll": {
"Size": 3858
"Size": 3851
},
"assemblies/System.Runtime.Serialization.dll": {
"Size": 1861
"Size": 1850
},
"assemblies/System.Runtime.Serialization.Formatters.dll": {
"Size": 2480
"Size": 2469
},
"assemblies/System.Runtime.Serialization.Primitives.dll": {
"Size": 3758
"Size": 3747
},
"assemblies/System.Security.Cryptography.dll": {
"Size": 8099
"Size": 8089
},
"assemblies/System.Text.RegularExpressions.dll": {
"Size": 158865
"Size": 158950
},
"assemblies/System.Xml.dll": {
"Size": 1752
"Size": 1741
},
"assemblies/System.Xml.Linq.dll": {
"Size": 1771
"Size": 1761
},
"assemblies/UnnamedProject.dll": {
"Size": 4987
Expand Down Expand Up @@ -206,7 +206,7 @@
"Size": 42282
},
"classes.dex": {
"Size": 3514720
"Size": 3513140
},
"lib/arm64-v8a/libmono-component-marshal-ilgen.so": {
"Size": 97080
Expand All @@ -215,19 +215,19 @@
"Size": 325320
},
"lib/arm64-v8a/libmonosgen-2.0.so": {
"Size": 3189360
"Size": 3193200
},
"lib/arm64-v8a/libSystem.IO.Compression.Native.so": {
"Size": 723560
},
"lib/arm64-v8a/libSystem.Native.so": {
"Size": 93920
"Size": 93952
},
"lib/arm64-v8a/libSystem.Security.Cryptography.Native.Android.so": {
"Size": 154904
},
"lib/arm64-v8a/libxamarin-app.so": {
"Size": 102752
"Size": 102840
},
"META-INF/android.support.design_material.version": {
"Size": 12
Expand Down Expand Up @@ -1916,5 +1916,5 @@
"Size": 325240
}
},
"PackageSize": 7977998
"PackageSize": 7973902
}
Binary file modified src/profiled-aot/dotnet.aotprofile
Binary file not shown.
Loading

0 comments on commit eb6397d

Please sign in to comment.