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

refactor: avoid closure creation #233

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Ted-Zhang
Copy link

refactor: avoid closure creation

The ExecutionStrategyExtensions.ExecuteAsync has an overload method to support passing the state, thereby updating the usage to avoid creating the closure to capture the reference.

Since this code example is part of the official documentation, so I think providing a refined code example would be beneficial.
Official document link

@Ted-Zhang
Copy link
Author

@dotnet-policy-service agree

@BrennanConroy
Copy link
Member

The closures capture other variables so I don't think you're really saving anything here. You can check to see if the closure is avoided by making the delegate static.

int i;
F(static func()
{
    Console.WriteLine(i); // compiler error, i is captured but delegate is static
});

void F(Action a);

@Ted-Zhang
Copy link
Author

The IExecutionStrategy from EF Core has an overloaded method that allows passing the status, this is leveraging that to avoid the closure creation to capture those variables.
https://github.com/dotnet/efcore/blob/fa093d46c94d490317ee87ac257ed5f37a3f5565/src/EFCore/Storage/ExecutionStrategyExtensions.cs#L228C1-L242C1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants