[trimmable-type-map] Avoid unused XML references - #12813
Draft
simonrozsival wants to merge 1 commit into
Draft
simonrozsival wants to merge 1 commit into
simonrozsival wants to merge 1 commit into
Conversation
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>
Contributor
There was a problem hiding this comment.
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
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
[Export]dispatch assembliesSystem.Xml.ReaderWriterto a generated type-map assembly when exported signatures do not use XML parser mappingsXmlPullParserorXmlResourceParsermappings are usedAPK size and combined MAUI validation
The PR is draft because the stock MAUI
sample-contentapplication 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:
Three independent MAUI trimming fixes were then applied together:
ShadowTypeConverter's regex dependencyThe combined MAUI changes reduced the sample-content signed APK by 81,920 bytes, removed
XamlLoader,XamlParser, andLocalhostRegex, and reduced the retained XML/regex implementations. However,System.Xml.ReaderWriterremains rooted directly by source-generated XAML line-info calls in the application, andSystem.Private.Xmlremains 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:
System.Xml.ReaderWriter.dllSystem.Private.Xml.dllAll 86 generated type-map DLLs were byte-identical, and none had a
System.Xml.ReaderWriterassembly reference. The sample-content app and its dependencies do not contain a non-XML[Export]dispatcher that triggers the bug fixed here. Its remainingSystem.Xml.ReaderWriterreference comes from generated XAMLInitializeComponentline-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:minimalTypeMapAssemblyGeneratorTests.Generate_Export*coverage