Skip to content

Commit

Permalink
Merge pull request dotnet#23553 from sharwell/prefer-trydequeue
Browse files Browse the repository at this point in the history
Use TryDequeue before falling back to DequeueAsync
  • Loading branch information
sharwell committed Dec 6, 2017
2 parents 721f6ea + c9c4288 commit 6c5c45b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/Compilers/Core/Portable/DiagnosticAnalyzer/AsyncQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Diagnostics
{
Expand Down Expand Up @@ -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);
Expand All @@ -227,6 +229,7 @@ public Task WhenCompletedTask
///
/// 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)
Expand All @@ -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)
Expand Down

0 comments on commit 6c5c45b

Please sign in to comment.