-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Evaluate count() once per loop instead of once per iteration
#12837
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
Open
mukeshpanchal27
wants to merge
7
commits into
WordPress:trunk
Choose a base branch
from
mukeshpanchal27:perf/hoist-count
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+8
−4
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
caa04a5
Editor, Themes: Evaluate count() once per loop instead of once per it…
mukeshpanchal27 073ae0e
Merge branch 'trunk' into perf/hoist-count
mukeshpanchal27 83e095b
Merge branch 'trunk' into perf/hoist-count
mukeshpanchal27 37fa950
Merge branch 'trunk' into perf/hoist-count
mukeshpanchal27 79d89df
Move count() out of the loop initialiser into a variable
mukeshpanchal27 13cdd71
Merge branch 'perf/hoist-count' of github.com:mukeshpanchal27/wordpre…
mukeshpanchal27 eaf9235
Merge branch 'trunk' into perf/hoist-count
mukeshpanchal27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Alternatively:
However, given that
range()doesn't use a generator, this means that the full array has to be in memory. But in reality, it's not many!So I think this is a better approach.
Uh oh!
There was an error while loading. Please reload this page.
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.
Gemini share response:
These two approaches use entirely different paradigms—and in PHP, the second option (
foreachwithrange()) introduces severe memory overhead and hidden performance issues.Here is how they compare and why you should avoid
range()for looping over arrays.Comparison
forloop (Option 1): Fast and memory-efficient. Calculates array length once ($script_count) and iterates using standard integer incrementation.foreach( range(...) )(Option 2): Creates an entire second array in memory filled with integers from0tocount($scripts) - 1before the loop even starts.Key Issues with
foreach( range(...) )1. Memory Overhead
If
$scriptscontains 10,000 items,range(0, 9999)allocates a brand-new array of 10,000 integers in RAM just to serve as the loop counter.2. Off-by-One Bugs on Empty Arrays
If
$scriptsis empty (count($scripts)is0),range(0, -1)will produce[0, -1]. Your loop will attempt to run twice instead of zero times, leading toUndefined array keywarnings.3. Unnecessary Complexity
Iterating over array indices with
foreach+rangedefeats the purpose offoreach.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.
How many items are we talking about here, though?
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.
The theoretical performance concern would be addressed by a generator, which I noted above that PHP doesn't use unfortunately. Nevertheless, I don't believe the number of items is particularly large.
That said, I don't feel strongly about using
range().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! That said, can we mark this conversation as resolved?
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.
It won't hurt, and changing it can reduce code smell so others don't raise this in the future.
I would suggest, however, that the count initializer be placed inside the
forloop, like this:Or for a common idiom for naming:
This can be found in core (via 3rd party libraries), for example:
wordpress-develop/src/wp-includes/SimplePie/src/IRI.php
Line 519 in f358f5e
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.
Another example:
wordpress-develop/src/wp-includes/formatting.php
Line 769 in f358f5e
There quite a few.
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.
Initially i propose that 83e095b but based on the #12837 (comment) feedback i apply that suggestion.
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.
Asking as a learning opportunity: what would be the advantage? That way,
count()is called at each loop iteration and the same variable is set again and again at each loop iteration. That seems completely unnecessary to me. If there are other places where this pattern is in used, then I'd argue it should be imporved there as well.I know there are many occurrences of similar patterns, also on the JS side. That doesn't mean they are entirely OK.
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.
Once this is finalized, I'll open a separate PR to address the other occurrences. That's my plan as a follow-up.