未验证 提交 6c5c45b1 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #23553 from sharwell/prefer-trydequeue

Use TryDequeue before falling back to DequeueAsync
......@@ -863,13 +863,16 @@ private async Task<CompilationCompletedEvent> ProcessCompilationEventsCoreAsync(
CompilationEvent e;
try
{
if (!prePopulatedEventQueue)
if (!CompilationEventQueue.TryDequeue(out e))
{
e = await CompilationEventQueue.DequeueAsync(cancellationToken).ConfigureAwait(false);
}
else if (!CompilationEventQueue.TryDequeue(out e))
{
return completedEvent;
if (!prePopulatedEventQueue)
{
e = await CompilationEventQueue.DequeueAsync(cancellationToken).ConfigureAwait(false);
}
else
{
return completedEvent;
}
}
}
catch (TaskCanceledException) when (!prePopulatedEventQueue)
......
......@@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Diagnostics
{
......@@ -218,6 +219,7 @@ public Task WhenCompletedTask
/// is empty, the returned task waits for an element to be enqueued. If <see cref="Complete"/>
/// is called before an element becomes available, the returned task is cancelled.
/// </summary>
[PerformanceSensitive("https://github.com/dotnet/roslyn/issues/23582", OftenCompletesSynchronously = true)]
public Task<TElement> DequeueAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return WithCancellation(DequeueAsyncCore(), cancellationToken);
......@@ -227,6 +229,7 @@ public Task<TElement> DequeueAsync(CancellationToken cancellationToken = default
///
/// Note: The early cancellation behavior is intentional.
/// </summary>
[PerformanceSensitive("https://github.com/dotnet/roslyn/issues/23582", OftenCompletesSynchronously = true)]
private static Task<T> WithCancellation<T>(Task<T> task, CancellationToken cancellationToken)
{
if (task.IsCompleted || !cancellationToken.CanBeCanceled)
......@@ -242,6 +245,7 @@ private static Task<T> WithCancellation<T>(Task<T> task, CancellationToken cance
return task.ContinueWith(t => t, cancellationToken, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default).Unwrap();
}
[PerformanceSensitive("https://github.com/dotnet/roslyn/issues/23582", OftenCompletesSynchronously = true)]
private Task<TElement> DequeueAsyncCore()
{
lock (SyncObject)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册