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

Modify BinderFactory to use a shared pool instead of one per BinderFactory instance. #72725

Merged
merged 4 commits into from
Mar 26, 2024

Conversation

ToddGrun
Copy link
Contributor

This showed up as abou 0.7% of allocations on a profile I took on my machine where I typed about 20 characters in a large file.

image

…ctory instance.

This showed up as abou 0.7% of allocations on a profile I took on my machine where I typed about 20 characters in a large file.
@ToddGrun ToddGrun requested a review from a team as a code owner March 25, 2024 22:10
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 25, 2024
333fred
333fred previously approved these changes Mar 25, 2024
@@ -55,16 +55,15 @@ public override bool Equals(object obj)
// In a typing scenario, GetBinder is regularly called with a non-zero position.
// This results in a lot of allocations of BinderFactoryVisitors. Pooling them
// reduces this churn to almost nothing.
private readonly ObjectPool<BinderFactoryVisitor> _binderFactoryVisitorPool;
private static readonly ObjectPool<BinderFactoryVisitor> s_binderFactoryVisitorPool
= new ObjectPool<BinderFactoryVisitor>(() => new BinderFactoryVisitor(), 64);
Copy link
Member

@jcouv jcouv Mar 25, 2024

Choose a reason for hiding this comment

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

() => new BinderFactoryVisitor()

static lambda? #Closed

Binder result = visitor.Visit(node);
_binderFactoryVisitorPool.Free(visitor);
s_binderFactoryVisitorPool.Free(visitor);
Copy link
Member

@jcouv jcouv Mar 25, 2024

Choose a reason for hiding this comment

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

Would it be useful to clear the instance (remove the this reference) before returning it to the pool? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, would it be preferable to clear out the BinderFactoryVisitor instance completely (CSharpSyntaxNode/Symbol/BinderFactory references), or are you just worried about the BinderFactory reference being kept about?

Copy link
Member

Choose a reason for hiding this comment

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

I'd clear all that's passed in Initialize.

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

Done with review pass (iteration 1)

@jcouv jcouv self-assigned this Mar 25, 2024

internal BinderFactory(CSharpCompilation compilation, SyntaxTree syntaxTree, bool ignoreAccessibility)
{
_compilation = compilation;
_syntaxTree = syntaxTree;
_ignoreAccessibility = ignoreAccessibility;

_binderFactoryVisitorPool = new ObjectPool<BinderFactoryVisitor>(() => new BinderFactoryVisitor(this), 64);
Copy link
Member

Choose a reason for hiding this comment

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

One of the challenges with using all these static pools is that it's very hard to test. Our tests run in parallel in many cases and hence trying to test behavior like "pool is same going in and out" is basically impossible. One of the nice parts about the binder is BinderFactory is that it had the pool as an instance variable hence it was easy to test.

Could we consider instead

  1. Keep these as an instance member
  2. Take the pool as an optional parameter
  3. When it's null grab from the static pool?

All this ambient authority is hard to manage at times in the compiler

@333fred, @jcouv thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I can add a parameter to the ctor that takes in an optional pool, one thing to note is that it appears that would require increasing the visibility of BinderFactoryVisitor to internal. I'll wait to make that change until there is a consensus on the preferred approach here.

Copy link
Member

Choose a reason for hiding this comment

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

@jaredpar If we clear the instances when they are free'd back to the pool, the risk of side-effects via global state is reduced. Would that mitigate your concerns?

Copy link
Member

Choose a reason for hiding this comment

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

I guess I'm unsure of the need to complicate it here? What is the goal of such a test?

Copy link
Member

Choose a reason for hiding this comment

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

Would that mitigate your concerns?

No. The concern is whether we're using the pool properly: basically putting objects back when we're done. That is very hard to test when the pool is an ambient authority.

What is the goal of such a test?

To make sure we're actually using the pool properly. Its' a functional part of the type, testing it is a reasonable action.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

4th commit adds the optional pool parameter to the constructor

@sharwell
Copy link
Member

sharwell commented Mar 26, 2024

Is this whole thing just a workaround for the fact that we don't have CSharpSyntaxVisitor<TArgument, TResult> like we have for CSharpSymbolVisitor<TArgument, TResult>? To be fair, you'd still need a pool for the argument in this case or it would cause stack depth problems.

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (iteration 4)

@jcouv jcouv requested a review from 333fred March 26, 2024 17:49
@jcouv jcouv dismissed 333fred’s stale review March 26, 2024 17:49

New iterations

@ToddGrun ToddGrun merged commit 8ca20e8 into dotnet:main Mar 26, 2024
24 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Mar 26, 2024
@dibarbet dibarbet modified the milestones: Next, 17.11 P1 Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants