-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Caching the size/length in loops to slightly improve performance #15852
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
Conversation
PR Reviewer Guide πHere are some key observations to aid the review process:
|
PR Code Suggestions β¨Explore these optional code suggestions:
|
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 your PR.
What's the performance gain?
Hi @diemol, thanks for the feedback! The performance improvement is minimal but measurable. I ran a quick JMH benchmark in a separate project and saw a slight gain from caching the size before iteration:
You can find the benchmark code here: https://github.com/LuisOsv/benchTests Note: I haven't run this within the Selenium codebase directly, as I'm not sure how to integrate JMH into the current Bazel-based setup. |
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.
Thank you, @LuisOsv!
User description
π Related Issues
N/A β performance and readability enhancement
π₯ What does this PR do?
This PR improves performance and readability by caching the size or length of collections and arrays before entering loops.
Instead of repeatedly calling .size() or .length in the loop condition, the value is stored in a local variable and reused.
π§ Implementation Notes
This change is based on a common Java performance recommendation highlighted in High Performance with Java by Javier Fernandez Gonzalez (OβReilly, 2024).
The book explains that repeatedly calling methods like .size() inside loop conditions is less efficient than caching their values in local variables. This approach improves both runtime performance and code readability.
Example refactor:
Instead of:
π‘ Additional Considerations
π Types of changes
π§ͺ Tests Executed
To ensure nothing was broken by this cleanup, the following affected test targets were executed:
β All tests passed successfully.
βΉοΈ Note on Loop Style
In some cases within this PR, the classic for loop with an index is intentionally retained rather than switching to a for-each or Java Stream approach. This is because the existing logic requires indexed access or manual index manipulation.
For such scenarios, the traditional indexed for loop is both more straightforward and readable than using other alternatives.
PR Type
Enhancement
Description
Cache collection/array size in loops for performance
Refactor loop conditions for improved readability
No behavioral or logic changes introduced
Consistent micro-optimization across multiple Java files
Changes walkthrough π
9 files
Cache args length before loop for efficiency
Β Β Β Β Β Β Β Β Β Β Β Β ΒCache args length before loop for efficiency
Β Β Β Β Β Β Β Β Β Β Β Β ΒCache config list size before loop for efficiency
Β Β Β Β Β Β Β ΒCache config list size before loop for efficiency
Β Β Β Β Β Β Β ΒCache string length before loop for efficiency
Β Β Β Β Β Β Β Β Β Β ΒCache array length before loop for efficiency
Β Β Β Β Β Β Β Β Β Β Β ΒCache list size before loops for efficiency
Β Β Β Β Β Β Β Β Β Β Β Β Β ΒCache prefix path array length before loops
Β Β Β Β Β Β Β Β Β Β Β Β Β ΒCache group list size before switch and loop
Β Β Β Β Β Β Β Β Β Β Β Β Β