Some optimisations to async reading. (#1171)

MoveToContentAsync is often called in states where it will immediately
return true without any reading. In this case return AsyncUtils.True
avoiding allocation or async state-machine.

DoReadAsync is a very heavily hit method, and can often be fulfilled
without creating another state machine, so do this.

The remaining looping async case is the only use of ParsePostValueAsync,
so inline it into the calling method, so there is one less task
allocation.

Return value of EatWhitespaceAsync is never used, returning void reduces
the number of locals an hence the size of the state-machine. Do the same
for EatWhitespace though the impact is less.
This commit is contained in:
Jon Hanna
2017-01-13 20:41:18 +13:00
committed by James Newton-King
parent ba04246a16
commit dcae8e220d
3 changed files with 141 additions and 145 deletions
+18 -7
View File
@@ -27,7 +27,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Utilities;
@@ -210,19 +209,31 @@ namespace Newtonsoft.Json
return await ReadAsync(cancellationToken).ConfigureAwait(false) && await MoveToContentAsync(cancellationToken).ConfigureAwait(false);
}
internal async Task<bool> MoveToContentAsync(CancellationToken cancellationToken)
internal Task<bool> MoveToContentAsync(CancellationToken cancellationToken)
{
switch (TokenType)
{
case JsonToken.None:
case JsonToken.Comment:
return MoveToContentFromNonContentAsync(cancellationToken);
default:
return AsyncUtils.True;
}
}
private async Task<bool> MoveToContentFromNonContentAsync(CancellationToken cancellationToken)
{
while (true)
{
if (!await ReadAsync(cancellationToken).ConfigureAwait(false))
{
return false;
}
switch (TokenType)
{
case JsonToken.None:
case JsonToken.Comment:
if (!await ReadAsync(cancellationToken).ConfigureAwait(false))
{
return false;
}
break;
default:
return true;