-Reduced allocations of JToken.Path
This commit is contained in:
@@ -31,6 +31,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Script.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Tests;
|
||||
using Newtonsoft.Json.Tests.Converters;
|
||||
|
||||
@@ -52,6 +53,7 @@ namespace Newtonsoft.Json.TestConsole
|
||||
//t.DeserializeLargeJson();
|
||||
|
||||
//PerformanceTests t = new PerformanceTests();
|
||||
LargeArrayJTokenPathPerformance();
|
||||
//t.Iterations = 50000;
|
||||
//t.BenchmarkDeserializeMethod<TestClass>(PerformanceTests.SerializeMethod.JsonNet, PerformanceTests.JsonText);
|
||||
|
||||
@@ -62,7 +64,7 @@ namespace Newtonsoft.Json.TestConsole
|
||||
|
||||
//ReadLargeJson();
|
||||
//WriteLargeJson();
|
||||
DeserializeJson();
|
||||
//DeserializeJson();
|
||||
//ReadLargeJson();
|
||||
//ReadLargeJsonJavaScriptSerializer();
|
||||
|
||||
@@ -72,6 +74,28 @@ namespace Newtonsoft.Json.TestConsole
|
||||
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()
|
||||
{
|
||||
PerformanceTests t = new PerformanceTests();
|
||||
|
||||
@@ -675,9 +675,10 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
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);
|
||||
@@ -759,7 +760,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
do
|
||||
{
|
||||
if (parent is JProperty && ((JProperty)parent).Value != null)
|
||||
if ((parent as JProperty)?.Value != null)
|
||||
{
|
||||
if (parent == this)
|
||||
{
|
||||
@@ -887,12 +888,7 @@ namespace Newtonsoft.Json.Linq
|
||||
PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors)
|
||||
{
|
||||
ICustomTypeDescriptor d = First as ICustomTypeDescriptor;
|
||||
if (d != null)
|
||||
{
|
||||
return d.GetProperties();
|
||||
}
|
||||
|
||||
return null;
|
||||
return d?.GetProperties();
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -958,9 +954,10 @@ namespace Newtonsoft.Json.Linq
|
||||
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.");
|
||||
@@ -1169,7 +1166,7 @@ namespace Newtonsoft.Json.Linq
|
||||
}
|
||||
}
|
||||
#else
|
||||
IDictionary<JToken, bool> items = new Dictionary<JToken, bool>(EqualityComparer);
|
||||
Dictionary<JToken, bool> items = new Dictionary<JToken, bool>(EqualityComparer);
|
||||
foreach (JToken t in target)
|
||||
{
|
||||
items[t] = true;
|
||||
|
||||
@@ -190,9 +190,14 @@ namespace Newtonsoft.Json.Linq
|
||||
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++)
|
||||
{
|
||||
JToken current = ancestors[i];
|
||||
|
||||
Reference in New Issue
Block a user