Optimize allocations for creating ActionConstraints
This commit is contained in:
parent
1ed22e5939
commit
761c7e6751
|
|
@ -29,9 +29,9 @@ namespace Microsoft.AspNet.Mvc.ActionConstraints
|
|||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
foreach (var item in context.Results)
|
||||
for (var i = 0; i < context.Results.Count; i++)
|
||||
{
|
||||
ProvideConstraint(item, context.HttpContext.RequestServices);
|
||||
ProvideConstraint(context.Results[i], context.HttpContext.RequestServices);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ namespace Microsoft.AspNet.Mvc.ActionConstraints
|
|||
{
|
||||
public static readonly int HttpMethodConstraintOrder = 100;
|
||||
|
||||
private readonly IReadOnlyList<string> _methods;
|
||||
private readonly IReadOnlyList<string> _httpMethods;
|
||||
|
||||
private readonly string OriginHeader = "Origin";
|
||||
private readonly string AccessControlRequestMethod = "Access-Control-Request-Method";
|
||||
|
|
@ -39,14 +39,14 @@ namespace Microsoft.AspNet.Mvc.ActionConstraints
|
|||
methods.Add(method);
|
||||
}
|
||||
|
||||
_methods = new ReadOnlyCollection<string>(methods);
|
||||
_httpMethods = new ReadOnlyCollection<string>(methods);
|
||||
}
|
||||
|
||||
public IEnumerable<string> HttpMethods
|
||||
{
|
||||
get
|
||||
{
|
||||
return _methods;
|
||||
return _httpMethods;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -62,7 +62,7 @@ namespace Microsoft.AspNet.Mvc.ActionConstraints
|
|||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
if (_methods.Count == 0)
|
||||
if (_httpMethods.Count == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
|
@ -83,7 +83,16 @@ namespace Microsoft.AspNet.Mvc.ActionConstraints
|
|||
}
|
||||
}
|
||||
|
||||
return (HttpMethods.Any(m => m.Equals(method, StringComparison.Ordinal)));
|
||||
for (var i = 0; i < _httpMethods.Count; i++)
|
||||
{
|
||||
var supportedMethod = _httpMethods[i];
|
||||
if (string.Equals(supportedMethod, method, StringComparison.Ordinal))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -260,12 +260,16 @@ namespace Microsoft.AspNet.Mvc.Infrastructure
|
|||
return null;
|
||||
}
|
||||
|
||||
var items = action.ActionConstraints.Select(c => new ActionConstraintItem(c)).ToList();
|
||||
var context = new ActionConstraintProviderContext(httpContext, action, items);
|
||||
|
||||
foreach (var provider in _actionConstraintProviders)
|
||||
var items = new List<ActionConstraintItem>(action.ActionConstraints.Count);
|
||||
for (var i = 0; i < action.ActionConstraints.Count; i++)
|
||||
{
|
||||
provider.OnProvidersExecuting(context);
|
||||
items.Add(new ActionConstraintItem(action.ActionConstraints[i]));
|
||||
}
|
||||
|
||||
var context = new ActionConstraintProviderContext(httpContext, action, items);
|
||||
for (var i = 0; i < _actionConstraintProviders.Length; i++)
|
||||
{
|
||||
_actionConstraintProviders[i].OnProvidersExecuting(context);
|
||||
}
|
||||
|
||||
for (var i = _actionConstraintProviders.Length - 1; i >= 0; i--)
|
||||
|
|
@ -273,11 +277,31 @@ namespace Microsoft.AspNet.Mvc.Infrastructure
|
|||
_actionConstraintProviders[i].OnProvidersExecuted(context);
|
||||
}
|
||||
|
||||
return
|
||||
context.Results
|
||||
.Where(item => item.Constraint != null)
|
||||
.Select(item => item.Constraint)
|
||||
.ToList();
|
||||
var count = 0;
|
||||
for (var i = 0; i < context.Results.Count; i++)
|
||||
{
|
||||
if (context.Results[i].Constraint != null)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var results = new IActionConstraint[count];
|
||||
for (int i = 0, j = 0; i < context.Results.Count; i++)
|
||||
{
|
||||
var constraint = context.Results[i].Constraint;
|
||||
if (constraint != null)
|
||||
{
|
||||
results[j++] = constraint;
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue