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));
|
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;
|
public static readonly int HttpMethodConstraintOrder = 100;
|
||||||
|
|
||||||
private readonly IReadOnlyList<string> _methods;
|
private readonly IReadOnlyList<string> _httpMethods;
|
||||||
|
|
||||||
private readonly string OriginHeader = "Origin";
|
private readonly string OriginHeader = "Origin";
|
||||||
private readonly string AccessControlRequestMethod = "Access-Control-Request-Method";
|
private readonly string AccessControlRequestMethod = "Access-Control-Request-Method";
|
||||||
|
|
@ -39,14 +39,14 @@ namespace Microsoft.AspNet.Mvc.ActionConstraints
|
||||||
methods.Add(method);
|
methods.Add(method);
|
||||||
}
|
}
|
||||||
|
|
||||||
_methods = new ReadOnlyCollection<string>(methods);
|
_httpMethods = new ReadOnlyCollection<string>(methods);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<string> HttpMethods
|
public IEnumerable<string> HttpMethods
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return _methods;
|
return _httpMethods;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -62,7 +62,7 @@ namespace Microsoft.AspNet.Mvc.ActionConstraints
|
||||||
throw new ArgumentNullException(nameof(context));
|
throw new ArgumentNullException(nameof(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_methods.Count == 0)
|
if (_httpMethods.Count == 0)
|
||||||
{
|
{
|
||||||
return true;
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var items = action.ActionConstraints.Select(c => new ActionConstraintItem(c)).ToList();
|
var items = new List<ActionConstraintItem>(action.ActionConstraints.Count);
|
||||||
var context = new ActionConstraintProviderContext(httpContext, action, items);
|
for (var i = 0; i < action.ActionConstraints.Count; i++)
|
||||||
|
|
||||||
foreach (var provider in _actionConstraintProviders)
|
|
||||||
{
|
{
|
||||||
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--)
|
for (var i = _actionConstraintProviders.Length - 1; i >= 0; i--)
|
||||||
|
|
@ -273,11 +277,31 @@ namespace Microsoft.AspNet.Mvc.Infrastructure
|
||||||
_actionConstraintProviders[i].OnProvidersExecuted(context);
|
_actionConstraintProviders[i].OnProvidersExecuted(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
var count = 0;
|
||||||
context.Results
|
for (var i = 0; i < context.Results.Count; i++)
|
||||||
.Where(item => item.Constraint != null)
|
{
|
||||||
.Select(item => item.Constraint)
|
if (context.Results[i].Constraint != null)
|
||||||
.ToList();
|
{
|
||||||
|
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