-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean up some MVC code #31511
Clean up some MVC code #31511
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
{ | ||
|
@@ -47,13 +46,11 @@ 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.AsSpan()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interestingly, foreaching over and array and an explicit for loop over an array are equivalent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
C# compiler lowers the foreach over array to a for-loop (I believe this is done since the beginning). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, you can see it in the C# -> C# translation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does the same for Span<T> too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the T[] case for really (safe) high-perf |
||
{ | ||
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 && | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.AsSpan()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
{ | ||
var propertyMetadata = propertyExplorer.Metadata; | ||
if (!ShouldShow(propertyExplorer, templateInfo)) | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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.AsSpan()) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||||||||||||
{ | ||||||||||||
var propertyMetadata = propertyExplorer.Metadata; | ||||||||||||
if (!ShouldShow(propertyExplorer, templateInfo)) | ||||||||||||
|
@@ -482,6 +482,14 @@ public override void Write(char[] buffer, int index, int count) | |||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
public override void Write(ReadOnlySpan<char> buffer) | ||||||||||||
{ | ||||||||||||
if (!buffer.IsEmpty) | ||||||||||||
{ | ||||||||||||
HasContent = true; | ||||||||||||
} | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
? |
||||||||||||
} | ||||||||||||
|
||||||||||||
public override void Write(string value) | ||||||||||||
{ | ||||||||||||
if (!string.IsNullOrEmpty(value)) | ||||||||||||
|
@@ -540,7 +548,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) | ||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we make these Arrays instead of lists?