Remove unnecessary awaits to speed up cold startup time.

The await/async patterns adds a state machine that takes time to jit, in this
case the await is not necessary and the code remains simple without adding
continuations.
This commit is contained in:
Yishai Galatzer 2014-06-25 11:53:44 -07:00
parent 177aadeadf
commit 039762c497
1 changed files with 5 additions and 4 deletions

View File

@ -60,7 +60,8 @@ namespace Microsoft.AspNet.Mvc
// This is necessary to enable calling await on the returned task. // This is necessary to enable calling await on the returned task.
// i.e we need to write the following var result = await (Task<ActualType>)mInfo.Invoke. // i.e we need to write the following var result = await (Task<ActualType>)mInfo.Invoke.
// Returning Task<object> enables us to await on the result. // Returning Task<object> enables us to await on the result.
private static async Task<object> CoerceResultToTaskAsync( // This method is intentionally not using async pattern to keep jit time (on cold start) to a minimum.
private static Task<object> CoerceResultToTaskAsync(
object result, object result,
Type returnType, Type returnType,
string methodName, string methodName,
@ -74,7 +75,7 @@ namespace Microsoft.AspNet.Mvc
if (returnType == typeof(Task)) if (returnType == typeof(Task))
{ {
ThrowIfWrappedTaskInstance(resultAsTask.GetType(), methodName, declaringType); ThrowIfWrappedTaskInstance(resultAsTask.GetType(), methodName, declaringType);
return await CastToObject(resultAsTask); return CastToObject(resultAsTask);
} }
var taskValueType = TypeHelper.GetTaskInnerTypeOrNull(returnType); var taskValueType = TypeHelper.GetTaskInnerTypeOrNull(returnType);
@ -84,7 +85,7 @@ namespace Microsoft.AspNet.Mvc
// constructs: return (Task<object>)Convert<T>((Task<T>)result) // constructs: return (Task<object>)Convert<T>((Task<T>)result)
var genericMethodInfo = _convertOfTMethod.MakeGenericMethod(taskValueType); var genericMethodInfo = _convertOfTMethod.MakeGenericMethod(taskValueType);
var convertedResult = (Task<object>)genericMethodInfo.Invoke(null, new object[] { result }); var convertedResult = (Task<object>)genericMethodInfo.Invoke(null, new object[] { result });
return await convertedResult; return convertedResult;
} }
// This will be the case for: // This will be the case for:
@ -96,7 +97,7 @@ namespace Microsoft.AspNet.Mvc
} }
else else
{ {
return result; return Task.FromResult<object>(result);
} }
} }