Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change SyntaxEquivalence.AreEquivalent to use a more appropriate pooling mechanism for the stack it uses to walk trees. #74610

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Compilers/CSharp/Portable/Syntax/SyntaxEquivalence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ namespace Microsoft.CodeAnalysis.CSharp.Syntax

internal static class SyntaxEquivalence
{
private static readonly ObjectPool<Stack<(GreenNode? before, GreenNode? after)>> s_equivalenceCheckStack =
new ObjectPool<Stack<(GreenNode?, GreenNode?)>>(() => new Stack<(GreenNode?, GreenNode?)>());

internal static bool AreEquivalent(SyntaxTree? before, SyntaxTree? after, Func<SyntaxKind, bool>? ignoreChildNode, bool topLevel)
{
if (before == after)
Expand Down Expand Up @@ -102,13 +105,14 @@ private static bool AreTokensEquivalent(GreenNode? before, GreenNode? after, Fun
private static bool AreEquivalentRecursive(GreenNode? before, GreenNode? after, Func<SyntaxKind, bool>? ignoreChildNode, bool topLevel)
{
// Use an explicit stack so we can walk down deep trees without blowing the real stack.
var stack = ArrayBuilder<(GreenNode? before, GreenNode? after)>.GetInstance();
var stack = s_equivalenceCheckStack.Allocate();
stack.Push((before, after));

try
{
while (stack.TryPop(out var current))
while (stack.Count > 0)
{
var current = stack.Pop();
if (!areEquivalentSingleLevel(current.before, current.after))
return false;
}
Expand All @@ -117,7 +121,7 @@ private static bool AreEquivalentRecursive(GreenNode? before, GreenNode? after,
}
finally
{
stack.Free();
s_equivalenceCheckStack.Free(stack);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there not FreeAndClear/ClearAndFree?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's defined at the workspace level

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well that's silly :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be fine to move that down if you wanted.

Copy link
Member

@333fred 333fred Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I will point out that those helpers stop returning pooled objects after they get to 512 items. That might not work well here either.

}

bool areEquivalentSingleLevel(GreenNode? before, GreenNode? after)
Expand Down
Loading