Use task.IsCompletedSuccessfully rather than ReferenceEquals (#11606)

This commit is contained in:
Ben Adams 2019-07-12 21:47:32 +02:00 committed by Justin Kotalik
parent 9c69287ed0
commit 7e9de494b7
3 changed files with 9 additions and 18 deletions

View File

@ -109,9 +109,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers
{ {
var flushTask = FlushAsyncCore(); var flushTask = FlushAsyncCore();
// FlushAsyncCore will return CompletedTask if nothing sync buffered return flushTask.IsCompletedSuccessfully ?
// Fast-path and skip async state-machine if only a single async operation
return ReferenceEquals(flushTask, Task.CompletedTask) ?
_inner.WriteAsync(value) : _inner.WriteAsync(value) :
WriteAsyncAwaited(flushTask, value); WriteAsyncAwaited(flushTask, value);
} }
@ -126,9 +124,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers
{ {
var flushTask = FlushAsyncCore(); var flushTask = FlushAsyncCore();
// FlushAsyncCore will return CompletedTask if nothing sync buffered return flushTask.IsCompletedSuccessfully ?
// Fast-path and skip async state-machine if only a single async operation
return ReferenceEquals(flushTask, Task.CompletedTask) ?
_inner.WriteAsync(buffer, index, count) : _inner.WriteAsync(buffer, index, count) :
WriteAsyncAwaited(flushTask, buffer, index, count); WriteAsyncAwaited(flushTask, buffer, index, count);
} }
@ -143,9 +139,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers
{ {
var flushTask = FlushAsyncCore(); var flushTask = FlushAsyncCore();
// FlushAsyncCore will return CompletedTask if nothing sync buffered return flushTask.IsCompletedSuccessfully ?
// Fast-path and skip async state-machine if only a single async operation
return ReferenceEquals(flushTask, Task.CompletedTask) ?
_inner.WriteAsync(value) : _inner.WriteAsync(value) :
WriteAsyncAwaited(flushTask, value); WriteAsyncAwaited(flushTask, value);
} }

View File

@ -767,7 +767,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
while (onStarting.TryPop(out var entry)) while (onStarting.TryPop(out var entry))
{ {
var task = entry.Key.Invoke(entry.Value); var task = entry.Key.Invoke(entry.Value);
if (!ReferenceEquals(task, Task.CompletedTask)) if (!task.IsCompletedSuccessfully)
{ {
return FireOnStartingAwaited(task, onStarting); return FireOnStartingAwaited(task, onStarting);
} }
@ -817,7 +817,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
try try
{ {
var task = entry.Key.Invoke(entry.Value); var task = entry.Key.Invoke(entry.Value);
if (!ReferenceEquals(task, Task.CompletedTask)) if (!task.IsCompletedSuccessfully)
{ {
return FireOnCompletedAwaited(task, onCompleted); return FireOnCompletedAwaited(task, onCompleted);
} }
@ -953,8 +953,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
public Task InitializeResponseAsync(int firstWriteByteCount) public Task InitializeResponseAsync(int firstWriteByteCount)
{ {
var startingTask = FireOnStarting(); var startingTask = FireOnStarting();
// If return is Task.CompletedTask no awaiting is required if (!startingTask.IsCompletedSuccessfully)
if (!ReferenceEquals(startingTask, Task.CompletedTask))
{ {
return InitializeResponseAwaited(startingTask, firstWriteByteCount); return InitializeResponseAwaited(startingTask, firstWriteByteCount);
} }
@ -1397,8 +1396,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
if (!HasResponseStarted) if (!HasResponseStarted)
{ {
var initializeTask = InitializeResponseAsync(0); var initializeTask = InitializeResponseAsync(0);
// If return is Task.CompletedTask no awaiting is required if (!initializeTask.IsCompletedSuccessfully)
if (!ReferenceEquals(initializeTask, Task.CompletedTask))
{ {
return FlushAsyncAwaited(initializeTask, cancellationToken); return FlushAsyncAwaited(initializeTask, cancellationToken);
} }
@ -1509,8 +1507,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
Debug.Assert(!HasResponseStarted); Debug.Assert(!HasResponseStarted);
var startingTask = FireOnStarting(); var startingTask = FireOnStarting();
if (!startingTask.IsCompletedSuccessfully)
if (!ReferenceEquals(startingTask, Task.CompletedTask))
{ {
return FirstWriteAsyncAwaited(startingTask, data, cancellationToken); return FirstWriteAsyncAwaited(startingTask, data, cancellationToken);
} }

View File

@ -107,7 +107,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure
try try
{ {
var task = entry.Key.Invoke(entry.Value); var task = entry.Key.Invoke(entry.Value);
if (!ReferenceEquals(task, Task.CompletedTask)) if (!task.IsCompletedSuccessfully)
{ {
return CompleteAsyncAwaited(task, onCompleted); return CompleteAsyncAwaited(task, onCompleted);
} }