From 1ed22e59390505f9e145371cdd0ac31e5692ac3e Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Sat, 3 Oct 2015 23:48:14 -0700 Subject: [PATCH] Avoid state machine and allocation for 0-parameters --- .../DefaultControllerActionArgumentBinder.cs | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/Controllers/DefaultControllerActionArgumentBinder.cs b/src/Microsoft.AspNet.Mvc.Core/Controllers/DefaultControllerActionArgumentBinder.cs index 7b7072c72d..5e3ed7a8ee 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Controllers/DefaultControllerActionArgumentBinder.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Controllers/DefaultControllerActionArgumentBinder.cs @@ -35,7 +35,7 @@ namespace Microsoft.AspNet.Mvc.Controllers _validator = validator; } - public async Task> BindActionArgumentsAsync( + public Task> BindActionArgumentsAsync( ActionContext actionContext, ActionBindingContext actionBindingContext, object controller) @@ -64,6 +64,29 @@ namespace Microsoft.AspNet.Mvc.Controllers nameof(actionContext)); } + // Perf: Avoid allocating async state machines when we know there's nothing to bind. + if (actionDescriptor.BoundProperties.Count == 0 && + actionDescriptor.Parameters.Count == 0) + { + return Task.FromResult>( + new Dictionary(StringComparer.Ordinal)); + } + else + { + return BindActionArgumentsCoreAsync( + actionContext, + actionBindingContext, + controller, + actionDescriptor); + } + } + + private async Task> BindActionArgumentsCoreAsync( + ActionContext actionContext, + ActionBindingContext actionBindingContext, + object controller, + ControllerActionDescriptor actionDescriptor) + { var operationBindingContext = GetOperationBindingContext(actionContext, actionBindingContext); var controllerProperties = new Dictionary(StringComparer.Ordinal); await PopulateArgumentsAsync(