Caching the length/size in a variable for collections in order to avo…#6453
Caching the length/size in a variable for collections in order to avo…#6453LuisOsv wants to merge 3 commits intoapache:masterfrom
Conversation
…id repeated calls within loops, which can slightly improve performance and readability.
|
Hi, Thanks to the PR Thanks |
|
Thanks for the feedback. Since the caching was applied in multiple places, benchmarking every change would be tricky. I can run a benchmark on a representative use case — could you suggest a core functionality or flow you'd like me to focus on? |
src/core/src/main/java/org/apache/jmeter/threads/TestCompiler.java
Outdated
Show resolved
Hide resolved
src/core/src/main/java/org/apache/jmeter/threads/TestCompiler.java
Outdated
Show resolved
Hide resolved
|
|
||
| // Now wrap the default managers with our key manager | ||
| for (int i = 0; i < managers.length; i++) { | ||
| for (int i = 0; i < managersCount; i++) { |
There was a problem hiding this comment.
Could it better be changed to for-each loop instead of factoring managersCount variable?
There was a problem hiding this comment.
Hi @vlsi agreed, but the new version will still use index since there is insertion on the array given the index. Are you ok with this change?
Example:
int indexManager = 0;
for (KeyManager manager : managers) {
if (manager instanceof X509KeyManager) {
newManagers[indexManager] = new WrappedX509KeyManager((X509KeyManager) manager, keys);
} else {
newManagers[indexManager] = manager;
}
indexManager++;
}
There was a problem hiding this comment.
hi @vlsi I think it's best to keep classic for since we need index access inside loops. What do you think?
There was a problem hiding this comment.
Yeah, I missed the index is needed there.
However, managers variable does not mutate, so JVM should be able to inline managers.length here.
Frankly, I find managers.length much easier to read than the variant with an extra variable, and I bet extracting managers.length to a variable does not improve performance. So I would suggest keeping the old code as is.
| int trustManagersCount = trustmanagers.length; | ||
| for (int i = 0; i < trustManagersCount; i++) { |
There was a problem hiding this comment.
Same here, are you agree with new version with index?
int indexTrustManager = 0;
for (TrustManager trustManager : trustmanagers) {
if (trustManager instanceof X509TrustManager) {
trustmanagers[indexTrustManager] = new CustomX509TrustManager(
(X509TrustManager) trustManager);
}
indexTrustManager++;
}
|
There are three types of changes here:
I suggest we should cleanup 2&3 before merging the PR. |
…the loop was unnecessary, as `stack.size()` was already evaluated only once during loop initialization. The additional variable does not improve performance or readability.
| int selectedRowsCount = rowsSelected.length; | ||
| if (selectedRowsCount > 0 && rowsSelected[selectedRowsCount - 1] < table.getRowCount() - 1) { | ||
| table.clearSelection(); | ||
| for (int i = rowsSelected.length - 1; i >= 0; i--) { | ||
| for (int i = selectedRowsCount - 1; i >= 0; i--) { |
There was a problem hiding this comment.
Frankly, I think the performance gains are negligible here, and the change makes the code slightly harder to follow
There was a problem hiding this comment.
hi @vlsi I think the change improves readability and maintainability. and It does not affect functionality. Are you still agree with reverting changes?
| int selectedRowsCount = rowsSelected.length; | ||
| if (selectedRowsCount > 0) { | ||
| for (int i = selectedRowsCount - 1; i >= 0; i--) { |
There was a problem hiding this comment.
I suggest we revert the change as well as it hardly yields any performance gains, yet it makes the code harder to follow
| int selectedNodesCount = nodes.length; | ||
| for (int i = selectedNodesCount - 1; i >= 0; i--) { |
There was a problem hiding this comment.
Please revert as discussed previously.
| int copiedNodesCount = copiedNodes.length; | ||
| for (int nodeIndex = copiedNodesCount - 1; nodeIndex >= 0; nodeIndex--) { |
There was a problem hiding this comment.
copiedNodes.length was computed one-time only, there's no point in adding a variable
| int nodesCount = nodes.length; | ||
| for (int i = nodesCount - 1; i >= 0; i--) { |
Description
Caching the length/size in a variable for collections in order to avoid repeated calls within loops, which can slightly improve performance and readability.
Motivation and Context
While reading High Performance with Java (link), I came across a useful performance tip: it's recommended to cache the size of a collection before entering a loop, instead of calling .size() in the loop condition.
Instead of:
Prefer:
How Has This Been Tested?
Types of changes