Skip to content

[TrimmableTypeMap] NativeAOT: TryResolveProxyFromSealedTargetType calls Type.Assembly on every peer lookup (≈98% of its cost) #13421

Closed

Description

@Redth

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 simpleperf profile (cpu-clock 4 kHz, frame-pointer call graphs, symbolized against the unstripped libPoolMath.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-Type decision, e.g. a small Type → bool cache, or record "type lives in Mono.Android" on the generated JavaPeerProxy so no reflection is needed.
  • Or check IsSealed / GetProxyForManagedType first 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

  1. Publish any app with PublishAot=true that creates many peers on the UI thread (for example a list whose items handle WindowInsetsCompat).
  2. Add <profileable android:shell="true" /> and profile with simpleperf record --app <pkg> --call-graph fp.
  3. simpleperf report --children against the unstripped lib<App>.dbg.so shows get_Assembly under TryResolveProxyFromSealedTargetType.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions