Replace is-and-cast with as-and-nullcheck

Gives slightly better performance, so applying across the board
adds up.
This commit is contained in:
Jon Hanna
2017-01-05 01:52:04 +00:00
parent 306c3a812b
commit a95665b481
16 changed files with 136 additions and 109 deletions
@@ -268,9 +268,10 @@ namespace Newtonsoft.Json.Utilities
}
object v = currentArray[0];
if (v is IList)
IList list = v as IList;
if (list != null)
{
currentArray = (IList)v;
currentArray = list;
}
else
{
@@ -52,9 +52,10 @@ namespace Newtonsoft.Json.Utilities
{
ValidationUtils.ArgumentNotNull(list, nameof(list));
if (list is ICollection<T>)
ICollection<T> collection = list as ICollection<T>;
if (collection != null)
{
_genericCollection = (ICollection<T>)list;
_genericCollection = collection;
}
else
{
+15 -8
View File
@@ -318,10 +318,13 @@ namespace Newtonsoft.Json.Utilities
{
return (BigInteger)value;
}
if (value is string)
string s = value as string;
if (s != null)
{
return BigInteger.Parse((string)value, CultureInfo.InvariantCulture);
return BigInteger.Parse(s, CultureInfo.InvariantCulture);
}
if (value is float)
{
return new BigInteger((float)value);
@@ -350,9 +353,11 @@ namespace Newtonsoft.Json.Utilities
{
return new BigInteger((ulong)value);
}
if (value is byte[])
byte[] bytes = value as byte[];
if (bytes != null)
{
return new BigInteger((byte[])value);
return new BigInteger(bytes);
}
throw new InvalidCastException("Cannot convert {0} to BigInteger.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
@@ -487,9 +492,10 @@ namespace Newtonsoft.Json.Utilities
}
#endif
if (initialValue is byte[] && targetType == typeof(Guid))
byte[] bytes = initialValue as byte[];
if (bytes != null && targetType == typeof(Guid))
{
value = new Guid((byte[])initialValue);
value = new Guid(bytes);
return ConvertResult.Success;
}
@@ -587,9 +593,10 @@ namespace Newtonsoft.Json.Utilities
}
#endif
#if !(DOTNET || PORTABLE40 || PORTABLE)
if (initialValue is INullable)
INullable nullable = initialValue as INullable;
if (nullable != null)
{
value = EnsureTypeAssignable(ToValue((INullable)initialValue), initialType, targetType);
value = EnsureTypeAssignable(ToValue(nullable), initialType, targetType);
return ConvertResult.Success;
}
#endif
@@ -139,9 +139,9 @@ namespace Newtonsoft.Json.Utilities
callExpression = Expression.Call(readParameter, (MethodInfo)method, argsExpression);
}
if (method is MethodInfo)
MethodInfo m = method as MethodInfo;
if (m != null)
{
MethodInfo m = (MethodInfo)method;
if (m.ReturnType != typeof(void))
{
callExpression = EnsureCastExpression(callExpression, type);
@@ -762,11 +762,11 @@ namespace Newtonsoft.Json.Utilities
// http://hyperthink.net/blog/getcustomattributes-gotcha/
// ICustomAttributeProvider doesn't do inheritance
if (provider is Type)
Type t = provider as Type;
if (t != null)
{
Type t = (Type)provider;
object[] a = (attributeType != null) ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit);
Attribute[] attributes = a.Cast<Attribute>().ToArray();
object[] array = attributeType != null ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit);
Attribute[] attributes = array.Cast<Attribute>().ToArray();
#if (NET20 || NET35)
// ye olde .NET GetCustomAttributes doesn't respect the inherit argument
@@ -779,29 +779,29 @@ namespace Newtonsoft.Json.Utilities
return attributes;
}
if (provider is Assembly)
Assembly a = provider as Assembly;
if (a != null)
{
Assembly a = (Assembly)provider;
return (attributeType != null) ? Attribute.GetCustomAttributes(a, attributeType) : Attribute.GetCustomAttributes(a);
}
if (provider is MemberInfo)
MemberInfo mi = provider as MemberInfo;
if (mi != null)
{
MemberInfo m = (MemberInfo)provider;
return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit);
return (attributeType != null) ? Attribute.GetCustomAttributes(mi, attributeType, inherit) : Attribute.GetCustomAttributes(mi, inherit);
}
#if !PORTABLE40
if (provider is Module)
Module m = provider as Module;
if (m != null)
{
Module m = (Module)provider;
return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit);
}
#endif
if (provider is ParameterInfo)
ParameterInfo p = provider as ParameterInfo;
if (p != null)
{
ParameterInfo p = (ParameterInfo)provider;
return (attributeType != null) ? Attribute.GetCustomAttributes(p, attributeType, inherit) : Attribute.GetCustomAttributes(p, inherit);
}