Repository navigation
Description
Description
Under NativeAOT, TrimmableTypeMap.TryResolveProxyFromSealedTargetType spends almost all of its time in Type.Assembly, which it evaluates on every peer lookup:
// src/Mono.Android/Microsoft.Android.Runtime/TrimmableTypeMap.cs
if (targetType is null ||
!targetType.IsSealed ||
targetType.Assembly != typeof (Java.Lang.Object).Assembly) {
return false;
}On CoreCLR Type.Assembly is cheap. On NativeAOT it goes through the reflection metadata path: NativeFormatRuntimeNamedTypeInfo.get_Assembly → MetadataReaderExtensions.ToRuntimeAssemblyName. That builds a RuntimeAssemblyName and resolves the assembly on each call. Because this sits on the Java.Lang.Object.GetObject → GetPeer → CreatePeer → GetProxyForJavaObject path, every new peer pays for it. That includes sealed target types outside Mono.Android, which pay the cost and then fall through to the hierarchy lookup anyway.
Part of #10795.
Measurements
- App and build: .NET MAUI app (PoolMath), Release,
PublishAot=true,TrimMode=full, Microsoft.Android.Sdk 37.2.0-rc.2.84 (.NET 11 RC2). - Device and workload: Pixel 8 Pro on Android 16. A
simpleperfprofile (cpu-clock 4 kHz, frame-pointer call graphs, symbolized against the unstrippedlibPoolMath.dbg.so) of the UI thread during 20 list-scroll flings.
| frame (inclusive, % of UI-thread samples) | |
|---|---|
Java.Lang.Object.GetObject |
28.9% |
JniValueManager.GetPeer |
25.7% |
TrimmableTypeMapValueManager.CreatePeer |
13.1% |
TrimmableTypeMap.GetProxyForJavaObject |
5.6% |
TrimmableTypeMap.TryResolveProxyFromSealedTargetType |
3.8% |
↳ NativeFormatRuntimeNamedTypeInfo.get_Assembly |
3.7% |
↳ MetadataReaderExtensions.ToRuntimeAssemblyName |
3.5% |
↳ JNIEnv.FindClass + IsInstanceOf |
<0.5% |
So about 98% of TryResolveProxyFromSealedTargetType is the Type.Assembly comparison, not the JNI work it's there to avoid.
The app was creating far too many peers. MAUI's safe-area code re-dispatched window insets on every list cell layout, which we're fixing on our side. But the per-peer overhead applies to any peer-heavy code path: insets, adapters, listeners and the like.
Possible fixes
- Cache the per-
Typedecision, e.g. a smallType→boolcache, or record "type lives in Mono.Android" on the generatedJavaPeerProxyso no reflection is needed. - Or check
IsSealed/GetProxyForManagedTypefirst and only compare assemblies when a proxy exists, so the reflection call is at least skipped for non-sealed or unknown types.
Steps to reproduce
- Publish any app with
PublishAot=truethat creates many peers on the UI thread (for example a list whose items handleWindowInsetsCompat). - Add
<profileable android:shell="true" />and profile withsimpleperf record --app <pkg> --call-graph fp. simpleperf report --childrenagainst the unstrippedlib<App>.dbg.soshowsget_AssemblyunderTryResolveProxyFromSealedTargetType.
Activity
[TrimmableTypeMap] Avoid assembly reflection during peer lookup (#13586)