Repository navigation
Optimize trimmable typemap generation - #12598
Conversation
Reduce scanner and emitter allocations by caching metadata, avoiding unused MCW method scans, and eliminating intermediate buffers. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
Review tier: Lite
Findings: 1
New issues introduced by this change (1)
| Severity | Finding |
|---|---|
src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/PEAssemblyBuilder.cs — WritePE adjusts MemoryStream.Capacity, but this can throw NotSupportedException for… |
What changed in this PR
This PR optimizes the trimmable typemap pipeline by adding an opt-in “fast scan” mode for the MSBuild task path (skipping unnecessary marshal-method scanning for non-ACW types), and by reducing allocations across metadata scanning and typemap emission while preserving the default full-detail behavior for public APIs.
Changes:
- Add
collectMarshalMethodsForNonAcwoption (defaulttrue) to preserve full-detail behavior, and disable it from the MSBuild task for faster builds. - Introduce caching and lower-allocation metadata parsing in the scanner (
AssemblyIndexname/type ref caches, fasterRegisterAttributeparsing, less string decoding). - Reduce generator/emitter allocations (iterator-based enumeration, fewer temporary collections, fewer intermediate buffers).
| File | Description |
|---|---|
| tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Generator/TrimmableTypeMapGeneratorTests.cs | Adds coverage ensuring skipping non-ACW marshal-method collection doesn’t change outputs. |
| src/Xamarin.Android.Build.Tasks/Tasks/GenerateTrimmableTypeMap.cs | Uses optimized scan mode in the MSBuild task (collectMarshalMethodsForNonAcw: false). |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/TrimmableTypeMapGenerator.cs | Extends generator API to optionally skip collecting non-ACW marshal-method metadata. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/SignatureTypeProvider.cs | Switches to AssemblyIndex-owned signature provider and caches primitive TypeRefData instances. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/JavaPeerScanner.cs | Allocation reductions and caches for scanning; conditional marshal-method collection for non-ACW types. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/JavaPeerInfo.cs | Centralizes default return type to reduce per-instance allocations. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/AssemblyIndex.cs | Adds cached type/name resolution and fast-path RegisterAttribute parsing. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/TypeMapAssemblyEmitter.cs | Removes LINQ-heavy preparation path for native registration strings. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/PEAssemblyBuilder.cs | Improves PE writing behavior and reduces per-string UTF-8 field emission allocations. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/ModelBuilder.cs | Reduces allocations when deduplicating reusable wrappers. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/MetadataHelper.cs | Avoids ToArray() allocation when hashing content fingerprints. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/JniSignatureHelper.cs | Reworks ambiguity detection to avoid intermediate allocations during signature parsing. |
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
Reviewed all 12 changed files in full and found no actionable code issues. The optimized MSBuild path skips metadata that is unused for non-ACW peers while preserving the public scanner's full-detail default, and the added regression test verifies that ACW marshal metadata, generated Java sources, and emitted typemap assemblies remain identical.
CI is not currently green: Windows > Build & Smoke Test failed, while many downstream checks are still running. I could not determine whether that failure is related to this change because Azure DevOps log retrieval was blocked by the runner's unwritable CLI configuration location, so the failure should be resolved or rerun before merge.
Generated by Android PR Reviewer for #12598 · gpt56 · 529.1 AIC · ⌖ 8.78 AIC · ⊞ 25.7K
Comment /review to run again
Stacked on #12598. ## Summary - Build the final per-assembly typemap model first and persist a SHA-256 fingerprint for every output. - Skip expensive PE emission when the model fingerprint and output DLL are unchanged. - Include the generator module identity, runtime version, shared-universe mode, and every emitter-consumed model field in the fingerprint. - Fingerprint the root typemap separately from its ordered per-assembly reference list, so it regenerates when assemblies are added or removed but not for unrelated per-assembly content changes. - Keep cross-assembly alias peers in the owner assembly model, so changing an alias invalidates the owner even when the owner's source assembly did not change. - Preserve the existing default full-detail scanner behavior and byte-identical generated output. ## Incremental benchmark Release scratch harness over `TestFixtures.dll`, median of 10 batches of 100 runs. Both paths perform assembly scanning and model construction; the incremental path skips unchanged per-assembly and root PE emission. | Path | Wall time | Allocations | | --- | ---: | ---: | | Full emission | 424.1 ms | 256.7 MiB | | Unchanged incremental | 249.1 ms | 156.8 MiB | | Change | **-41.3%** | **-38.9%** | ## Correctness - Standalone `Microsoft.Android.Sdk.TrimmableTypeMap.Tests`: **779 passed**. - Added regressions for unchanged-output skipping, byte-identical callback emission, cross-assembly alias owner invalidation, and root reference-list invalidation. - Added task coverage that verifies a second run reports skipped `_Mono.Android.TypeMap` PE emission and preserves the output timestamp. - The modified targets file parses as valid XML. Full-build task/integration validation was unavailable because this worktree has no local Android SDK build (`bin/Debug/dotnet/dotnet` / `bin/Release/dotnet/dotnet`). A direct build attempt reached missing bootstrap/local environment prerequisites (including no usable JDK), so no full SDK build was started.

Summary
RegisterAttributewithout generic decoder allocationsPerformance
Measured with the 18 MiB
Microsoft.Android.Ref.36Mono.Android.dllworkload containing 8,820 Java peers:mainGenerated
_Java.Interop.TypeMap.dll,_Mono.Android.TypeMap.dll,_Microsoft.Android.TypeMaps.dll, and all 313 Java sources are byte-identical tomain.Tests
dotnet test tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests.csproj -c Release(775 passed)Mono.Android.dllgeneration and SHA-256 comparison againstmain