Skip to content

Commit a05b3d1

Browse files
committed
Optimizes AnyContext extension methods
Adds aggressive inlining to AnyContext extension methods to improve performance. Also, swallows the OperationCanceledException in the AnyContext extension method for AwaitableDisposable to allow for graceful cancellation.
1 parent a2a1580 commit a05b3d1

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/Foundatio/Extensions/TaskExtensions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Runtime.CompilerServices;
@@ -11,42 +11,49 @@ namespace Foundatio.Utility;
1111
internal static class TaskExtensions
1212
{
1313
[DebuggerStepThrough]
14+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1415
public static ConfiguredTaskAwaitable<TResult> AnyContext<TResult>(this Task<TResult> task)
1516
{
1617
return task.ConfigureAwait(continueOnCapturedContext: false);
1718
}
1819

1920
[DebuggerStepThrough]
21+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2022
public static ConfiguredValueTaskAwaitable<TResult> AnyContext<TResult>(this ValueTask<TResult> task)
2123
{
2224
return task.ConfigureAwait(continueOnCapturedContext: false);
2325
}
2426

2527
[DebuggerStepThrough]
28+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2629
public static ConfiguredCancelableAsyncEnumerable<TResult> AnyContext<TResult>(this IAsyncEnumerable<TResult> source)
2730
{
2831
return source.ConfigureAwait(continueOnCapturedContext: false);
2932
}
3033

3134
[DebuggerStepThrough]
35+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3236
public static ConfiguredAsyncDisposable AnyContext(this IAsyncDisposable source)
3337
{
3438
return source.ConfigureAwait(continueOnCapturedContext: false);
3539
}
3640

3741
[DebuggerStepThrough]
42+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3843
public static ConfiguredTaskAwaitable AnyContext(this Task task)
3944
{
4045
return task.ConfigureAwait(continueOnCapturedContext: false);
4146
}
4247

4348
[DebuggerStepThrough]
49+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4450
public static ConfiguredValueTaskAwaitable AnyContext(this ValueTask task)
4551
{
4652
return task.ConfigureAwait(continueOnCapturedContext: false);
4753
}
4854

4955
[DebuggerStepThrough]
56+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5057
public static ConfiguredTaskAwaitable<TResult> AnyContext<TResult>(this AwaitableDisposable<TResult> task) where TResult : IDisposable
5158
{
5259
return task.ConfigureAwait(continueOnCapturedContext: false);
@@ -60,6 +67,7 @@ public static async Task SafeDelay(this TimeProvider timeProvider, TimeSpan dela
6067
}
6168
catch (OperationCanceledException)
6269
{
70+
// Swallow the exception to allow for graceful cancellation.
6371
}
6472
}
6573
}

0 commit comments

Comments
 (0)