Skip to content

Optimize trimmable typemap generation - #12598

Merged
jonathanpeppers merged 1 commit into
mainfrom
simonrozsival-optimize-trimmable-typemap
Aug 31, 2026
Merged

jonathanpeppers merged 1 commit into
mainfrom
simonrozsival-optimize-trimmable-typemap

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

Summary

  • skip unused marshal-method, property, and constructor scanning for non-ACW types in the MSBuild task path
  • cache metadata names and type references and parse RegisterAttribute without generic decoder allocations
  • reduce typemap emitter allocations from intermediate collections, delegates, and buffers
  • preserve the public full-detail scanner behavior by default

Performance

Measured with the 18 MiB Microsoft.Android.Ref.36 Mono.Android.dll workload containing 8,820 Java peers:

Metric main This PR
Cold end-to-end 624 ms 285 ms
Cold scan 457 ms 128 ms
Warm scan 251 ms 48 ms
Total allocations 299 MiB 93 MiB

Generated _Java.Interop.TypeMap.dll, _Mono.Android.TypeMap.dll, _Microsoft.Android.TypeMaps.dll, and all 313 Java sources are byte-identical to main.

Tests

  • dotnet test tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests.csproj -c Release (775 passed)
  • full Mono.Android.dll generation and SHA-256 comparison against main

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>
Copilot AI lite review requested due to automatic review settings August 31, 2026 11:21

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.

Copilot review overview

🔵 Needs a closer look

Review tier: Lite
Findings: 1 Medium severity

New issues introduced by this change (1)
Severity Finding
Medium severity 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 collectMarshalMethodsForNonAcw option (default true) 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 (AssemblyIndex name/type ref caches, faster RegisterAttribute parsing, 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.

@simonrozsival

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #12598

@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.

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

@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Aug 31, 2026
@jonathanpeppers
jonathanpeppers merged commit dc8d21f into main Aug 31, 2026
45 checks passed
@jonathanpeppers
jonathanpeppers deleted the simonrozsival-optimize-trimmable-typemap branch August 31, 2026 15:40
simonrozsival added a commit that referenced this pull request Sep 3, 2026
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.
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants