Skip to content

Commit d32b018

Browse files
authored
Clean up some MVC code (#31511)
* Clean up some MVC code * PR feedback
1 parent 8f63065 commit d32b018

File tree

9 files changed

+38
-41
lines changed

9 files changed

+38
-41
lines changed

src/Mvc/Mvc.Core/src/Controllers/ControllerBinderDelegateProvider.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.Linq;
78
using System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Mvc.Abstractions;
810
using Microsoft.AspNetCore.Mvc.ModelBinding;
911

1012
namespace Microsoft.AspNetCore.Mvc.Controllers
@@ -55,6 +57,18 @@ internal static class ControllerBinderDelegateProvider
5557
return null;
5658
}
5759

60+
var parameters = actionDescriptor.Parameters switch
61+
{
62+
List<ParameterDescriptor> list => list.ToArray(),
63+
_ => actionDescriptor.Parameters.ToArray()
64+
};
65+
66+
var properties = actionDescriptor.BoundProperties switch
67+
{
68+
List<ParameterDescriptor> list => list.ToArray(),
69+
_ => actionDescriptor.BoundProperties.ToArray()
70+
};
71+
5872
return Bind;
5973

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

6882
Debug.Assert(valueProvider is not null);
6983

70-
var parameters = actionDescriptor.Parameters;
71-
72-
for (var i = 0; i < parameters.Count; i++)
84+
for (var i = 0; i < parameters.Length; i++)
7385
{
7486
var parameter = parameters[i];
7587
var bindingInfo = parameterBindingInfo![i];
@@ -95,8 +107,7 @@ async Task Bind(ControllerContext controllerContext, object controller, Dictiona
95107
}
96108
}
97109

98-
var properties = actionDescriptor.BoundProperties;
99-
for (var i = 0; i < properties.Count; i++)
110+
for (var i = 0; i < properties.Length; i++)
100111
{
101112
var property = properties[i];
102113
var bindingInfo = propertyBindingInfo![i];

src/Mvc/Mvc.Razor/src/TagHelpers/UrlResolutionTagHelper.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,7 @@ private static string CreateTrimmedString(string input, int start)
343343

344344
private static bool IsCharWhitespace(char ch)
345345
{
346-
for (var i = 0; i < ValidAttributeWhitespaceChars.Length; i++)
347-
{
348-
if (ValidAttributeWhitespaceChars[i] == ch)
349-
{
350-
return true;
351-
}
352-
}
353-
// the character is not white space
354-
return false;
346+
return ValidAttributeWhitespaceChars.AsSpan().IndexOf(ch) != -1;
355347
}
356348

357349
private class EncodeFirstSegmentContent : IHtmlContent

src/Mvc/Mvc.TagHelpers/src/AttributeMatcher.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using Microsoft.AspNetCore.Razor.TagHelpers;
76

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

4948
// Perf: Avoid allocating enumerator
50-
var modeInfosCount = modeInfos.Count;
5149
var allAttributes = context.AllAttributes;
5250
// Read interface .Count once rather than per iteration
5351
var allAttributesCount = allAttributes.Count;
54-
for (var i = 0; i < modeInfosCount; i++)
52+
foreach (var modeInfo in modeInfos)
5553
{
56-
var modeInfo = modeInfos[i];
5754
var requiredAttributes = modeInfo.Attributes;
5855
// If there are fewer attributes present than required, one or more of them must be missing.
59-
if (allAttributesCount >= requiredAttributes.Length &&
56+
if (allAttributesCount >= requiredAttributes.Length &&
6057
!HasMissingAttributes(allAttributes, requiredAttributes) &&
6158
compare(result, modeInfo.Mode) <= 0)
6259
{

src/Mvc/Mvc.ViewFeatures/src/DefaultDisplayTemplates.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper)
212212
var viewBufferScope = serviceProvider.GetRequiredService<IViewBufferScope>();
213213

214214
var content = new HtmlContentBuilder(modelExplorer.Metadata.Properties.Count);
215-
foreach (var propertyExplorer in modelExplorer.Properties)
215+
foreach (var propertyExplorer in modelExplorer.PropertiesInternal)
216216
{
217217
var propertyMetadata = propertyExplorer.Metadata;
218218
if (!ShouldShow(propertyExplorer, templateInfo))

src/Mvc/Mvc.ViewFeatures/src/DefaultEditorTemplates.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper)
255255
var viewBufferScope = serviceProvider.GetRequiredService<IViewBufferScope>();
256256

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

477477
public override void Write(char[] buffer, int index, int count)
478478
{
479-
if (count > 0)
480-
{
481-
HasContent = true;
482-
}
479+
HasContent |= count > 0;
480+
}
481+
482+
public override void Write(ReadOnlySpan<char> buffer)
483+
{
484+
HasContent |= buffer.IsEmpty;
483485
}
484486

485487
public override void Write(string value)
486488
{
487-
if (!string.IsNullOrEmpty(value))
488-
{
489-
HasContent = true;
490-
}
489+
HasContent |= !string.IsNullOrEmpty(value);
491490
}
492491
}
493492

@@ -540,7 +539,7 @@ public override void Encode(TextWriter output, string value, int startIndex, int
540539
return;
541540
}
542541

543-
output.Write(value.Substring(startIndex, characterCount));
542+
output.Write(value.AsSpan(startIndex, characterCount));
544543
}
545544

546545
public override unsafe int FindFirstCharacterToEncode(char* text, int textLength)

src/Mvc/Mvc.ViewFeatures/src/ModelExplorer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public Type ModelType
199199
/// </remarks>
200200
public IEnumerable<ModelExplorer> Properties => PropertiesInternal;
201201

202-
private ModelExplorer[] PropertiesInternal
202+
internal ModelExplorer[] PropertiesInternal
203203
{
204204
get
205205
{
@@ -472,4 +472,4 @@ private ModelExplorer CreateExplorerForProperty(
472472
return new ModelExplorer(_metadataProvider, this, propertyMetadata, modelAccessor);
473473
}
474474
}
475-
}
475+
}

src/Mvc/Mvc.ViewFeatures/src/ModelExplorerExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Diagnostics;
66
using System.Globalization;
7-
using System.Linq;
87

98
namespace Microsoft.AspNetCore.Mvc.ViewFeatures
109
{
@@ -68,12 +67,13 @@ public static string GetSimpleDisplayText(this ModelExplorer modelExplorer)
6867
return stringResult;
6968
}
7069

71-
var firstProperty = modelExplorer.Properties.FirstOrDefault();
72-
if (firstProperty == null)
70+
if (modelExplorer.PropertiesInternal.Length == 0)
7371
{
7472
return string.Empty;
7573
}
7674

75+
var firstProperty = modelExplorer.PropertiesInternal[0];
76+
7777
if (firstProperty.Model == null)
7878
{
7979
return firstProperty.Metadata.NullDisplayText;
@@ -82,4 +82,4 @@ public static string GetSimpleDisplayText(this ModelExplorer modelExplorer)
8282
return Convert.ToString(firstProperty.Model, CultureInfo.CurrentCulture);
8383
}
8484
}
85-
}
85+
}

src/Mvc/MvcNoDeps.slnf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"src\\Mvc\\test\\WebSites\\SimpleWebSite\\SimpleWebSite.csproj",
6868
"src\\Mvc\\test\\WebSites\\TagHelpersWebSite\\TagHelpersWebSite.csproj",
6969
"src\\Mvc\\test\\WebSites\\VersioningWebSite\\VersioningWebSite.csproj",
70-
"src\\Mvc\\test\\WebSites\\XmlFormattersWebSite\\XmlFormattersWebSite.csproj",
70+
"src\\Mvc\\test\\WebSites\\XmlFormattersWebSite\\XmlFormattersWebSite.csproj"
7171
]
7272
}
7373
}

src/Shared/ChunkingCookieManager/ChunkingCookieManager.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ private static int ParseChunksCount(string? value)
7070
{
7171
if (value != null && value.StartsWith(ChunkCountPrefix, StringComparison.Ordinal))
7272
{
73-
var chunksCountString = value.Substring(ChunkCountPrefix.Length);
74-
int chunksCount;
75-
if (int.TryParse(chunksCountString, NumberStyles.None, CultureInfo.InvariantCulture, out chunksCount))
73+
if (int.TryParse(value.AsSpan(ChunkCountPrefix.Length), NumberStyles.None, CultureInfo.InvariantCulture, out var chunksCount))
7674
{
7775
return chunksCount;
7876
}

0 commit comments

Comments
 (0)