Skip to content

[trimmable-type-map] Avoid unused XML references - #12813

Draft
simonrozsival wants to merge 1 commit into
mainfrom
simonrozsival-trim-xml-assemblies
Draft

simonrozsival wants to merge 1 commit into
mainfrom
simonrozsival-trim-xml-assemblies

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Sep 17, 2026

Copy link
Copy Markdown
Member

Summary

  • create XML marshalling metadata references lazily in generated [Export] dispatch assemblies
  • avoid adding System.Xml.ReaderWriter to a generated type-map assembly when exported signatures do not use XML parser mappings
  • preserve the existing XML marshalling references when XmlPullParser or XmlResourceParser mappings are used

APK size and combined MAUI validation

The PR is draft because the stock MAUI sample-content application does not currently demonstrate an APK-size or packaged-assembly reduction from this change.

The first stock .NET 11 Android CoreCLR Release comparison produced an identical signed APK before and after this change:

Build Signed APK size
Baseline 23,300,175 bytes
Patched 23,300,175 bytes
Difference 0 bytes

Three independent MAUI trimming fixes were then applied together:

The combined MAUI changes reduced the sample-content signed APK by 81,920 bytes, removed XamlLoader, XamlParser, and LocalhostRegex, and reduced the retained XML/regex implementations. However, System.Xml.ReaderWriter remains rooted directly by source-generated XAML line-info calls in the application, and System.Private.Xml remains transitively and through additional framework/control references.

To exercise this PR's actual code path, the identical combined-MAUI input was then published twice with the trimmable Android type map explicitly enabled:

dotnet publish CombinedTrimApp.csproj \
  -c Release -f net11.0-android -r android-arm64 \
  -p:AndroidKeyStore=false \
  -p:PublishReadyToRun=false \
  -p:TrimMode=partial \
  -p:_AndroidTypeMapImplementation=trimmable \
  -p:AndroidPackageFormats=apk
Output Baseline generator Patched generator Difference
Signed APK 21,248,079 B 21,248,079 B 0 B
Unsigned APK 21,118,994 B 21,119,021 B +27 B packaging noise
Generated type-map DLLs 11,133,952 B 11,133,952 B 0 B
System.Xml.ReaderWriter.dll 5,120 B 5,120 B 0 B
System.Private.Xml.dll 381,440 B 381,440 B 0 B

All 86 generated type-map DLLs were byte-identical, and none had a System.Xml.ReaderWriter assembly reference. The sample-content app and its dependencies do not contain a non-XML [Export] dispatcher that triggers the bug fixed here. Its remaining System.Xml.ReaderWriter reference comes from generated XAML InitializeComponent line-info services (System.Xml.IXmlLineInfo), outside the Android type-map generator.

Therefore this change is valid generator cleanup and is covered by focused tests, but it does not currently reduce the stock MAUI sample-content APK or remove its XML assemblies. The benefit applies only to an app/library that generates a non-XML [Export] dispatcher and has no independent XML roots.

Tests

  • dotnet test tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests.csproj --no-restore --nologo -v:minimal
  • targeted TypeMapAssemblyGeneratorTests.Generate_Export* coverage

Create XML marshalling metadata lazily so non-XML Export methods do not root System.Xml.ReaderWriter and System.Private.Xml.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings September 17, 2026 06:25

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

Add coverage for XML parameter/return paths and all supported XML assembly identities.

Review effort: Lite
Findings: None

What changed in this PR

This PR lazily creates XML marshalling metadata to avoid unnecessary XML references in generated dispatch assemblies.

Changes:

  • Defers XML metadata reference creation until needed.
  • Preserves references for XML parser mappings.
  • Adds conditional-reference regression tests.
File Summary
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.Tests/​Generator/​TypeMapAssemblyGeneratorTests.cs Tests conditional XML reference emission.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​Generator/​ExportMethodDispatchEmitterContext.cs Adds lazy XML metadata reference getters.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​Generator/​ExportMethodDispatchEmitter.cs Uses lazy getters for XML marshalling emission.

@simonrozsival
simonrozsival marked this pull request as draft September 17, 2026 06:54
kubaflo pushed a commit to dotnet/maui that referenced this pull request Sep 18, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Root Cause

`ShadowTypeConverter` constructs a runtime `Regex` whenever it parses a
shadow string. That reference contributes to retaining
`System.Text.RegularExpressions` and additional regex implementation
code in trimmed MAUI Android apps.

### Broader goal and related work

This PR is one step toward removing the roughly 300 KB linked
`System.Text.RegularExpressions` assembly from MAUI apps that do not use
regular expressions in their own application logic. Removing the
`ShadowTypeConverter` root is not sufficient by itself because other
framework and dependency roots still retain regex code.

Related work toward that goal includes:

- #38616 trims disabled MAUI Aspire support, including
`Microsoft.Maui`'s source-generated `LocalhostRegex`, when the Aspire
feature switch is off.
- dotnet/android#12813 avoids unnecessary XML marshalling references in
trimmable type-map dispatch assemblies. This helps remove XML assemblies
where they are not otherwise needed; `System.Private.Xml` contains a
generated `LanguageRegex` implementation that can independently retain
regex code.

Additional roots may still need to be removed or made trimmable before
`System.Text.RegularExpressions` disappears entirely from a given app.
For example, the measured sample-content app also reaches regex through
`CommunityToolkit.Maui`, `System.Data.Common`, and `System.Private.Xml`.

### Description of Change

Replaces the runtime `Regex` with an allocation-conscious inline
tokenizer/parser.

The parser intentionally preserves the existing unanchored
`Regex.Matches` behavior, including supported color forms,
invariant-culture decimal/scientific numbers, Unicode whitespace,
ignored punctuation, greedy 3-to-8 digit hex matching, and rollback for
incomplete decimals or exponents.

Token boundaries are stored in a stack-allocated `Span<Part>`, and only
matched tokens are materialized as strings for the existing color and
numeric parsers.

The project now also bans the `System.Text.RegularExpressions` namespace
through a `Controls.Core`-specific `BannedSymbols.txt`, preventing this
dependency from being reintroduced.

### Performance considerations

Vectorizing the character scans was considered but intentionally not
implemented. Shadow strings are normally short (roughly tens of
characters) and whitespace runs are generally zero to two characters, so
SIMD setup and branching are unlikely to beat the straightforward scalar
loops. A vectorized path would also require separate `netstandard`
fallbacks and careful tables to preserve the old regex's Unicode `\s`
and `\d` behavior, adding disproportionate complexity without
demonstrated benefit.

### Tests

- Added focused coverage for every supported color syntax,
offsets/radius/opacity, whitespace, malformed values, invariant culture,
and legacy regex edge cases.
- `ShadowTests`: 85 passed.
- `Controls.Core` builds for `netstandard2.0`, `netstandard2.1`, and
`net10.0`.
- Verified an intentional `System.Text.RegularExpressions.Regex` usage
in `Controls.Core` fails with RS0030.

### Android size measurement

Published identical stock `.NET 11 preview 7` `dotnet new maui
--sample-content` apps in Release with `TrimMode=partial`, replacing
only the `Microsoft.Maui.Controls.dll` input in the patched build.

| Artifact | Baseline | Patched | Delta |
|---|---:|---:|---:|
| Signed APK | 36,903,788 B | 36,899,692 B | **-4,096 B** |
| Unsigned APK | 36,774,978 B | 36,771,221 B | **-3,757 B** |
| Linked `Microsoft.Maui.Controls.dll` (per ABI) | 1,495,552 B |
1,495,040 B | **-512 B** |
| Linked `System.Text.RegularExpressions.dll` (per ABI) | 334,336 B |
330,752 B | **-3,584 B** |
| arm64 assembly store | 7,689,840 B | 7,688,216 B | **-1,624 B** |
| x64 assembly store | 7,899,160 B | 7,896,448 B | **-2,712 B** |

`System.Text.RegularExpressions` remains retained by other roots in this
sample: `CommunityToolkit.Maui`, `Microsoft.Maui` (`LocalhostRegex`),
`System.Data.Common`, and `System.Private.Xml`. This change removes the
`Microsoft.Maui.Controls` reference and reduces the linked regex
implementation, but does not eliminate the assembly from this app.

### Issues Fixed

None.

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants