提交 857df892 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #19255 from sharwell/asynclazy-fast-path

Use a wait-free fast path in AsyncLazy<T>.GetValueAsync
......@@ -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;
......@@ -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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册