Permalink
Browse files

perf: Optimize return value validation of methods returning a non-gen…

…eric Task.

A direct comparison of the type to typeof(Task) is faster than analyzing if the type is generic or not.
  • Loading branch information...
marcind committed Apr 7, 2012
1 parent 4592f29 commit 38b159cb927610f99b7b6c062e9cb07fb9fb9172
Showing with 8 additions and 4 deletions.
  1. +8 −4 src/System.Web.Http/Controllers/ReflectedHttpActionDescriptor.cs
@@ -410,11 +410,15 @@ private static void ThrowIfWrappedTaskInstance(MethodInfo method, Type type)
// Throw if a method declares a return type of Task and returns an instance of Task<Task> or Task<Task<T>>
// This most likely indicates that the developer forgot to call Unwrap() somewhere.
Contract.Assert(method.ReturnType == typeof(Task));
- Type innerTaskType = TypeHelper.GetTaskInnerTypeOrNull(type);
- if (innerTaskType != null && typeof(Task).IsAssignableFrom(innerTaskType))
+ // Fast path: check if type is exactly Task first.
+ if (type != typeof(Task))
{
- throw Error.InvalidOperation(SRResources.ActionExecutor_WrappedTaskInstance,
- method.Name, method.DeclaringType.Name, type.FullName);
+ Type innerTaskType = TypeHelper.GetTaskInnerTypeOrNull(type);
+ if (innerTaskType != null && typeof(Task).IsAssignableFrom(innerTaskType))
+ {
+ throw Error.InvalidOperation(SRResources.ActionExecutor_WrappedTaskInstance,
+ method.Name, method.DeclaringType.Name, type.FullName);
+ }
}
}
}

0 comments on commit 38b159c

Please sign in to comment.