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

Minor optimizations in if() blocks #2951

Merged
merged 2 commits into from
May 6, 2020
Merged

Minor optimizations in if() blocks #2951

merged 2 commits into from
May 6, 2020

Conversation

Ayesh
Copy link
Contributor

@Ayesh Ayesh commented May 5, 2020

A PR with a few micro-optimizations for performance and readability.

  1. if() expression order

Expressions in if() are run left-to-right, and evaluating computationally smaller expressions can improve the performance a little bit. This PR contains two of such improvements where it puts === null call and a truthy evaluation ahead of function calls.

  1. if()-elseif()-else branching optimizations.

If such a block returns something, the execution of that function is complete, so we often do not need elseif or else blocks. While they don't provide any performance benefit, we take nested code outside, which reduces the cognitive load on developers.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling d5a3f35 on Ayesh:if-branching into b66a2d8 on slimphp:4.x.

@l0gicgate l0gicgate added this to the 4.6.0 milestone May 6, 2020
Copy link
Member

@l0gicgate l0gicgate left a comment

Choose a reason for hiding this comment

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

Hey thanks for this PR @Ayesh! I suppose there is probably quite of a few instances of this throughout all the codebases. I’m a bit surprised that static analysis doesn’t notify us of those.

@l0gicgate l0gicgate merged commit 18f23fe into slimphp:4.x May 6, 2020
@Ayesh
Copy link
Contributor Author

Ayesh commented May 6, 2020

Thanks a lot @l0gicgate :)
I will see for more similar optimizations and send a PR. Most of the elseif blocks are easy to refactor and remove the besting without affecting anything else.

I also sent slimphp/Slim-Csrf#116 and an update to it if you could kindly take a look. Thank you.

@Ayesh Ayesh deleted the if-branching branch May 6, 2020 18:03
@l0gicgate l0gicgate mentioned this pull request Nov 15, 2020
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.

None yet

3 participants