Don't explicitly check for Failed or Cancelled Statuses (#1179)
Between discussion at #1137 and a few rounds of revision at #1159 we arrived at an optimisation where tasks that are both heavily hit and very likely to return in a completed state are checked for a status of either RanToCompletion, Canceled or Faulted and a path is taken where no await is needed for any of those. The Canceled and Faulted tasks are returned immediately, allowing that state to be passed to the caller. However, these are exceptional cases, and if we just continued the subsequent await would raise the appropriate exception. Checking for all three cases instead of just RanToCompletion is optimising for those execptional conditions at cost (in the form of a larger comparison segment of code) to the more common cases of the task either being completed or still running. Simplify this back to a check just for RanToCompletion.
This commit is contained in:
committed by
James Newton-King
parent
3b583b886f
commit
0ef9ec292b
@@ -381,29 +381,17 @@ namespace Newtonsoft.Json.Utilities
|
||||
bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, JsonTextWriter client, char[] writeBuffer, CancellationToken cancellationToken)
|
||||
{
|
||||
Task task = writer.WriteAsync(delimiter, cancellationToken);
|
||||
switch (task.Status)
|
||||
if (task.Status != TaskStatus.RanToCompletion)
|
||||
{
|
||||
case TaskStatus.Canceled:
|
||||
case TaskStatus.Faulted:
|
||||
return task;
|
||||
case TaskStatus.RanToCompletion:
|
||||
// Completed synchronously (or very fast!), continue within this rather
|
||||
// that the default branch which uses async
|
||||
break;
|
||||
default:
|
||||
return WriteEscapedJavaScriptStringWithDelimitersAsync(task, writer, s, delimiter, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken);
|
||||
return WriteEscapedJavaScriptStringWithDelimitersAsync(task, writer, s, delimiter, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(s))
|
||||
{
|
||||
task = WriteEscapedJavaScriptStringWithoutDelimitersAsync(writer, s, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken);
|
||||
switch (task.Status)
|
||||
if (task.Status == TaskStatus.RanToCompletion)
|
||||
{
|
||||
case TaskStatus.Canceled:
|
||||
case TaskStatus.Faulted:
|
||||
return task;
|
||||
case TaskStatus.RanToCompletion:
|
||||
return writer.WriteAsync(delimiter, cancellationToken);
|
||||
return writer.WriteAsync(delimiter, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user