From 4db6579593b50487d70421b0b2d092b6b8f778e3 Mon Sep 17 00:00:00 2001 From: Jon Hanna Date: Thu, 5 Jan 2017 00:49:54 +0000 Subject: [PATCH] Optimise Extensions.Values Branch on key being null or not once, rather than once per loop. Remove up-cast of token to JToken to allow jitter to either elide JValue path if impossible or turn into null-test when inevitable. Replace is-and-cast with as-and-null-test for quicker cast in cases where cast is still done. --- Src/Newtonsoft.Json/Linq/Extensions.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Src/Newtonsoft.Json/Linq/Extensions.cs b/Src/Newtonsoft.Json/Linq/Extensions.cs index f9f8976c..66381dcd 100644 --- a/Src/Newtonsoft.Json/Linq/Extensions.cs +++ b/Src/Newtonsoft.Json/Linq/Extensions.cs @@ -184,13 +184,14 @@ namespace Newtonsoft.Json.Linq { ValidationUtils.ArgumentNotNull(source, nameof(source)); - foreach (JToken token in source) + if (key == null) { - if (key == null) + foreach (T token in source) { - if (token is JValue) + JValue value = token as JValue; + if (value != null) { - yield return Convert((JValue)token); + yield return Convert(value); } else { @@ -200,7 +201,10 @@ namespace Newtonsoft.Json.Linq } } } - else + } + else + { + foreach (T token in source) { JToken value = token[key]; if (value != null) @@ -209,8 +213,6 @@ namespace Newtonsoft.Json.Linq } } } - - yield break; } //TODO