Speed up expression delegates when widening types is not needed
This commit is contained in:
@@ -105,18 +105,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
|
|
||||||
Expression paramAccessorExpression = Expression.ArrayIndex(argsParameterExpression, indexExpression);
|
Expression paramAccessorExpression = Expression.ArrayIndex(argsParameterExpression, indexExpression);
|
||||||
|
|
||||||
Expression argExpression;
|
Expression argExpression = EnsureCastExpression(paramAccessorExpression, parameterType, !isByRef);
|
||||||
|
|
||||||
if (parameterType.IsValueType())
|
|
||||||
{
|
|
||||||
BinaryExpression ensureValueTypeNotNull = Expression.Coalesce(paramAccessorExpression, Expression.New(parameterType));
|
|
||||||
|
|
||||||
argExpression = EnsureCastExpression(ensureValueTypeNotNull, parameterType);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
argExpression = EnsureCastExpression(paramAccessorExpression, parameterType);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isByRef)
|
if (isByRef)
|
||||||
{
|
{
|
||||||
@@ -350,7 +339,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
return compiled;
|
return compiled;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Expression EnsureCastExpression(Expression expression, Type targetType)
|
private Expression EnsureCastExpression(Expression expression, Type targetType, bool allowWidening = false)
|
||||||
{
|
{
|
||||||
Type expressionType = expression.Type;
|
Type expressionType = expression.Type;
|
||||||
|
|
||||||
@@ -360,15 +349,28 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
return expression;
|
return expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (targetType.IsPrimitive())
|
if (targetType.IsValueType())
|
||||||
{
|
{
|
||||||
MethodInfo toTargetTypeMethod = typeof(Convert)
|
Expression convert = Expression.Unbox(expression, targetType);
|
||||||
.GetMethod("To" + targetType.Name, new[] { typeof(object) });
|
|
||||||
|
|
||||||
if (toTargetTypeMethod != null)
|
if (allowWidening && targetType.IsPrimitive())
|
||||||
{
|
{
|
||||||
expression = Expression.Call(toTargetTypeMethod, expression);
|
MethodInfo toTargetTypeMethod = typeof(Convert)
|
||||||
|
.GetMethod("To" + targetType.Name, new[] { typeof(object) });
|
||||||
|
|
||||||
|
if (toTargetTypeMethod != null)
|
||||||
|
{
|
||||||
|
convert = Expression.Condition(
|
||||||
|
Expression.TypeIs(expression, targetType),
|
||||||
|
convert,
|
||||||
|
Expression.Call(toTargetTypeMethod, expression));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Expression.Condition(
|
||||||
|
Expression.Equal(expression, Expression.Constant(null, typeof(object))),
|
||||||
|
Expression.Default(targetType),
|
||||||
|
convert);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Expression.Convert(expression, targetType);
|
return Expression.Convert(expression, targetType);
|
||||||
|
|||||||
Reference in New Issue
Block a user