Make ViewBuffer methods more inlinable (#8339)

* Make ViewBuffer methods more inlinable
This commit is contained in:
Ben Adams 2018-08-30 01:07:45 +01:00 committed by Pranav K
parent c7f6e7ab2f
commit 2a426dfea5
2 changed files with 36 additions and 13 deletions

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.CompilerServices;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Html;
@ -90,55 +91,73 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
} }
/// <inheritdoc /> /// <inheritdoc />
// Very common trival method; nudge it to inline https://github.com/aspnet/Mvc/pull/8339
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IHtmlContentBuilder Append(string unencoded) public IHtmlContentBuilder Append(string unencoded)
{ {
if (unencoded == null) if (unencoded != null)
{ {
return this; // Text that needs encoding is the uncommon case in views, which is why it
// creates a wrapper and pre-encoded text does not.
AppendValue(new ViewBufferValue(new EncodingWrapper(unencoded)));
} }
// Text that needs encoding is the uncommon case in views, which is why it
// creates a wrapper and pre-encoded text does not.
AppendValue(new ViewBufferValue(new EncodingWrapper(unencoded)));
return this; return this;
} }
/// <inheritdoc /> /// <inheritdoc />
// Very common trival method; nudge it to inline https://github.com/aspnet/Mvc/pull/8339
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IHtmlContentBuilder AppendHtml(IHtmlContent content) public IHtmlContentBuilder AppendHtml(IHtmlContent content)
{ {
if (content == null) if (content != null)
{ {
return this; AppendValue(new ViewBufferValue(content));
} }
AppendValue(new ViewBufferValue(content));
return this; return this;
} }
/// <inheritdoc /> /// <inheritdoc />
// Very common trival method; nudge it to inline https://github.com/aspnet/Mvc/pull/8339
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IHtmlContentBuilder AppendHtml(string encoded) public IHtmlContentBuilder AppendHtml(string encoded)
{ {
if (encoded == null) if (encoded != null)
{ {
return this; AppendValue(new ViewBufferValue(encoded));
} }
AppendValue(new ViewBufferValue(encoded));
return this; return this;
} }
// Very common trival method; nudge it to inline https://github.com/aspnet/Mvc/pull/8339
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AppendValue(ViewBufferValue value) private void AppendValue(ViewBufferValue value)
{ {
var page = GetCurrentPage(); var page = GetCurrentPage();
page.Append(value); page.Append(value);
} }
// Very common trival method; nudge it to inline https://github.com/aspnet/Mvc/pull/8339
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ViewBufferPage GetCurrentPage() private ViewBufferPage GetCurrentPage()
{ {
if (_currentPage == null || _currentPage.IsFull) var currentPage = _currentPage;
if (currentPage == null || currentPage.IsFull)
{ {
AddPage(new ViewBufferPage(_bufferScope.GetPage(_pageSize))); // Uncommon slow-path
return AppendNewPage();
} }
return currentPage;
}
// Slow path for above, don't inline
[MethodImpl(MethodImplOptions.NoInlining)]
private ViewBufferPage AppendNewPage()
{
AddPage(new ViewBufferPage(_bufferScope.GetPage(_pageSize)));
return _currentPage; return _currentPage;
} }

View File

@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Runtime.CompilerServices;
namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
{ {
public class ViewBufferPage public class ViewBufferPage
@ -18,6 +20,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
public bool IsFull => Count == Capacity; public bool IsFull => Count == Capacity;
// Very common trival method; nudge it to inline https://github.com/aspnet/Mvc/pull/8339
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(ViewBufferValue value) => Buffer[Count++] = value; public void Append(ViewBufferValue value) => Buffer[Count++] = value;
} }
} }