-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Performance: remove unnecessary repeated function call #44545
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hello again @jrfnl - did you measure any results with these changes or confirm that they make things faster or more efficient?
strlen()
boils down to a property lookup, not a string-length computation, so the performance should be on par with storing it inside a variable. further, I would like to see any data suggesting that it's possible to measure the impact of this change; my own tests suggest otherwise.
for the case of the stack count in the block parser this patch introduces a breakage because $this->stack
does change during every iteration of the while
loop it's in. we need to confirm that a small refactor, even one that looks harmless, doesn't introduce a new behavior or defect into the code, and in this case all we need to do is look inside $this->add_block_from_stack();
to see it, as its first line mutates the stack.
I did not do any performance testing, but any kind of function call within a loop condition is generally speaking bad practice, no matter how small the impact may seem. It is also teaching devs bad code patterns.
Okay, so clearly I missed that, which does say something about how the code obfuscates what's going on, but that's another matter. I will remove that change from this PR. |
The length of the `$existing_class` does not change within the loop, so it is completely redundant and inefficient to recalculate the length for every loop.
1812942
to
eb9f916
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, @jrfnl.
LGTM!
In my opinion, calling |
this is fine, and I'm comfortable merging, but we should note that this doesn't have a clear performance impact.
this is inaccurate, as it's not completely inefficient and the length is not recomputed - it's returned as a property lookup on the thanks for the patch! |
@dmsnell If you are comfortable merging, would you please approve this PR? |
Why?
The length of the
$existing_class
does not change within the loop, so it is completely redundant and inefficient to recalculate the length for every loop.Along the same lines, the count of
$this->stack
will not change within the loop.How?
By making the function call once and saving the return value to a variable, we can avoid the unnecessary repeated function calls.