Repository navigation
[release/11.0.1xx] [TrimmableTypeMap] Avoid peer lookup reflection - #13586
Conversation
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>
There was a problem hiding this comment.
🟢 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.IsSealedflag 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
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
✅ 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 { |
There was a problem hiding this comment.
🤖 💡 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}
| 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 |
There was a problem hiding this comment.
@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?
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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; } |
There was a problem hiding this comment.
What if we made this EditorBrowsable.Hidden? We'd also want to do that in main.
There was a problem hiding this comment.
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
jonathanpeppers
left a comment
There was a problem hiding this comment.
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.
Context: #13526, #13421.
The sealed-peer shortcut checks
Type.Assemblyon every lookup, which is expensive under NativeAOT. Backport the generator-selected, bool-backedJavaPeerProxy.IsSealedflag so eligible sealedMono.Androidpeers 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 existingintellisensemode.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
_AndroidRuntimeconfirmed the intended runtimes.DefaultItemExcludesInProjectFolderprevents the sibling configuration's intermediate Java files from being included in default globs.NativeAOT build/install — passed:
NativeAOT tests — 22 passed, 0 skipped:
CoreCLR build/install — passed:
CoreCLR tests — 22 passed, 0 skipped:
Pull Request
title and
description
should follow the
commit-messages.mdworkflow documentation, and in particular should include: