-Reduced allocations of JToken.Path++

This commit is contained in:
James Newton-King
2016-03-28 19:44:05 +13:00
parent 795ef28e0a
commit 5aa75ae595
+10 -23
View File
@@ -190,28 +190,9 @@ namespace Newtonsoft.Json.Linq
return string.Empty; return string.Empty;
} }
List<JToken> ancestors = new List<JToken>(); List<JsonPosition> positions = new List<JsonPosition>();
JToken previous = null;
for (JToken current = this; current != null; current = current.Parent) for (JToken current = this; current != null; current = current.Parent)
{
ancestors.Add(current);
}
ancestors.Reverse();
List<JsonPosition> positions = new List<JsonPosition>(ancestors.Count);
for (int i = 0; i < ancestors.Count; i++)
{
JToken current = ancestors[i];
JToken next = null;
if (i + 1 < ancestors.Count)
{
next = ancestors[i + 1];
}
else if (ancestors[i].Type == JTokenType.Property)
{
next = ancestors[i];
}
if (next != null)
{ {
switch (current.Type) switch (current.Type)
{ {
@@ -221,13 +202,19 @@ namespace Newtonsoft.Json.Linq
break; break;
case JTokenType.Array: case JTokenType.Array:
case JTokenType.Constructor: case JTokenType.Constructor:
int index = ((IList<JToken>)current).IndexOf(next); if (previous != null)
{
int index = ((IList<JToken>)current).IndexOf(previous);
positions.Add(new JsonPosition(JsonContainerType.Array) { Position = index }); positions.Add(new JsonPosition(JsonContainerType.Array) { Position = index });
}
break; break;
} }
previous = current;
} }
}
positions.Reverse();
return JsonPosition.BuildPath(positions, null); return JsonPosition.BuildPath(positions, null);
} }