From 2a426dfea5982bf654a5dd0cedf138bdfc233afc Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Thu, 30 Aug 2018 01:07:45 +0100 Subject: [PATCH] Make ViewBuffer methods more inlinable (#8339) * Make ViewBuffer methods more inlinable --- .../Internal/ViewBuffer.cs | 45 +++++++++++++------ .../Internal/ViewBufferPage.cs | 4 ++ 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ViewBuffer.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ViewBuffer.cs index 5143e515d4..7a39276b7b 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ViewBuffer.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ViewBuffer.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Runtime.CompilerServices; using System.Text.Encodings.Web; using System.Threading.Tasks; using Microsoft.AspNetCore.Html; @@ -90,55 +91,73 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal } /// + // Very common trival method; nudge it to inline https://github.com/aspnet/Mvc/pull/8339 + [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } /// + // Very common trival method; nudge it to inline https://github.com/aspnet/Mvc/pull/8339 + [MethodImpl(MethodImplOptions.AggressiveInlining)] public IHtmlContentBuilder AppendHtml(IHtmlContent content) { - if (content == null) + if (content != null) { - return this; + AppendValue(new ViewBufferValue(content)); } - AppendValue(new ViewBufferValue(content)); return this; } /// + // Very common trival method; nudge it to inline https://github.com/aspnet/Mvc/pull/8339 + [MethodImpl(MethodImplOptions.AggressiveInlining)] public IHtmlContentBuilder AppendHtml(string encoded) { - if (encoded == null) + if (encoded != null) { - return this; + AppendValue(new ViewBufferValue(encoded)); } - AppendValue(new ViewBufferValue(encoded)); 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) { var page = GetCurrentPage(); page.Append(value); } + // Very common trival method; nudge it to inline https://github.com/aspnet/Mvc/pull/8339 + [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ViewBufferPage.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ViewBufferPage.cs index b561e71825..416e329bd7 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ViewBufferPage.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ViewBufferPage.cs @@ -1,6 +1,8 @@ // 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. +using System.Runtime.CompilerServices; + namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal { public class ViewBufferPage @@ -18,6 +20,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal 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; } }