From 2fbe1eaa80def36331c6d3b83f4eab34684d27ef Mon Sep 17 00:00:00 2001 From: AMooreMicrosoft Date: Tue, 30 Jul 2013 10:06:47 -0700 Subject: [PATCH] WA34: WebAPI: ReflectedHttpActionDescriptor: Cache repeated calls to MethodInfo.GetParameters. In WebAPI scenarios involving paramters, there are repeated calls to extract the same set of paramters on the long lifetime ReflectedHttpActionDescriptor. This changes caches them and will prevent one array allocation per request and should improve throughput by 0.3-0.5% depending on the scenario. --- .../ReflectedHttpActionDescriptor.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/System.Web.Http/Controllers/ReflectedHttpActionDescriptor.cs b/src/System.Web.Http/Controllers/ReflectedHttpActionDescriptor.cs index a4617ebd..cb4bdf05 100644 --- a/src/System.Web.Http/Controllers/ReflectedHttpActionDescriptor.cs +++ b/src/System.Web.Http/Controllers/ReflectedHttpActionDescriptor.cs @@ -26,6 +26,7 @@ namespace System.Web.Http.Controllers private static readonly object[] _empty = new object[0]; private readonly Lazy> _parameters; + private ParameterInfo[] _parameterInfos; private Lazy _actionExecutor; private MethodInfo _methodInfo; @@ -98,6 +99,18 @@ namespace System.Web.Http.Controllers } } + private ParameterInfo[] ParameterInfos + { + get + { + if (_parameterInfos == null) + { + _parameterInfos = _methodInfo.GetParameters(); + } + return _parameterInfos; + } + } + /// public override Type ReturnType { @@ -154,6 +167,7 @@ namespace System.Web.Http.Controllers private void InitializeProperties(MethodInfo methodInfo) { _methodInfo = methodInfo; + _parameterInfos = null; _returnType = GetReturnType(methodInfo); _actionExecutor = new Lazy(() => InitializeActionExecutor(_methodInfo)); _declaredOnlyAttributeCache = _methodInfo.GetCustomAttributes(inherit: false); @@ -180,7 +194,7 @@ namespace System.Web.Http.Controllers { Contract.Assert(_methodInfo != null); - List parameterInfos = _methodInfo.GetParameters().Select( + List parameterInfos = ParameterInfos.Select( (item) => new ReflectedHttpParameterDescriptor(this, item)).ToList(); return new Collection(parameterInfos); } @@ -193,7 +207,7 @@ namespace System.Web.Http.Controllers return _empty; } - ParameterInfo[] parameterInfos = MethodInfo.GetParameters(); + ParameterInfo[] parameterInfos = ParameterInfos; int parameterCount = parameterInfos.Length; object[] parameterValues = new object[parameterCount]; for (int parameterIndex = 0; parameterIndex < parameterCount; parameterIndex++)