-Reduced allocations of JToken.Path

This commit is contained in:
James Newton-King
2016-03-28 19:26:27 +13:00
parent 3a412e5ab2
commit 795ef28e0a
3 changed files with 41 additions and 15 deletions
+25 -1
View File
@@ -31,6 +31,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web.Script.Serialization; using System.Web.Script.Serialization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Tests; using Newtonsoft.Json.Tests;
using Newtonsoft.Json.Tests.Converters; using Newtonsoft.Json.Tests.Converters;
@@ -52,6 +53,7 @@ namespace Newtonsoft.Json.TestConsole
//t.DeserializeLargeJson(); //t.DeserializeLargeJson();
//PerformanceTests t = new PerformanceTests(); //PerformanceTests t = new PerformanceTests();
LargeArrayJTokenPathPerformance();
//t.Iterations = 50000; //t.Iterations = 50000;
//t.BenchmarkDeserializeMethod<TestClass>(PerformanceTests.SerializeMethod.JsonNet, PerformanceTests.JsonText); //t.BenchmarkDeserializeMethod<TestClass>(PerformanceTests.SerializeMethod.JsonNet, PerformanceTests.JsonText);
@@ -62,7 +64,7 @@ namespace Newtonsoft.Json.TestConsole
//ReadLargeJson(); //ReadLargeJson();
//WriteLargeJson(); //WriteLargeJson();
DeserializeJson(); //DeserializeJson();
//ReadLargeJson(); //ReadLargeJson();
//ReadLargeJsonJavaScriptSerializer(); //ReadLargeJsonJavaScriptSerializer();
@@ -72,6 +74,28 @@ namespace Newtonsoft.Json.TestConsole
Console.ReadKey(); Console.ReadKey();
} }
public static void LargeArrayJTokenPathPerformance()
{
JArray a = new JArray();
for (int i = 0; i < 100000; i++)
{
a.Add(i);
}
JToken last = a.Last;
int interations = 1000;
Console.WriteLine("Ready!!!");
Console.ReadKey();
string p = null;
for (int i = 0; i < interations; i++)
{
p = last.Path;
}
}
public static void DeserializeJson() public static void DeserializeJson()
{ {
PerformanceTests t = new PerformanceTests(); PerformanceTests t = new PerformanceTests();
+9 -12
View File
@@ -675,9 +675,10 @@ namespace Newtonsoft.Json.Linq
internal static JToken CreateFromContent(object content) internal static JToken CreateFromContent(object content)
{ {
if (content is JToken) JToken token = content as JToken;
if (token != null)
{ {
return (JToken)content; return token;
} }
return new JValue(content); return new JValue(content);
@@ -759,7 +760,7 @@ namespace Newtonsoft.Json.Linq
do do
{ {
if (parent is JProperty && ((JProperty)parent).Value != null) if ((parent as JProperty)?.Value != null)
{ {
if (parent == this) if (parent == this)
{ {
@@ -887,12 +888,7 @@ namespace Newtonsoft.Json.Linq
PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors) PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors)
{ {
ICustomTypeDescriptor d = First as ICustomTypeDescriptor; ICustomTypeDescriptor d = First as ICustomTypeDescriptor;
if (d != null) return d?.GetProperties();
{
return d.GetProperties();
}
return null;
} }
#endif #endif
@@ -958,9 +954,10 @@ namespace Newtonsoft.Json.Linq
return null; return null;
} }
if (value is JToken) JToken token = value as JToken;
if (token != null)
{ {
return (JToken)value; return token;
} }
throw new ArgumentException("Argument is not a JToken."); throw new ArgumentException("Argument is not a JToken.");
@@ -1169,7 +1166,7 @@ namespace Newtonsoft.Json.Linq
} }
} }
#else #else
IDictionary<JToken, bool> items = new Dictionary<JToken, bool>(EqualityComparer); Dictionary<JToken, bool> items = new Dictionary<JToken, bool>(EqualityComparer);
foreach (JToken t in target) foreach (JToken t in target)
{ {
items[t] = true; items[t] = true;
+7 -2
View File
@@ -190,9 +190,14 @@ namespace Newtonsoft.Json.Linq
return string.Empty; return string.Empty;
} }
IList<JToken> ancestors = AncestorsAndSelf().Reverse().ToList(); List<JToken> ancestors = new List<JToken>();
for (JToken current = this; current != null; current = current.Parent)
{
ancestors.Add(current);
}
ancestors.Reverse();
List<JsonPosition> positions = new List<JsonPosition>(); List<JsonPosition> positions = new List<JsonPosition>(ancestors.Count);
for (int i = 0; i < ancestors.Count; i++) for (int i = 0; i < ancestors.Count; i++)
{ {
JToken current = ancestors[i]; JToken current = ancestors[i];