Skip to content

Commit

Permalink
Clean up some MVC code (#31511)
Browse files Browse the repository at this point in the history
* Clean up some MVC code

* PR feedback
  • Loading branch information
pranavkm authored Apr 3, 2021
1 parent 8f63065 commit d32b018
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace Microsoft.AspNetCore.Mvc.Controllers
Expand Down Expand Up @@ -55,6 +57,18 @@ internal static class ControllerBinderDelegateProvider
return null;
}

var parameters = actionDescriptor.Parameters switch
{
List<ParameterDescriptor> list => list.ToArray(),
_ => actionDescriptor.Parameters.ToArray()
};

var properties = actionDescriptor.BoundProperties switch
{
List<ParameterDescriptor> list => list.ToArray(),
_ => actionDescriptor.BoundProperties.ToArray()
};

return Bind;

async Task Bind(ControllerContext controllerContext, object controller, Dictionary<string, object?> arguments)
Expand All @@ -67,9 +81,7 @@ async Task Bind(ControllerContext controllerContext, object controller, Dictiona

Debug.Assert(valueProvider is not null);

var parameters = actionDescriptor.Parameters;

for (var i = 0; i < parameters.Count; i++)
for (var i = 0; i < parameters.Length; i++)
{
var parameter = parameters[i];
var bindingInfo = parameterBindingInfo![i];
Expand All @@ -95,8 +107,7 @@ async Task Bind(ControllerContext controllerContext, object controller, Dictiona
}
}

var properties = actionDescriptor.BoundProperties;
for (var i = 0; i < properties.Count; i++)
for (var i = 0; i < properties.Length; i++)
{
var property = properties[i];
var bindingInfo = propertyBindingInfo![i];
Expand Down
10 changes: 1 addition & 9 deletions src/Mvc/Mvc.Razor/src/TagHelpers/UrlResolutionTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,7 @@ private static string CreateTrimmedString(string input, int start)

private static bool IsCharWhitespace(char ch)
{
for (var i = 0; i < ValidAttributeWhitespaceChars.Length; i++)
{
if (ValidAttributeWhitespaceChars[i] == ch)
{
return true;
}
}
// the character is not white space
return false;
return ValidAttributeWhitespaceChars.AsSpan().IndexOf(ch) != -1;
}

private class EncodeFirstSegmentContent : IHtmlContent
Expand Down
9 changes: 3 additions & 6 deletions src/Mvc/Mvc.TagHelpers/src/AttributeMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace Microsoft.AspNetCore.Mvc.TagHelpers
Expand All @@ -24,7 +23,7 @@ internal static class AttributeMatcher
/// <returns><c>true</c> if a mode was determined, otherwise <c>false</c>.</returns>
public static bool TryDetermineMode<TMode>(
TagHelperContext context,
IReadOnlyList<ModeAttributes<TMode>> modeInfos,
ModeAttributes<TMode>[] modeInfos,
Func<TMode, TMode, int> compare,
out TMode result)
{
Expand All @@ -47,16 +46,14 @@ public static bool TryDetermineMode<TMode>(
result = default;

// Perf: Avoid allocating enumerator
var modeInfosCount = modeInfos.Count;
var allAttributes = context.AllAttributes;
// Read interface .Count once rather than per iteration
var allAttributesCount = allAttributes.Count;
for (var i = 0; i < modeInfosCount; i++)
foreach (var modeInfo in modeInfos)
{
var modeInfo = modeInfos[i];
var requiredAttributes = modeInfo.Attributes;
// If there are fewer attributes present than required, one or more of them must be missing.
if (allAttributesCount >= requiredAttributes.Length &&
if (allAttributesCount >= requiredAttributes.Length &&
!HasMissingAttributes(allAttributes, requiredAttributes) &&
compare(result, modeInfo.Mode) <= 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Mvc/Mvc.ViewFeatures/src/DefaultDisplayTemplates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper)
var viewBufferScope = serviceProvider.GetRequiredService<IViewBufferScope>();

var content = new HtmlContentBuilder(modelExplorer.Metadata.Properties.Count);
foreach (var propertyExplorer in modelExplorer.Properties)
foreach (var propertyExplorer in modelExplorer.PropertiesInternal)
{
var propertyMetadata = propertyExplorer.Metadata;
if (!ShouldShow(propertyExplorer, templateInfo))
Expand Down
19 changes: 9 additions & 10 deletions src/Mvc/Mvc.ViewFeatures/src/DefaultEditorTemplates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper)
var viewBufferScope = serviceProvider.GetRequiredService<IViewBufferScope>();

var content = new HtmlContentBuilder(modelExplorer.Metadata.Properties.Count);
foreach (var propertyExplorer in modelExplorer.Properties)
foreach (var propertyExplorer in modelExplorer.PropertiesInternal)
{
var propertyMetadata = propertyExplorer.Metadata;
if (!ShouldShow(propertyExplorer, templateInfo))
Expand Down Expand Up @@ -476,18 +476,17 @@ public override void Write(char value)

public override void Write(char[] buffer, int index, int count)
{
if (count > 0)
{
HasContent = true;
}
HasContent |= count > 0;
}

public override void Write(ReadOnlySpan<char> buffer)
{
HasContent |= buffer.IsEmpty;
}

public override void Write(string value)
{
if (!string.IsNullOrEmpty(value))
{
HasContent = true;
}
HasContent |= !string.IsNullOrEmpty(value);
}
}

Expand Down Expand Up @@ -540,7 +539,7 @@ public override void Encode(TextWriter output, string value, int startIndex, int
return;
}

output.Write(value.Substring(startIndex, characterCount));
output.Write(value.AsSpan(startIndex, characterCount));
}

public override unsafe int FindFirstCharacterToEncode(char* text, int textLength)
Expand Down
4 changes: 2 additions & 2 deletions src/Mvc/Mvc.ViewFeatures/src/ModelExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public Type ModelType
/// </remarks>
public IEnumerable<ModelExplorer> Properties => PropertiesInternal;

private ModelExplorer[] PropertiesInternal
internal ModelExplorer[] PropertiesInternal
{
get
{
Expand Down Expand Up @@ -472,4 +472,4 @@ private ModelExplorer CreateExplorerForProperty(
return new ModelExplorer(_metadataProvider, this, propertyMetadata, modelAccessor);
}
}
}
}
8 changes: 4 additions & 4 deletions src/Mvc/Mvc.ViewFeatures/src/ModelExplorerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;

namespace Microsoft.AspNetCore.Mvc.ViewFeatures
{
Expand Down Expand Up @@ -68,12 +67,13 @@ public static string GetSimpleDisplayText(this ModelExplorer modelExplorer)
return stringResult;
}

var firstProperty = modelExplorer.Properties.FirstOrDefault();
if (firstProperty == null)
if (modelExplorer.PropertiesInternal.Length == 0)
{
return string.Empty;
}

var firstProperty = modelExplorer.PropertiesInternal[0];

if (firstProperty.Model == null)
{
return firstProperty.Metadata.NullDisplayText;
Expand All @@ -82,4 +82,4 @@ public static string GetSimpleDisplayText(this ModelExplorer modelExplorer)
return Convert.ToString(firstProperty.Model, CultureInfo.CurrentCulture);
}
}
}
}
2 changes: 1 addition & 1 deletion src/Mvc/MvcNoDeps.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"src\\Mvc\\test\\WebSites\\SimpleWebSite\\SimpleWebSite.csproj",
"src\\Mvc\\test\\WebSites\\TagHelpersWebSite\\TagHelpersWebSite.csproj",
"src\\Mvc\\test\\WebSites\\VersioningWebSite\\VersioningWebSite.csproj",
"src\\Mvc\\test\\WebSites\\XmlFormattersWebSite\\XmlFormattersWebSite.csproj",
"src\\Mvc\\test\\WebSites\\XmlFormattersWebSite\\XmlFormattersWebSite.csproj"
]
}
}
4 changes: 1 addition & 3 deletions src/Shared/ChunkingCookieManager/ChunkingCookieManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ private static int ParseChunksCount(string? value)
{
if (value != null && value.StartsWith(ChunkCountPrefix, StringComparison.Ordinal))
{
var chunksCountString = value.Substring(ChunkCountPrefix.Length);
int chunksCount;
if (int.TryParse(chunksCountString, NumberStyles.None, CultureInfo.InvariantCulture, out chunksCount))
if (int.TryParse(value.AsSpan(ChunkCountPrefix.Length), NumberStyles.None, CultureInfo.InvariantCulture, out var chunksCount))
{
return chunksCount;
}
Expand Down

0 comments on commit d32b018

Please sign in to comment.