Skip to content

[release/11.0.1xx] [TrimmableTypeMap] Avoid peer lookup reflection - #13586

Merged
simonrozsival merged 1 commit into
release/11.0.1xxfrom
simonrozsival-backport-13526-release-11
Nov 18, 2026
Merged

simonrozsival merged 1 commit into
release/11.0.1xxfrom
simonrozsival-backport-13526-release-11

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

Context: #13526, #13421.

The sealed-peer shortcut checks Type.Assembly on every lookup, which is expensive under NativeAOT. Backport the generator-selected, bool-backed JavaPeerProxy.IsSealed flag so eligible sealed Mono.Android peers can be resolved without assembly reflection. External peers still use the object's Java class hierarchy, preserving the class-loader safeguard.

Include the eligibility flag in both incremental and content fingerprints, retain the two-argument proxy constructor, and adapt the upstream generator and device regressions to the release branch's test suites. See #13526 for upstream performance measurements; no separate release-branch performance benchmark was run.

Validation

  • PATH="$PWD/bin/Release/dotnet:$PATH" DOTNET_ROOT="$PWD/bin/Release/dotnet" make prepare CONFIGURATION=Release MSBUILD_ARGS='-m:4 -nr:false -p:UseSharedCompilation=false' — passed.
  • PATH="$PWD/bin/Release/dotnet:$PATH" DOTNET_ROOT="$PWD/bin/Release/dotnet" IncludeAndroidJavadoc=True AndroidJavadocVerbosity=intellisense make leeroy CONFIGURATION=Release MSBUILD_ARGS='-m:4 -nr:false -p:UseSharedCompilation=false' — passed, including extra API levels and workload packaging. The initial build with full Javadoc verbosity failed on unrelated missing copyright metadata; the retry used the existing intellisense mode.
  • bin/Release/dotnet/dotnet test tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests.csproj --no-restore -c Release -v minimal — 1,080 passed.
  • ./dotnet-local.sh test bin/TestRelease/net11.0/Microsoft.Android.Sdk.TrimmableTypeMap.IntegrationTests.dll -v minimal — 41 passed.
  • ./dotnet-local.sh test bin/TestRelease/net11.0/Microsoft.Android.Build.Tasks.Tests.dll --filter 'FullyQualifiedName~GenerateTrimmableTypeMapTests' -v minimal — 16 passed.
Android 35 ARM64 emulator: NativeAOT and CoreCLR device commands

Each command was run from the repository root. Both builds and installs passed; each device run passed 22 tests with no skips, including all seven relevant regressions. The APKs and evaluated _AndroidRuntime confirmed the intended runtimes. DefaultItemExcludesInProjectFolder prevents the sibling configuration's intermediate Java files from being included in default globs.

NativeAOT build/install — passed:

ANDROID_SERIAL=emulator-5554 DOTNET_ROOT="$PWD/bin/Release/dotnet" ./dotnet-local.sh build -t:Install -c Release tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj -p:RuntimeIdentifier=android-arm64 -p:AndroidTypeMapImplementation=trimmable -p:PublishAot=true -p:UseMonoRuntime=false -p:Optimize=true -p:Device=emulator-5554 -p:IncludeCategories=NativeAOTTrimmable -p:TestsFlavor=BackportNativeAOT -p:BaseIntermediateOutputPath=obj/BackportNativeAOT/ -p:OutputPath=bin/BackportNativeAOT/ '-p:DefaultItemExcludesInProjectFolder=obj/**' -p:AndroidPackageFormat=apk -m:4 -nr:false -p:UseSharedCompilation=false -bl:/Users/simon/.copilot/session-state/1f393a83-0122-407a-9d2d-9060d09c285c/files/device-nativeaot-retry.binlog

NativeAOT tests — 22 passed, 0 skipped:

root="$PWD"; cd tests/Mono.Android-Tests/Mono.Android-Tests && ANDROID_SERIAL=emulator-5554 DOTNET_ROOT="$root/bin/Release/dotnet" ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Release --report-trx --results-directory /Users/simon/.copilot/session-state/1f393a83-0122-407a-9d2d-9060d09c285c/files/test-results/nativeaot -p:RuntimeIdentifier=android-arm64 -p:AndroidTypeMapImplementation=trimmable -p:PublishAot=true -p:UseMonoRuntime=false -p:Optimize=true -p:Device=emulator-5554 -p:IncludeCategories=NativeAOTTrimmable -p:TestsFlavor=BackportNativeAOT -p:BaseIntermediateOutputPath=obj/BackportNativeAOT/ -p:OutputPath=bin/BackportNativeAOT/ '-p:DefaultItemExcludesInProjectFolder=obj/**' -p:AndroidPackageFormat=apk

CoreCLR build/install — passed:

DOTNET_ROOT="$PWD/bin/Release/dotnet" ./dotnet-local.sh build -t:Install -c Release tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj -p:RuntimeIdentifier=android-arm64 -p:AndroidTypeMapImplementation=trimmable -p:PublishAot=false -p:UseMonoRuntime=false -p:Optimize=true -p:Device=emulator-5554 -p:IncludeCategories=NativeAOTTrimmable -p:TestsFlavor=BackportCoreCLR -p:BaseIntermediateOutputPath=obj/BackportCoreCLR/ -p:OutputPath=bin/BackportCoreCLR/ '-p:DefaultItemExcludesInProjectFolder=obj/**' -p:AndroidPackageFormat=apk -m:4 -nr:false -p:UseSharedCompilation=false -bl:/Users/simon/.copilot/session-state/1f393a83-0122-407a-9d2d-9060d09c285c/files/device-coreclr-retry.binlog

CoreCLR tests — 22 passed, 0 skipped:

root="$PWD"; cd tests/Mono.Android-Tests/Mono.Android-Tests && ANDROID_SERIAL=emulator-5554 DOTNET_ROOT="$root/bin/Release/dotnet" ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Release --report-trx --results-directory /Users/simon/.copilot/session-state/1f393a83-0122-407a-9d2d-9060d09c285c/files/test-results/coreclr -p:RuntimeIdentifier=android-arm64 -p:AndroidTypeMapImplementation=trimmable -p:PublishAot=false -p:UseMonoRuntime=false -p:Optimize=true -p:Device=emulator-5554 -p:IncludeCategories=NativeAOTTrimmable -p:TestsFlavor=BackportCoreCLR -p:BaseIntermediateOutputPath=obj/BackportCoreCLR/ -p:OutputPath=bin/BackportCoreCLR/ '-p:DefaultItemExcludesInProjectFolder=obj/**' -p:AndroidPackageFormat=apk

Pull Request
title and
description
should follow the
commit-messages.md workflow documentation, and in particular should include:

  • Useful description of why the change is necessary.
  • Links to issues fixed
  • Unit tests

Context: #13526, #13421.

Checking Type.Assembly on every sealed-peer lookup is costly under NativeAOT. Mark generated proxies for sealed Mono.Android types using a constructor-backed bool instead. Continue to resolve external peers through the actual Java class hierarchy so differing class loaders remain safe.

Include eligibility in the incremental and content fingerprints, and cover generated proxies, cache behavior, sealed and nonsealed targets, and class-loader isolation.

(cherry picked from commit 0cb62f7)

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 16, 2026 21:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the backport addressing NativeAOT peer-lookup overhead.

No open issues 🎉

What changed in this PR

Backports #13526 to release/11.0.1xx, addressing the NativeAOT peer-lookup overhead described in #13421. The trimmable type map uses generator-selected eligibility instead of runtime assembly reflection, while preserving class-loader safeguards for external peers.

Changes:

  • Adds a bool-backed JavaPeerProxy.IsSealed flag while retaining the two-argument constructor.
  • Carries eligibility through scanning, generation, and both incremental and content fingerprints.
  • Adds generator and device regressions and documents the lookup behavior.
File Description
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​TrimmableTypeMapTypeManagerTests.cs Covers constructor compatibility, lookup paths, caching, and separate class loaders.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.Tests/​Scanner/​JavaPeerScannerTests.cs Verifies sealed-type detection.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.Tests/​Generator/​TypeMapAssemblyGeneratorTests.cs Checks eligibility arguments in generated proxy constructors.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.Tests/​Generator/​TrimmableTypeMapGeneratorTests.cs Checks regeneration and module identity changes when sealedness changes.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.Tests/​Generator/​FingerprintWriterTests.cs Updates reference serializers and exercises eligibility fingerprinting.
src/​Mono.Android/​PublicAPI/​API-37/​PublicAPI.Unshipped.txt Records the new property and constructor overload.
src/​Mono.Android/​Microsoft.Android.Runtime/​TrimmableTypeMap.cs Replaces the assembly check with proxy eligibility.
src/​Mono.Android/​Java.Interop/​JavaPeerProxy.cs Adds the eligibility flag and preserves the existing constructor.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​Scanner/​JavaPeerScanner.cs Reads sealedness from type metadata.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​Scanner/​JavaPeerInfo.cs Stores scanned sealedness.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​Generator/​TypeMapAssemblyEmitter.cs Emits the eligibility constructor argument.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​Generator/​ModelBuilder.cs Restricts eligibility to sealed Mono.Android targets.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​Generator/​Model/​TypeMapAssemblyData.cs Adds eligibility to the proxy model.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​Generator/​MetadataHelper.cs Includes eligibility in both fingerprints.
Documentation/​guides/​internals/​JavaJNI_Interop.md Explains the shortcut and external-peer class-loader safeguard.

Review effort: Balanced

@simonrozsival

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13586

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM — no blocking correctness findings. 0 errors, 0 warnings, 1 non-blocking inline suggestion.

The backport carries eligibility from scanner metadata through the generated constructor and runtime guard, includes it in both fingerprints, and retains the two-argument constructor. The external class-loader safeguard remains intact, with useful coverage for incompatible sealed targets, nonsealed hierarchy lookup, closed generics, and incremental invalidation. The performance rationale is supported by the upstream measurements in #13526; this review did not independently benchmark the release branch.

CI: All 45 GitHub check runs report success, including the aggregate dotnet-android check for Azure build 1684256. Direct Azure build/timeline inspection was blocked because the Azure CLI requires authentication, so detailed Azure results could not be independently inspected. This was a source review; no local builds or device tests were run.

Generated by Android PR Reviewer for #13586 · copilot · gpt61 · 84.7 AIC · ⌖ 4.72 AIC · ⊞ 21K
Comment /review to run again

};
var peers = new List<JavaPeerInfo> { peer };
if (alias) {
peers.Add (MakePeerWithActivation ("example/Peer", "Example.OtherPeer", assemblyName) with {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 💡 Testing — Consider adding a mixed-assembly alias case: a sealed Mono.Android peer and a sealed external peer sharing a JNI name, passed through MergeCrossAssemblyAliases, then assert that their emitted constructor arguments are true and false, respectively. The current alias cases keep both peers in the same input assembly. The mixed case would pin the important distinction between a peer's defining assembly and the typemap assembly that owns its alias group, protecting the class-loader safeguard against future refactoring.

{Rule: Test edge cases; verify interop boundary behavior}

@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Nov 17, 2026
Comment on lines +2355 to +2357
Java.Interop.JavaPeerProxy.IsSealed.get -> bool
Java.Interop.JavaPeerProxy.JavaPeerProxy(string! jniName, System.Type! targetType) -> void
Java.Interop.JavaPeerProxy.JavaPeerProxy(string! jniName, System.Type! targetType, bool isSealed) -> void

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simonrozsival is it a problem to add new APIs in a service release?

What would happen is someone with the new workload could create a NuGet package that wouldn't work for folks with the old one?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typemap code, which uses this, is always generated at app build time. No libraries (incl. Mono.Android) use this ctor. Also, this currently only affects Native AOT apps by default. I will think this through tomorrow and I'll try to think about the risks.

At the same time, this is just performance improvement that we do not need to backport and we can keep this .NET 12 only.

@jonathanpeppers jonathanpeppers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's think about the potential API break.

/// Generated proxies pass <see langword="true"/> only for sealed Mono.Android targets. External
/// targets pass <see langword="false"/> even when sealed, because they can use a different class loader.
/// </remarks>
public bool IsSealed { get; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we made this EditorBrowsable.Hidden? We'd also want to do that in main.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how that would be very helpful. This property is in no way different from the other properties. If anything, we should add it to the whole JavaPeerProxy class

@simonrozsival
simonrozsival enabled auto-merge (squash) November 18, 2026 15:26

@jonathanpeppers jonathanpeppers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll do a separate PR in main to:

  • Add the EditorBrowsable attribute
  • Summary comment that says "do not use directly"

Then we'll backport this.

@simonrozsival
simonrozsival merged commit d644ae6 into release/11.0.1xx Nov 18, 2026
45 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-backport-13526-release-11 branch November 18, 2026 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants