Skip to content

Commit

Permalink
remove some redundant async state machines (#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jan 7, 2023
1 parent 55aaa7c commit a2cc212
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 100 deletions.
16 changes: 8 additions & 8 deletions src/Polly.Benchmarks/Bulkhead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public void Bulkhead_Synchronous()
}

[Benchmark]
public async Task Bulkhead_Asynchronous()
public Task Bulkhead_Asynchronous()
{
await AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync());
return AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync());
}

[Benchmark]
public async Task Bulkhead_Asynchronous_With_CancellationToken()
public Task Bulkhead_Asynchronous_With_CancellationToken()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
return AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
}

[Benchmark]
Expand All @@ -35,14 +35,14 @@ public int Bulkhead_Synchronous_With_Result()
}

[Benchmark]
public async Task<int> Bulkhead_Asynchronous_With_Result()
public Task<int> Bulkhead_Asynchronous_With_Result()
{
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
return AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
}

[Benchmark]
public async Task<int> Bulkhead_Asynchronous_With_Result_With_CancellationToken()
public Task<int> Bulkhead_Asynchronous_With_Result_With_CancellationToken()
{
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
return AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
}
}
12 changes: 6 additions & 6 deletions src/Polly.Benchmarks/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public class Cache
private static readonly Context MissContext = new Context(nameof(MissContext));

[GlobalSetup]
public async Task GlobalSetup()
public Task GlobalSetup()
{
SyncPolicyHit.Execute((context) => GetObject(), HitContext);
await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
return AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
}

[Benchmark]
Expand All @@ -36,9 +36,9 @@ public object Cache_Synchronous_Hit()
}

[Benchmark]
public async Task<object> Cache_Asynchronous_Hit()
public Task<object> Cache_Asynchronous_Hit()
{
return await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
return AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
}

[Benchmark]
Expand All @@ -48,9 +48,9 @@ public object Cache_Synchronous_Miss()
}

[Benchmark]
public async Task<object> Cache_Asynchronous_Miss()
public Task<object> Cache_Asynchronous_Miss()
{
return await AsyncPolicyMiss.ExecuteAsync((context, token) => GetObjectAsync(token), MissContext, CancellationToken.None);
return AsyncPolicyMiss.ExecuteAsync((context, token) => GetObjectAsync(token), MissContext, CancellationToken.None);
}

private static object GetObject() => new object();
Expand Down
8 changes: 4 additions & 4 deletions src/Polly.Benchmarks/CircuitBreaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public void CircuitBreaker_Synchronous_Succeeds()
}

[Benchmark]
public async Task CircuitBreaker_Asynchronous_Succeeds()
public Task CircuitBreaker_Asynchronous_Succeeds()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
return AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
}

[Benchmark]
Expand All @@ -30,8 +30,8 @@ public int CircuitBreaker_Synchronous_With_Result_Succeeds()
}

[Benchmark]
public async Task<int> CircuitBreaker_Asynchronous_With_Result_Succeeds()
public Task<int> CircuitBreaker_Asynchronous_With_Result_Succeeds()
{
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
return AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
}
}
8 changes: 4 additions & 4 deletions src/Polly.Benchmarks/Fallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public int Fallback_Synchronous_Succeeds()
}

[Benchmark]
public async Task<int> Fallback_Asynchronous_Succeeds()
public Task<int> Fallback_Asynchronous_Succeeds()
{
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
return AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
}

[Benchmark]
Expand All @@ -29,8 +29,8 @@ public int Fallback_Synchronous_Throws()
}

[Benchmark]
public async Task<int> Fallback_Asynchronous_Throws()
public Task<int> Fallback_Asynchronous_Throws()
{
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncThrowsAsync<int, InvalidOperationException>());
return AsyncPolicy.ExecuteAsync(() => Workloads.FuncThrowsAsync<int, InvalidOperationException>());
}
}
8 changes: 4 additions & 4 deletions src/Polly.Benchmarks/NoOp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public void NoOp_Synchronous()
}

[Benchmark]
public async Task NoOp_Asynchronous()
public Task NoOp_Asynchronous()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
return AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
}

[Benchmark]
Expand All @@ -29,8 +29,8 @@ public int NoOp_Synchronous_With_Result()
}

[Benchmark]
public async Task<int> NoOp_Asynchronous_With_Result()
public Task<int> NoOp_Asynchronous_With_Result()
{
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
return AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
}
}
8 changes: 4 additions & 4 deletions src/Polly.Benchmarks/PolicyWrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public void PolicyWrap_Synchronous()
}

[Benchmark]
public async Task PolicyWrap_Asynchronous()
public Task PolicyWrap_Asynchronous()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
return AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
}

[Benchmark]
Expand All @@ -39,8 +39,8 @@ public int PolicyWrap_Synchronous_With_Result()
}

[Benchmark]
public async Task<int> PolicyWrap_Asynchronous_With_Result()
public Task<int> PolicyWrap_Asynchronous_With_Result()
{
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
return AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
}
}
8 changes: 4 additions & 4 deletions src/Polly.Benchmarks/RateLimit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public void RateLimit_Synchronous_Succeeds()
}

[Benchmark]
public async Task RateLimit_Asynchronous_Succeeds()
public Task RateLimit_Asynchronous_Succeeds()
{
await AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync());
return AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync());
}

[Benchmark]
Expand All @@ -29,8 +29,8 @@ public int RateLimit_Synchronous_With_Result_Succeeds()
}

[Benchmark]
public async Task<int> RateLimit_Asynchronous_With_Result_Succeeds()
public Task<int> RateLimit_Asynchronous_With_Result_Succeeds()
{
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
return AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
}
}
20 changes: 10 additions & 10 deletions src/Polly.Benchmarks/Retry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public void Retry_Synchronous_Succeeds()
}

[Benchmark]
public async Task Retry_Asynchronous_Succeeds()
public Task Retry_Asynchronous_Succeeds()
{
await AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync());
return AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync());
}

[Benchmark]
public async Task Retry_Asynchronous_Succeeds_With_CancellationToken()
public Task Retry_Asynchronous_Succeeds_With_CancellationToken()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
return AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
}

[Benchmark]
Expand All @@ -36,15 +36,15 @@ public int Retry_Synchronous_With_Result_Succeeds()
}

[Benchmark]
public async Task<int> Retry_Asynchronous_With_Result_Succeeds()
public Task<int> Retry_Asynchronous_With_Result_Succeeds()
{
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
return AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
}

[Benchmark]
public async Task<int> Retry_Asynchronous_With_Result_Succeeds_With_CancellationToken()
public Task<int> Retry_Asynchronous_With_Result_Succeeds_With_CancellationToken()
{
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
return AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
}

[Benchmark]
Expand All @@ -62,11 +62,11 @@ public void Retry_Synchronous_Throws_Then_Succeeds()
}

[Benchmark]
public async Task Retry_Asynchronous_Throws_Then_Succeeds()
public Task Retry_Asynchronous_Throws_Then_Succeeds()
{
int count = 0;

await AsyncPolicy.ExecuteAsync(() =>
return AsyncPolicy.ExecuteAsync(() =>
{
if (count++ % 2 == 0)
{
Expand Down
20 changes: 10 additions & 10 deletions src/Polly.Benchmarks/Timeout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public void Timeout_Synchronous_Succeeds()
}

[Benchmark]
public async Task Timeout_Asynchronous_Succeeds()
public Task Timeout_Asynchronous_Succeeds()
{
await AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync());
return AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync());
}

[Benchmark]
public async Task Timeout_Asynchronous_Succeeds_With_CancellationToken()
public Task Timeout_Asynchronous_Succeeds_With_CancellationToken()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
return AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
}

[Benchmark]
Expand All @@ -36,20 +36,20 @@ public int Timeout_Synchronous_With_Result_Succeeds()
}

[Benchmark]
public async Task<int> Timeout_Asynchronous_With_Result_Succeeds()
public Task<int> Timeout_Asynchronous_With_Result_Succeeds()
{
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
return AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
}

[Benchmark]
public async Task<int> Timeout_Asynchronous_With_Result_Succeeds_With_CancellationToken()
public Task<int> Timeout_Asynchronous_With_Result_Succeeds_With_CancellationToken()
{
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
return AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
}

[Benchmark]
public async Task Timeout_Asynchronous_Times_Out_Optimistic()
public Task Timeout_Asynchronous_Times_Out_Optimistic()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionInfiniteAsync(token), CancellationToken.None);
return AsyncPolicy.ExecuteAsync((token) => Workloads.ActionInfiniteAsync(token), CancellationToken.None);
}
}
12 changes: 6 additions & 6 deletions src/Polly/Caching/AsyncSerializingCacheProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ public AsyncSerializingCacheProvider(IAsyncCacheProvider<TSerialized> wrappedCac
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="continueOnCapturedContext">Whether async calls should continue on a captured synchronization context.</param>
/// <returns>A <see cref="Task" /> which completes when the value has been cached.</returns>
public async Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken,
public Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken,
bool continueOnCapturedContext)
{
await _wrappedCacheProvider.PutAsync(
return _wrappedCacheProvider.PutAsync(
key,
_serializer.Serialize(value),
ttl,
cancellationToken,
continueOnCapturedContext
).ConfigureAwait(continueOnCapturedContext);
);
}
}

Expand Down Expand Up @@ -112,15 +112,15 @@ public AsyncSerializingCacheProvider(IAsyncCacheProvider<TSerialized> wrappedCac
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="continueOnCapturedContext">Whether async calls should continue on a captured synchronization context.</param>
/// <returns>A <see cref="Task" /> which completes when the value has been cached.</returns>
public async Task PutAsync(string key, TResult value, Ttl ttl, CancellationToken cancellationToken,
public Task PutAsync(string key, TResult value, Ttl ttl, CancellationToken cancellationToken,
bool continueOnCapturedContext)
{
await _wrappedCacheProvider.PutAsync(
return _wrappedCacheProvider.PutAsync(
key,
_serializer.Serialize(value),
ttl,
cancellationToken,
continueOnCapturedContext
).ConfigureAwait(continueOnCapturedContext);
);
}
}
4 changes: 2 additions & 2 deletions src/Polly/NoOp/AsyncNoOpPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal AsyncNoOpPolicy()
[DebuggerStepThrough]
protected override Task<TResult> ImplementationAsync<TResult>( Func<Context, CancellationToken,Task<TResult>> action, Context context, CancellationToken cancellationToken,
bool continueOnCapturedContext)
=> NoOpEngine.ImplementationAsync(action, context, cancellationToken, continueOnCapturedContext);
=> NoOpEngine.ImplementationAsync(action, context, cancellationToken);
}

/// <summary>
Expand All @@ -34,5 +34,5 @@ internal AsyncNoOpPolicy()
[DebuggerStepThrough]
protected override Task<TResult> ImplementationAsync(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken,
bool continueOnCapturedContext)
=> NoOpEngine.ImplementationAsync(action, context, cancellationToken, continueOnCapturedContext);
=> NoOpEngine.ImplementationAsync(action, context, cancellationToken);
}
4 changes: 2 additions & 2 deletions src/Polly/NoOp/NoOpEngineAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace Polly.NoOp;

internal static partial class NoOpEngine
{
internal static async Task<TResult> ImplementationAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
=> await action(context, cancellationToken).ConfigureAwait(continueOnCapturedContext);
internal static Task<TResult> ImplementationAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken)
=> action(context, cancellationToken);
}
Loading

0 comments on commit a2cc212

Please sign in to comment.