Skip to content

Commit

Permalink
Merge pull request dotnet#19255 from sharwell/asynclazy-fast-path
Browse files Browse the repository at this point in the history
Use a wait-free fast path in AsyncLazy<T>.GetValueAsync
  • Loading branch information
sharwell authored May 5, 2017
2 parents 6f86e61 + 243ff23 commit 857df89
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Workspaces/Core/Portable/Utilities/AsyncLazy`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ public override T GetValue(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

// If the value is already available, return it immediately
if (TryGetValue(out T value))
{
return value;
}

Request request = null;
AsynchronousComputationToStart? newAsynchronousComputation = null;

Expand Down Expand Up @@ -297,7 +303,14 @@ public override Task<T> GetValueAsync(CancellationToken cancellationToken)
// Optimization: if we're already cancelled, do not pass go
if (cancellationToken.IsCancellationRequested)
{
return new Task<T>(() => default(T), cancellationToken);
return Task.FromCanceled<T>(cancellationToken);
}

// Avoid taking the lock if a cached value is available
var cachedResult = _cachedResult;
if (cachedResult != null)
{
return cachedResult;
}

Request request;
Expand Down

0 comments on commit 857df89

Please sign in to comment.