Skip to content

Use spans to reduce allocations in Essentials - #40783

Merged
kubaflo merged 2 commits into
mainfrom
simonrozsival-net12-essentials-spans
Nov 30, 2026
Merged

kubaflo merged 2 commits into
mainfrom
simonrozsival-net12-essentials-spans

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 6f75ab472d03f5dc70154418240bf2338f098b65

Dimension Assessment Rationale
Change confidence 8/10 ↑ Both Essentials commits rebased without changes, confirmed by range-diff; review found no substantive issue in traversal checks, coordinate conversion or query decoding.
↑ All 550 Essentials tests pass on net12.0; unit-test and Release benchmark builds succeed without warnings.
↓ Platform builds and external-consumer compatibility remain unverified; inherited benchmark measurements do not cover the formatting follow-up; CI validation is not complete.

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 from this PR and let us know in a comment if this change resolves your issue. Thank you!

Target Branch

This PR is for Initial base
New features, API changes, or .NET 12 work (including .NET 12 fixes) main
.NET 11 servicing fixes net11, for review
Older servicing, release integration, infrastructure, or documentation The branch designated for that work

For .NET 11 fixes, only a maintainer retargets the approved PR to the active
inflight lane and merges it after CI validates that final base. Do not merge
individual fixes directly into net11; agents must not retarget or merge.

.NET 12 stack: this layer targets simonrozsival-net12-simplify-xaml-component-registry (#40782), rather than duplicating its prerequisite changes against main.

Description of Change

Prepared for review following the XamlC removal. Broader discussion and evaluation of external consumers requiring netstandard remain necessary before merging the stack. Essentials is also consumed standalone, so compatibility must be evaluated beyond applications using the full MAUI stack.

Current prerequisite stack, bottom to top: #40775 (drop netstandard) → #40776 (Graphics) → #40778 (Markdig) → #40779 (Core) → #40780 (Controls.Core) → #40782 (Controls.Xaml) → this PR. The former XamlC-removal prerequisite #40608 is now incorporated upstream. This layer's two commits are rebased onto parent f1dfc0cea27b57c5f26e4d62c16df76d650ca3c0 without patch changes.

Use spans for relative-path segment validation, latitude/longitude parsing, and query parameter decoding. Query values containing + use a reusable stack buffer up to 256 characters, with a heap buffer for larger values. Format LocationTypeConverter.ConvertTo output with string.Create(CultureInfo.InvariantCulture, ...), avoiding two intermediate numeric strings while preserving the exact output and NotSupportedException behavior. Add 30 test cases covering traversal segments, malformed coordinates, exact location formatting, invariant culture, invalid conversion values, query decoding and stack-buffer boundaries. TextToSpeech chunking is intentionally skipped: chunks are strings anyway.

This layer contains Essentials only, plus its benchmark methods. The six Essentials files are taken from 04fb9f8a715163e48978e576bdf31c8c33d1b488, plus a follow-up limited to LocationTypeConverter.ConvertTo and its tests. The benchmark differs from that source only by omitting the Maps import, field and method. Maps code, Maps tests and Core.Benchmarks.csproj are unchanged; Maps follows in a separate layer.

Inherited benchmark measurements — not re-run in this layer, and they do not measure the ConvertTo formatting follow-up. BenchmarkDotNet Release, --job short --inProcess, Apple Silicon; measured on the combined Essentials/Maps source. The exact runtime was not preserved, so no runtime version is claimed. Allocations are reliable; timings are noisy short-run measurements, not guaranteed real-world gains.

Operation Mean before Mean after Allocated before Allocated after
FileSystemUtils.IsValidRelativePath 45.6 ns 9.2 ns 288 B 0 B
WebUtils.ParseQueryString 229.2 ns 183.9 ns 1192 B 880 B
LocationTypeConverter.ConvertFrom 135.2 ns 99.5 ns 440 B 208 B

Validation re-run at the assessed rebased head:

dotnet build src/Essentials/test/UnitTests/Essentials.UnitTests.csproj -p:IncludeAndroidTargetFrameworks=false -p:IncludeiOSTargetFrameworks=false -p:IncludeMacCatalystTargetFrameworks=false -p:IncludeWindowsTargetFrameworks=false --verbosity quiet

Passed: 0 warnings, 0 errors.

LC_ALL=en_US.UTF-8 dotnet test --project src/Essentials/test/UnitTests/Essentials.UnitTests.csproj --no-build -p:IncludeAndroidTargetFrameworks=false -p:IncludeiOSTargetFrameworks=false -p:IncludeMacCatalystTargetFrameworks=false -p:IncludeWindowsTargetFrameworks=false

Passed on net12.0/arm64: 550 tests, 0 failed, 0 skipped, including the 30 new cases. Local SDK: 12.0.100-alpha.1.26551.101. The old xunit.console v2 invocation failed to load xunit.abstractions; the parent migrated this test project to xUnit v3/Microsoft.Testing.Platform, so validation now uses its supported dotnet test runner. No test failure or source fix was needed.

dotnet build src/Core/tests/Benchmarks/Core.Benchmarks.csproj -c Release -p:IncludeAndroidTargetFrameworks=false -p:IncludeiOSTargetFrameworks=false -p:IncludeMacCatalystTargetFrameworks=false -p:IncludeWindowsTargetFrameworks=false --verbosity quiet
git range-diff 2bfe1fac7f..27de68af8a f1dfc0cea2..HEAD
git diff --check

Passed: Release benchmark build has 0 warnings, 0 errors and no Maps reference; range-diff confirms both commits are unchanged; whitespace check is clean.

Previously run before rebase, not re-run:

dotnet format Microsoft.Maui.sln --no-restore --include src/Essentials/src/FileSystem/FileSystemUtils.shared.cs src/Essentials/src/Types/LocationTypeConverter.shared.cs src/Essentials/src/Types/Shared/WebUtils.shared.cs src/Essentials/test/UnitTests/FileSystemUtils_Tests.cs src/Essentials/test/UnitTests/LocationTypeConverter_Tests.cs src/Essentials/test/UnitTests/WebUtils_Tests.cs src/Core/tests/Benchmarks/Benchmarks/EssentialsMapsBenchmarker.cs --exclude Templates/src --exclude-diagnostics CA1822 --verbosity quiet
git diff --stat 04fb9f8a715163e48978e576bdf31c8c33d1b488 HEAD -- src/Essentials

Passed before rebase: formatting completed; the Essentials source comparison contained only the LocationTypeConverter follow-up and its tests. The rebase preserved both patches unchanged. Platform builds, external-consumer compatibility and benchmark execution were not run in this layer. CI is not claimed green: Build Insights is in progress; the required ci-analysis skill was unavailable for detailed AzDO verification. Ready for human review does not imply ready to merge.

Issues Fixed

N/A — performance modernization; no bug issue is claimed fixed.

Let AI finish this PR

  • Let the MAUI agent address review feedback and fix remaining issues on this PR. Maintainers still decide whether it is merged. For PRs from forks, keep "Allow edits by maintainers" on so the agent can push to your branch; otherwise it opens a follow-up PR that keeps your commits.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@simonrozsival simonrozsival changed the title [net12] Use spans to reduce allocations in Essentials Use spans to reduce allocations in Essentials Nov 28, 2026
@kubaflo

kubaflo commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

/review expert-review

@kubaflo

This comment has been minimized.

@github-actions

This comment has been minimized.

@kubaflo

kubaflo commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

/review expert-review

@kubaflo

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

Selected Tests

@kubaflo — AI-selected UI tests and device tests for commit 08a9a317.

Result NOT QUEUED Platforms none Tests 0 UI, 0 device Commit 08a9a317


🧪 Test selection — click to expand
Suite Result Selected Build
UI tests ❌ not queued none —
Device tests ❌ not queued none —
🧪 UI tests — nothing to run

❌ Could not queue maui-pr-uitests: No AI model produced a test selection, so nothing was queued. Comment /review run-tests again, or use /azp run maui-pr-uitests for the full suite.

🧪 Running — nothing

No UI tests run for this PR.

🧪 Device tests — nothing to run

❌ Could not queue maui-pr-devicetests: No AI model produced a test selection, so nothing was queued. Comment /review run-tests again, or use /azp run maui-pr-devicetests for the full suite.

🧪 Running — nothing

No device tests run for this PR.

⏭️ Skipped — what neither suite runs

UI tests — 77 categories, 4 platforms

Platforms not run: Android, iOS, MacCatalyst, Windows

Each skipped platform and category is listed as a maui-pr-uitests (skipped) … status on this commit. Use /azp run maui-pr-uitests for the full suite.

Device tests — 105 categories, 4 platforms

Platforms not run: Android, iOS, MacCatalyst, Windows

Each skipped platform and project is listed as a maui-pr-devicetests (skipped) … status on this commit. Use /azp run maui-pr-devicetests for the full suite.

🤖 Why these tests — AI rationale

UI tests

No rationale provided.

Device tests

No rationale provided.

Generated by /review run-tests. A targeted run is a fast signal, not full-suite certification.

@simonrozsival
simonrozsival force-pushed the simonrozsival-net12-essentials-spans branch from 08a9a31 to 27de68a Compare November 30, 2026 11:57
@github-actions

This comment has been minimized.

@simonrozsival
simonrozsival force-pushed the simonrozsival-net12-essentials-spans branch from 27de68a to 6f75ab4 Compare November 30, 2026 12:11
@simonrozsival
simonrozsival marked this pull request as ready for review November 30, 2026 12:14
Copilot AI balanced review requested due to automatic review settings November 30, 2026 12:14

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.

🔵 Needs a closer look

The intentional removal of netstandard assets leaves standalone Essentials compatibility and migration paths unvalidated, requiring maintainer evaluation of the tradeoff.

No open issues 🎉

What changed in this PR

This PR reduces temporary allocations in Essentials path validation, location conversion, and query decoding as part of the .NET 12 modernization stack. Maps changes remain outside this layer.

Changes:

  • Replace string splitting and intermediate strings with span-based parsing and reusable query-decoding buffers.
  • Format locations using invariant-culture string.Create.
  • Add 30 regression test cases and benchmarks for three Essentials operations.
File Description
src/​Essentials/​test/​UnitTests/​WebUtils_Tests.cs Test query decoding, buffer boundaries, and fragment handling.
src/​Essentials/​test/​UnitTests/​LocationTypeConverter_Tests.cs Test malformed coordinates, exact formatting, and invariant culture.
src/​Essentials/​test/​UnitTests/​FileSystemUtils_Tests.cs Expand path-segment and traversal validation coverage.
src/​Essentials/​src/​Types/​Shared/​WebUtils.shared.cs Decode query spans with a reusable stack buffer and heap fallback.
src/​Essentials/​src/​Types/​LocationTypeConverter.shared.cs Parse coordinate spans and avoid intermediate formatting strings.
src/​Essentials/​src/​FileSystem/​FileSystemUtils.shared.cs Validate path segments without allocating a split array.
src/​Core/​tests/​Benchmarks/​Benchmarks/​EssentialsMapsBenchmarker.cs Benchmark Essentials parsing and path validation.

Review effort: Balanced

@kubaflo
kubaflo force-pushed the simonrozsival-net12-essentials-spans branch from 6f75ab4 to 14eb983 Compare November 30, 2026 12:43
Copilot AI balanced review requested due to automatic review settings November 30, 2026 12:43
@kubaflo
kubaflo force-pushed the simonrozsival-net12-essentials-spans branch 2 times, most recently from 88facea to ebed120 Compare November 30, 2026 12:45
@kubaflo
kubaflo force-pushed the simonrozsival-net12-essentials-spans branch from ebed120 to 186cec8 Compare November 30, 2026 12:46

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.

🔵 Needs a closer look

Removing netstandard assets leaves compatibility and migration paths for standalone Essentials consumers unresolved, requiring maintainer review beyond the supplied host-only validation.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 30, 2026 12:47
@kubaflo
kubaflo force-pushed the simonrozsival-net12-essentials-spans branch from 186cec8 to e694a64 Compare November 30, 2026 12:47
Base automatically changed from simonrozsival-net12-simplify-xaml-component-registry to main November 30, 2026 12:49
simonrozsival and others added 2 commits November 30, 2026 13:49
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@kubaflo
kubaflo force-pushed the simonrozsival-net12-essentials-spans branch from e694a64 to 06a732a Compare November 30, 2026 12:49
@kubaflo
kubaflo merged commit 6020a18 into main Nov 30, 2026
4 of 5 checks passed
@kubaflo
kubaflo deleted the simonrozsival-net12-essentials-spans branch November 30, 2026 12:50
kubaflo added a commit that referenced this pull request Nov 30, 2026
<!-- copilot-assessment:v1 -->
### Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: `cc5e8b30c6a4a9e4b9c7ec8fe4802536ef6fb4b7`

| Dimension | Assessment | Rationale |
|---|---|---|
| Change confidence | 7.5/10 | ↑ Clean rebase onto Essentials
`6f75ab472d`; diff is the same five Maps files.<br>↑ On net12.0: 200/200
Map-filtered Controls.Core tests and 19/19 converter tests pass;
benchmark project builds with 0 warnings; format check clean.<br>↓ net12
Android MapHandler not compiled locally (NETSDK1139: the installed
Android workload targets net11); relies on CI.<br>↓ Benchmark numbers
are historical and not rerun; ConvertTo change not benchmarked; CI for
this head not inspected. |
<!-- /copilot-assessment -->

<!-- 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!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

This is the final Maps layer of the .NET 12 spans stack. Its immediate
base is `simonrozsival-net12-essentials-spans` (#40783), rebased onto
that branch's head `6f75ab472d03f5dc70154418240bf2338f098b65`. GitHub
native stack registration is handled separately.

### Description of Change

<!-- Enter description of the fix in this section -->

> [!IMPORTANT]
> Reviewers: please weigh the risk of breaking external consumers and
dependencies that require netstandard before merging. That compatibility
evaluation has not been done in this PR.

- Parse MapSpan coordinates through stack-allocated ranges and
span-based numeric parsing instead of allocating a split string array
and substrings. Exactly-four-part validation, whitespace handling,
invariant-culture parsing and exceptions are unchanged.
- Build Android polygon/polyline point lists with a pre-sized indexed
loop instead of LINQ projection and list growth.
- Format `ConvertTo` output with a single
`string.Create(CultureInfo.InvariantCulture, ...)` interpolation instead
of four separate `ToString(CultureInfo.InvariantCulture)` calls and two
concatenated strings. Output order, separators, invariant numeric
formatting and `NotSupportedException` for non-`MapSpan` values are
unchanged.
- Add malformed-input, exact-output, non-invariant-culture and
invalid-value converter tests. Add the Maps converter benchmark and
project reference, keeping the Essentials benchmarks from the base.

Only these five files differ from the immediate base:

- `src/Core/maps/src/Converters/MapSpanTypeConverter.cs`
- `src/Core/maps/src/Handlers/Map/MapHandler.Android.cs`
- `src/Controls/tests/Core.UnitTests/MapSpanTypeConverterTests.cs`
- `src/Core/tests/Benchmarks/Benchmarks/EssentialsMapsBenchmarker.cs`
(Maps import, field and method only)
- `src/Core/tests/Benchmarks/Core.Benchmarks.csproj` (Maps project
reference only)

#### Benchmark evidence (historical, from the earlier combined-source
run)

| Operation | Before time | After time | Before allocated | After
allocated |
| --- | ---: | ---: | ---: | ---: |
| `MapSpanTypeConverter.ConvertFrom` | 166.2 ns | 118.4 ns | 488 B | 176
B |

These figures are historical. They come from an earlier benchmark of the
combined source branch (commit `04fb9f8a71`) and were **not rerun for
this PR or this head**. That run's exact runtime was not preserved, so
no .NET 12 benchmark runtime is claimed. Short-run timings are noisy,
and this split layer was not measured on its own. There is no benchmark
evidence for the Android polygon/polyline change, and the `ConvertTo`
formatting change was not benchmarked.

#### Local validation (head `cc5e8b30c6`)

Local SDK: `12.0.100-alpha.1.26551.101`. Portable output TFM:
**`net12.0`**. Installed workloads: android, ios and maccatalyst, all on
net11 (rc.2) manifests.

| Exact command | Result |
| --- | --- |
| `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj
-p:IncludeAndroidTargetFrameworks=false
-p:IncludeIosTargetFrameworks=false
-p:IncludeMacCatalystTargetFrameworks=false --filter
'FullyQualifiedName~Map' -v:q` | Passed: 200 tests, 0 failed, 0 skipped
(net12.0). |
| `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj
-p:IncludeAndroidTargetFrameworks=false
-p:IncludeIosTargetFrameworks=false
-p:IncludeMacCatalystTargetFrameworks=false --filter
'FullyQualifiedName~MapSpanTypeConverterTests' -v:q` | Passed: 19 tests,
0 failed (net12.0). |
| `dotnet build src/Core/tests/Benchmarks/Core.Benchmarks.csproj
-p:IncludeAndroidTargetFrameworks=false
-p:IncludeIosTargetFrameworks=false
-p:IncludeMacCatalystTargetFrameworks=false -v:q` | Passed: 0 warnings,
0 errors. Benchmarks were not executed. |
| `dotnet build src/Core/maps/src/Maps.csproj -f net12.0-android
-p:IncludeIosTargetFrameworks=false
-p:IncludeMacCatalystTargetFrameworks=false -v:q` | **Blocked:**
NETSDK1139 (`android` not recognized for net12.0-android37.0).
**`MapHandler.Android.cs` was not compiled locally**, so CI must cover
it. |
| `dotnet format whitespace <project> --no-restore --verify-no-changes
--include <changed .cs files> --verbosity quiet` (Maps,
Controls.Core.UnitTests and Core.Benchmarks projects) | Passed (exit 0)
for all three. |
| `git diff --quiet 94138e4 HEAD -- <Maps source/test files>` |
Passed: the rebase left the Maps converter, handler and test content
unchanged. |

I did not inspect CI for this head.

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

N/A — performance work, not a bug fix.

<!--
Verify the target branch follows the release workflow in
docs/ReleaseBranchWorkflow.md.
Infrastructure and documentation changes should target the branch where
they apply.
-->

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>

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.

🔵 Needs a closer look

The change depends on removing netstandard assets from Essentials, leaving standalone-consumer compatibility and platform builds unvalidated and requiring maintainer evaluation of those integration risks before merge.

No open issues 🎉

Review effort: Balanced

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.

3 participants