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

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