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:
committed by
James Newton-King
parent
ba04246a16
commit
dcae8e220d
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user