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

Use static lambda if a binding to $this is not required. #4784

Merged
merged 1 commit into from
Jun 7, 2021

Conversation

jeromegamez
Copy link
Contributor

When not declared static, anonymous functions are bound to/hold a reference to $this, which can prevent garbage collection. It can also bring additional performance improvements, e.g. as here

Also, by using static function () {} closures, we squeezed out another 15% of hydration performance when dealing with private properties.

:octocat:

Copy link
Member

@lonnieezell lonnieezell left a comment

Choose a reason for hiding this comment

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

I don't know exactly how this change would affect existing developers, if at all. I'm guessing not at all, and the changes seem simple enough. I'll approve but I'd love to hear from @MGatner or @paulbalandan

Copy link
Member

@paulbalandan paulbalandan left a comment

Choose a reason for hiding this comment

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

This is definitely one of the rules I want to enforce because of the optimizations it can give. 👍

@jeromegamez
Copy link
Contributor Author

Rebased branch to resolve merge conflicts 💪

@paulbalandan
Copy link
Member

@jeromegamez can you rebase again? Thanks!

@jeromegamez
Copy link
Contributor Author

Done!

@paulbalandan paulbalandan merged commit 4d1cb84 into codeigniter4:develop Jun 7, 2021
@jeromegamez jeromegamez deleted the static-lambdas branch June 7, 2021 10:35
@MGatner
Copy link
Member

MGatner commented Jun 7, 2021

Wow, I had no idea. We should update our Events docs since those will always be in this category and should take advantage. Thanks @jeromegamez! 🥳

@kenjis kenjis added the refactor Pull requests that refactor code label Nov 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Pull requests that refactor code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants