Optimize 'Span.Content' memory allocation

This commit is contained in:
Yves57 2016-12-22 13:48:33 +01:00 committed by Ryan Nowak
parent 6b075880ce
commit e9a688be15
1 changed files with 14 additions and 5 deletions

View File

@ -41,9 +41,17 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
get get
{ {
if (_content == null) if (_content == null)
{
var symbolCount = Symbols.Count;
if (symbolCount == 1)
{
// Perf: no StringBuilder allocation if not necessary
_content = Symbols[0].Content;
}
else
{ {
var builder = new StringBuilder(); var builder = new StringBuilder();
for (var i = 0; i < Symbols.Count; i++) for (var i = 0; i < symbolCount; i++)
{ {
var symbol = Symbols[i]; var symbol = Symbols[i];
builder.Append(symbol.Content); builder.Append(symbol.Content);
@ -51,6 +59,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
_content = builder.ToString(); _content = builder.ToString();
} }
}
return _content; return _content;
} }