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.
This commit is contained in:
Jon Hanna
2017-01-05 00:49:54 +00:00
parent 306c3a812b
commit 4db6579593
+9 -7
View File
@@ -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, U>((JValue)token);
yield return Convert<JValue, U>(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