Reduce enumeration of rightResults in BooleanQueryExpression.IsMatch (#1155)
If Operator is Exists or leftResults is empty don't obtain rightResults, as not relevant. Otherwise if rightResults is not an ICollection<JToken> load it into an List so it won't be recomputed on each enumeration of leftResults.
This commit is contained in:
committed by
James Newton-King
parent
ec2659e3ad
commit
cad2ec3a09
@@ -2,6 +2,11 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
#if NET20
|
||||||
|
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||||
|
#else
|
||||||
|
using System.Linq;
|
||||||
|
#endif
|
||||||
using Newtonsoft.Json.Utilities;
|
using Newtonsoft.Json.Utilities;
|
||||||
|
|
||||||
namespace Newtonsoft.Json.Linq.JsonPath
|
namespace Newtonsoft.Json.Linq.JsonPath
|
||||||
@@ -88,24 +93,29 @@ namespace Newtonsoft.Json.Linq.JsonPath
|
|||||||
|
|
||||||
public override bool IsMatch(JToken root, JToken t)
|
public override bool IsMatch(JToken root, JToken t)
|
||||||
{
|
{
|
||||||
IEnumerable<JToken> rightResults = GetResult(root, t, Right);
|
if (Operator == QueryOperator.Exists)
|
||||||
IEnumerable<JToken> leftResults = GetResult(root, t, Left);
|
|
||||||
|
|
||||||
foreach (JToken leftResult in leftResults)
|
|
||||||
{
|
{
|
||||||
if (Operator == QueryOperator.Exists)
|
return GetResult(root, t, Left).Any();
|
||||||
|
}
|
||||||
|
|
||||||
|
using (IEnumerator<JToken> leftResults = GetResult(root, t, Left).GetEnumerator())
|
||||||
|
{
|
||||||
|
if (leftResults.MoveNext())
|
||||||
{
|
{
|
||||||
return true;
|
IEnumerable<JToken> rightResultsEn = GetResult(root, t, Right);
|
||||||
}
|
ICollection<JToken> rightResults = rightResultsEn as ICollection<JToken> ?? rightResultsEn.ToList();
|
||||||
else
|
|
||||||
{
|
do
|
||||||
foreach (JToken rightResult in rightResults)
|
|
||||||
{
|
{
|
||||||
if (MatchTokens(leftResult, rightResult))
|
JToken leftResult = leftResults.Current;
|
||||||
|
foreach (JToken rightResult in rightResults)
|
||||||
{
|
{
|
||||||
return true;
|
if (MatchTokens(leftResult, rightResult))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
} while (leftResults.MoveNext());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user