Optimize HTML symbol content string allocation
This commit is contained in:
parent
e65c2ef1ef
commit
bd4300d8cc
|
|
@ -543,12 +543,13 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
|
||||||
{
|
{
|
||||||
CSharpKeyword keyword;
|
CSharpKeyword keyword;
|
||||||
var type = CSharpSymbolType.Identifier;
|
var type = CSharpSymbolType.Identifier;
|
||||||
if (_keywords.TryGetValue(Buffer.ToString(), out keyword))
|
var symbolContent = Buffer.ToString();
|
||||||
|
if (_keywords.TryGetValue(symbolContent, out keyword))
|
||||||
{
|
{
|
||||||
type = CSharpSymbolType.Keyword;
|
type = CSharpSymbolType.Keyword;
|
||||||
}
|
}
|
||||||
|
|
||||||
symbol = new CSharpSymbol(CurrentStart, Buffer.ToString(), type)
|
symbol = new CSharpSymbol(CurrentStart, symbolContent, type)
|
||||||
{
|
{
|
||||||
Keyword = type == CSharpSymbolType.Keyword ? (CSharpKeyword?)keyword : null,
|
Keyword = type == CSharpSymbolType.Keyword ? (CSharpKeyword?)keyword : null,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -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
|
// http://dev.w3.org/html5/spec/Overview.html#data-state
|
||||||
private StateResult Data()
|
private StateResult Data()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -215,12 +215,17 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
|
||||||
errors[i] = CurrentErrors[i];
|
errors[i] = CurrentErrors[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
sym = CreateSymbol(start, Buffer.ToString(), type, errors);
|
sym = CreateSymbol(start, GetSymbolContent(type), type, errors);
|
||||||
}
|
}
|
||||||
StartSymbol();
|
StartSymbol();
|
||||||
return sym;
|
return sym;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual string GetSymbolContent(TSymbolType type)
|
||||||
|
{
|
||||||
|
return Buffer.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
protected bool TakeUntil(Func<char, bool> predicate)
|
protected bool TakeUntil(Func<char, bool> predicate)
|
||||||
{
|
{
|
||||||
// Take all the characters up to the end character
|
// Take all the characters up to the end character
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue