Repository navigation
Reduce allocations in DefaultLinkGenerator - #70127
Conversation
- Build the endpoint list without LINQ (OfType + ToList) when the address scheme returns an array. - Only compute the combined route values (a full RouteValueDictionary copy plus ambient values) when the template binder has constraints, since they are only consumed by constraint evaluation. Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the routing allocation optimizations.
No open issues 🎉
What changed in this PR
This PR reduces per-call allocations in routing link generation and parsing while preserving public TemplateBinder.GetValues behavior and leaving the public API unchanged.
Changes:
- Adds an array fast path for filtering route endpoints, shared by the link generator and parser.
- Separates accepted-value processing from combined-value construction.
- Skips combined route-value allocation when link generation has no constraints to evaluate.
| File | Description |
|---|---|
src/Http/Routing/src/Template/TemplateBinder.cs |
Extracts internal value-processing helpers and exposes constraint presence internally. |
src/Http/Routing/src/DefaultLinkParser.cs |
Uses the shared endpoint-filtering fast path. |
src/Http/Routing/src/DefaultLinkGenerator.cs |
Adds array-based endpoint filtering and avoids combined-value allocation for unconstrained routes. |
Review effort: Balanced
|
/azp run aspnetcore-ci The only failure is the Helix job monitor exiting with an unhandled exception. Test results show 0 failures (71,410 passed), so this looks like infrastructure. Rerunning the pipeline. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
No pipelines are associated with this pull request. |
|
/azp run aspnetcore-ci The only failure was the Helix job monitor crashing with an unhandled exception (infrastructure). All 71,410 tests that ran passed, with 0 failures, so I'm rerunning the pipeline. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
No pipelines are associated with this pull request. |
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
|
The second CI run had two single-test failures that look unrelated to this change. One was in 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
The rerun had 2 test failures. Both look unrelated flakes, and both tests passed on the identical code in the first run:
The first run had 0 test failures (71,410 passed) and only failed because the Helix job monitor crashed. I am retriggering CI once more. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <[email protected]>
|
The latest run failed only in the Blazor E2E (Mono server) leg, on 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
CI on fe3f15e: everything passed except the Blazor E2E "Mono client and WebAssembly" leg, which had 2 failures out of 3,803 tests. Both are unrelated flaky tests:
🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <[email protected]>
|
CI on f02c105: all
Both tests fail on unrelated PRs too, according to the e2e check annotations:
These are browser-side tests. The NavigationLock failure is the browser staying on 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <[email protected]>
|
Final rerun on fa421e8. The failures are all outside this change:
Across 6 runs the product code has been identical, and every non-E2E leg has been green except for one-off infra or unrelated failures. I'm not pushing more empty commits. This needs a maintainer to rerun the failed jobs in AzDO or accept them as known flakes. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
/azp run aspnetcore-components-e2e, aspnetcore-ci 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
/azp run aspnetcore-ci 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
No pipelines are associated with this pull request. |
Co-authored-by: Copilot <[email protected]>
|
Rerun status:
I pushed an empty commit so that all pipelines rerun on a new head. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
/review |
|
✅ No documentation PR was created. Confidence that documentation is needed: 3% No docs needed: internal allocation-reduction refactoring with no observable behavior or public API changes. Documentation surfaces considered
|
Reduce allocations in DefaultLinkGenerator
Reduce per-call allocations in link generation (
DefaultLinkGenerator/DefaultLinkParser).Description
OfType+ToList) when the address scheme returns an array.RouteValueDictionarycopy plus ambient values) when the template binder has constraints, since they are only consumed by constraint evaluation.The public
TemplateBinder.GetValueskeeps its behavior; it is now built on two new internal helpers (GetAcceptedValues,GetCombinedValues) plus an internalHasConstraints. No public API changes.Results
Routing microbenchmarks; local BenchmarkDotNet on a noisy 2-vCPU VM, interleaved A/B rounds:
Tests
Routing unit tests: 3505/3505 passing. Routing microbenchmarks project builds.
No linked issue; this is a perf-only change.
Benchmark (3 interleaved rounds)
Existing link-generation benchmarks in
src/Http/Routing/perf/Microbenchmarks/LinkGeneration: theEndpointRoutingmethods, plusSingleRouteRouteValuesAddressSchemeBenchmarkas a control, since it only callsFindEndpointsand doesn't reach the changed code.Environment: AMD EPYC 9V74, 2 vCPU (container), Ubuntu 24.04; .NET SDK 12.0.100-alpha.1.26521.103, .NET 12.0.0 (12.0.26.52911); BenchmarkDotNet v0.13.0
--config perflab(InProcess, MemoryDiagnoser), WarmupCount=3, IterationCount=10. "main" is the merge base4f556f73a6. Both builds use the same benchmark source, and runs were interleaved main, PR, main, PR, main, PR. Each cell shows mean / allocated per operation; allocation figures are exact bytes from the CSV exporter.LinkGeneratorscenarios allocate less in every round: no parameters 728 → 520 B, with parameters 1,144 → 888 B, GitHub 1,072 → 784 B, and with constraints 1,144 → 1,032 B. That last case still builds the combined values, so it saves only the LINQ allocations.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)