提交 7f286887 编写于 作者: D Dustin Campbell 提交者: Tomas Matousek

Use ObjectPool rather than thread local storage

上级 01516377
......@@ -123,8 +123,7 @@ public static bool CanRemoveParentheses(this ParenthesizedExpressionSyntax node)
: false;
}
[ThreadStatic]
private static Stack<SyntaxNode> s_nodeStack;
private static readonly ObjectPool<Stack<SyntaxNode>> s_nodeStackPool = new ObjectPool<Stack<SyntaxNode>>(() => new Stack<SyntaxNode>());
private static bool RemovalMayIntroduceInterpolationAmbiguity(ParenthesizedExpressionSyntax node)
{
......@@ -148,38 +147,37 @@ private static bool RemovalMayIntroduceInterpolationAmbiguity(ParenthesizedExpre
return false;
}
if (s_nodeStack == null)
var stack = s_nodeStackPool.AllocateAndClear();
try
{
s_nodeStack = new Stack<SyntaxNode>();
}
else
{
s_nodeStack.Clear();
}
s_nodeStack.Push(node.Expression);
while (s_nodeStack.Count > 0)
{
var expression = s_nodeStack.Pop();
stack.Push(node.Expression);
foreach (var nodeOrToken in expression.ChildNodesAndTokens())
while (stack.Count > 0)
{
// Note: There's no need drill into other parenthesized expressions, since any colons in them would be unambiguous.
if (nodeOrToken.IsNode && !nodeOrToken.IsKind(SyntaxKind.ParenthesizedExpression))
{
s_nodeStack.Push(nodeOrToken.AsNode());
}
else if (nodeOrToken.IsToken)
var expression = stack.Pop();
foreach (var nodeOrToken in expression.ChildNodesAndTokens())
{
if (nodeOrToken.IsKind(SyntaxKind.ColonToken) || nodeOrToken.IsKind(SyntaxKind.ColonColonToken))
// Note: There's no need drill into other parenthesized expressions, since any colons in them would be unambiguous.
if (nodeOrToken.IsNode && !nodeOrToken.IsKind(SyntaxKind.ParenthesizedExpression))
{
s_nodeStack.Clear();
return true;
stack.Push(nodeOrToken.AsNode());
}
else if (nodeOrToken.IsToken)
{
if (nodeOrToken.IsKind(SyntaxKind.ColonToken) || nodeOrToken.IsKind(SyntaxKind.ColonColonToken))
{
return true;
}
}
}
}
}
finally
{
s_nodeStackPool.ClearAndFree(stack);
}
return false;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册