diff --git a/Src/Newtonsoft.Json.TestConsole/Program.cs b/Src/Newtonsoft.Json.TestConsole/Program.cs index 051a5e65..9df1c254 100644 --- a/Src/Newtonsoft.Json.TestConsole/Program.cs +++ b/Src/Newtonsoft.Json.TestConsole/Program.cs @@ -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(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(); diff --git a/Src/Newtonsoft.Json/Linq/JContainer.cs b/Src/Newtonsoft.Json/Linq/JContainer.cs index 02bb0555..0ad8a5ce 100644 --- a/Src/Newtonsoft.Json/Linq/JContainer.cs +++ b/Src/Newtonsoft.Json/Linq/JContainer.cs @@ -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 items = new Dictionary(EqualityComparer); + Dictionary items = new Dictionary(EqualityComparer); foreach (JToken t in target) { items[t] = true; diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs index d6d0830c..bac89342 100644 --- a/Src/Newtonsoft.Json/Linq/JToken.cs +++ b/Src/Newtonsoft.Json/Linq/JToken.cs @@ -190,9 +190,14 @@ namespace Newtonsoft.Json.Linq return string.Empty; } - IList ancestors = AncestorsAndSelf().Reverse().ToList(); + List ancestors = new List(); + for (JToken current = this; current != null; current = current.Parent) + { + ancestors.Add(current); + } + ancestors.Reverse(); - List positions = new List(); + List positions = new List(ancestors.Count); for (int i = 0; i < ancestors.Count; i++) { JToken current = ancestors[i];