Skip to content

Improve NullHtmlEncoder perf #7987

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

Merged
merged 5 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ public partial interface ITagHelperComponent
void Init(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext context);
System.Threading.Tasks.Task ProcessAsync(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext context, Microsoft.AspNetCore.Razor.TagHelpers.TagHelperOutput output);
}
public partial class NullHtmlEncoder : System.Text.Encodings.Web.HtmlEncoder
public sealed partial class NullHtmlEncoder : System.Text.Encodings.Web.HtmlEncoder
{
protected NullHtmlEncoder() { }
internal NullHtmlEncoder() { }
public static new Microsoft.AspNetCore.Razor.TagHelpers.NullHtmlEncoder Default { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
public override int MaxOutputCharactersPerInputCharacter { get { throw null; } }
public override void Encode(System.IO.TextWriter output, char[] value, int startIndex, int characterCount) { }
public override void Encode(System.IO.TextWriter output, string value, int startIndex, int characterCount) { }
public override string Encode(string value) { throw null; }
public unsafe override int FindFirstCharacterToEncode(char* text, int textLength) { throw null; }
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public unsafe override int FindFirstCharacterToEncode(char* text, int textLength) { throw null; }
public unsafe override bool TryEncodeUnicodeScalar(int unicodeScalar, char* buffer, int bufferLength, out int numberOfCharactersWritten) { throw null; }
public override bool WillEncode(int unicodeScalar) { throw null; }
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public override bool WillEncode(int unicodeScalar) { throw null; }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Class, AllowMultiple=false, Inherited=false)]
public sealed partial class OutputElementHintAttribute : System.Attribute
Expand Down
20 changes: 9 additions & 11 deletions src/Razor/Razor/src/TagHelpers/NullHtmlEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text.Encodings.Web;

namespace Microsoft.AspNetCore.Razor.TagHelpers
Expand All @@ -11,12 +12,12 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers
/// A <see cref="HtmlEncoder"/> that does not encode. Should not be used when writing directly to a response
/// expected to contain valid HTML.
/// </summary>
public class NullHtmlEncoder : HtmlEncoder
public sealed class NullHtmlEncoder : HtmlEncoder
{
/// <summary>
/// Initializes a <see cref="NullHtmlEncoder"/> instance.
/// </summary>
protected NullHtmlEncoder()
private NullHtmlEncoder()
{
}

Expand All @@ -27,13 +28,7 @@ protected NullHtmlEncoder()
public static new NullHtmlEncoder Default { get; } = new NullHtmlEncoder();

/// <inheritdoc />
public override int MaxOutputCharactersPerInputCharacter
{
get
{
return 1;
}
}
public override int MaxOutputCharactersPerInputCharacter => 1;

/// <inheritdoc />
public override string Encode(string value)
Expand Down Expand Up @@ -67,7 +62,6 @@ public override void Encode(TextWriter output, char[] value, int startIndex, int
output.Write(value, startIndex, characterCount);
}

/// <inheritdoc />
public override void Encode(TextWriter output, string value, int startIndex, int characterCount)
{
if (output == null)
Expand All @@ -85,10 +79,13 @@ public override void Encode(TextWriter output, string value, int startIndex, int
return;
}

output.Write(value.Substring(startIndex, characterCount));
var span = value.AsSpan(startIndex, characterCount);

output.Write(span);
}

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override unsafe int FindFirstCharacterToEncode(char* text, int textLength)
{
return -1;
Expand All @@ -112,6 +109,7 @@ public override unsafe bool TryEncodeUnicodeScalar(
}

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool WillEncode(int unicodeScalar)
{
return false;
Expand Down