Repository navigation
Improve synchronous async callback execution - #3313
Conversation
Add a fast path for synchronously completed ValueTasks in pipeline execution. Preserve asynchronous exception capture while avoiding state machines for common completed callbacks. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3313 +/- ##
=======================================
Coverage 98.04% 98.04%
=======================================
Files 321 321
Lines 7148 7157 +9
Branches 1105 1107 +2
=======================================
+ Hits 7008 7017 +9
Misses 98 98
Partials 42 42
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
Independent benchmark verificationI re-ran Method: short run ( Environment: BenchmarkDotNet v0.15.8; .NET 10.0.14; Windows 11 (10.0.26300); 13th Gen Intel Core i7-13700H; X64 RyuJIT x86-64-v3; InProcessEmitToolchain.
Conclusions
Note This comment was generated with Claude Code. |
Add test coverage for new optimisations.
There was a problem hiding this comment.
🟡 Changes recommended
The successful non-generic fast path skips consuming completed ValueTasks, potentially preventing resource release or pool return across all four non-result ExecuteAsync overloads.
1 open issue
What changed in this PR
This PR reduces callback overhead in Polly.Core async pipeline execution by adding a fast path for synchronously completed ValueTask callbacks.
Changes:
- Adds internal outcome-conversion helpers with synchronous fast paths and asynchronous exception capture.
- Routes result-returning and non-result async execution overloads through those helpers.
- Adds tests for completion, faults, cancellation, and asynchronous exception propagation.
| File | Description |
|---|---|
test/Polly.Core.Tests/ResiliencePipelineTests.AsyncT.cs |
Tests asynchronous result callbacks and exception propagation. |
test/Polly.Core.Tests/ResiliencePipelineTests.Async.cs |
Tests asynchronous non-result callbacks and exception propagation. |
test/Polly.Core.Tests/OutcomeTests.cs |
Tests outcome conversion for completed, pending, faulted, and canceled tasks. |
src/Polly.Core/ResiliencePipeline.AsyncT.cs |
Uses outcome-conversion helpers for result-returning callbacks. |
src/Polly.Core/ResiliencePipeline.Async.cs |
Uses outcome-conversion helpers for non-result callbacks. |
src/Polly.Core/Outcome.cs |
Adds ValueTask conversion helpers and synchronous fast paths. |
Review effort: Balanced
Address GitHub Copilot feedback on #3313.

Pull Request
The issue or feature being addressed
Reduce overhead for asynchronously exposed pipeline callbacks that complete synchronously.
Details on the issue fix or feature implementation
Add a synchronous-completion fast path that avoids async state machines when callbacks return an already-completed
ValueTask. Asynchronous callbacks retain the existing await and exception-capture behavior.BenchmarkDotNet results
Repeat baseline and changed runs, using
MediumRun(15 measurement iterations), on the same host and .NET runtime.Allocated: -is BenchmarkDotNet's report for zero measured bytes. The ratio is branch mean / main baseline mean; values below 1.00x indicate faster branch execution.Environment: BenchmarkDotNet 0.15.8; .NET 10.0.14; Ubuntu 24.04.3 (container); AMD EPYC 9V74 2.60 GHz (1 CPU, 2 logical/2 physical cores); x64 RyuJIT.
ExecuteOutcomeAsync(reference)ExecuteAsync_ResilienceContextAndStateExecuteAsync_CancellationTokenExecuteAsync_GenericStrategy_CancellationTokenExecute_ResilienceContextAndStateExecute_CancellationTokenExecute_GenericStrategy_CancellationTokenThe three async convenience execution cases improved by about 28%, 38%, and 33%, respectively. All reported scenarios remain allocation-free. The outcome API reference and synchronous controls are included for context; their between-run timings are subject to benchmark noise.
Validation:
dotnet build ./src/Polly.Core/Polly.Core.csproj --configuration Release --no-restoresucceeded for all target frameworks with 0 warnings and 0 errors.dotnet test ./test/Polly.Core.Tests/Polly.Core.Tests.csproj --configuration Release --no-restore --framework net10.0passed all 798 tests.Confirm the following
Note
This PR description was generated with GitHub Copilot.