Optimize HTML symbol content string allocation

This commit is contained in:
Yves57 2016-12-20 21:45:31 +01:00 committed by N. Taylor Mullen
parent e65c2ef1ef
commit bd4300d8cc
3 changed files with 65 additions and 3 deletions

View File

@ -543,12 +543,13 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
{
CSharpKeyword keyword;
var type = CSharpSymbolType.Identifier;
if (_keywords.TryGetValue(Buffer.ToString(), out keyword))
var symbolContent = Buffer.ToString();
if (_keywords.TryGetValue(symbolContent, out keyword))
{
type = CSharpSymbolType.Keyword;
}
symbol = new CSharpSymbol(CurrentStart, Buffer.ToString(), type)
symbol = new CSharpSymbol(CurrentStart, symbolContent, type)
{
Keyword = type == CSharpSymbolType.Keyword ? (CSharpKeyword?)keyword : null,
};

View File

@ -70,6 +70,62 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
}
}
// Optimize memory allocation by returning constants for the most frequent cases
protected override string GetSymbolContent(HtmlSymbolType type)
{
var symbolLength = Buffer.Length;
if (symbolLength == 1)
{
switch (type)
{
case HtmlSymbolType.OpenAngle:
return "<";
case HtmlSymbolType.Bang:
return "!";
case HtmlSymbolType.ForwardSlash:
return "/";
case HtmlSymbolType.QuestionMark:
return "?";
case HtmlSymbolType.LeftBracket:
return "[";
case HtmlSymbolType.CloseAngle:
return ">";
case HtmlSymbolType.RightBracket:
return "]";
case HtmlSymbolType.Equals:
return "=";
case HtmlSymbolType.DoubleQuote:
return "\"";
case HtmlSymbolType.SingleQuote:
return "'";
case HtmlSymbolType.WhiteSpace:
if (Buffer[0] == ' ')
{
return " ";
}
if (Buffer[0] == '\t')
{
return "\t";
}
break;
case HtmlSymbolType.NewLine:
if (Buffer[0] == '\n')
{
return "\n";
}
break;
}
}
if (symbolLength == 2 && type == HtmlSymbolType.NewLine)
{
return "\r\n";
}
return base.GetSymbolContent(type);
}
// http://dev.w3.org/html5/spec/Overview.html#data-state
private StateResult Data()
{

View File

@ -215,12 +215,17 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
errors[i] = CurrentErrors[i];
}
sym = CreateSymbol(start, Buffer.ToString(), type, errors);
sym = CreateSymbol(start, GetSymbolContent(type), type, errors);
}
StartSymbol();
return sym;
}
protected virtual string GetSymbolContent(TSymbolType type)
{
return Buffer.ToString();
}
protected bool TakeUntil(Func<char, bool> predicate)
{
// Take all the characters up to the end character