-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Caching the length/size in a variable for collections in order to avo… #6453
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
base: master
Are you sure you want to change the base?
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
|
||
if (log.isDebugEnabled()) { | ||
log.debug("JmeterKeyStore type: {}", keys.getClass()); | ||
} | ||
|
||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could it better be changed to for-each loop instead of factoring managersCount
variable?
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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.
Old code was reverted @vlsi
int trustManagersCount = trustmanagers.length; | ||
for (int i = 0; i < trustManagersCount; i++) { |
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.
WDYT of for-each loop?
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.
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 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.
old code reverted @vlsi
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert as discussed previously.
int copiedNodesCount = copiedNodes.length; | ||
for (int nodeIndex = copiedNodesCount - 1; nodeIndex >= 0; nodeIndex--) { |
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.
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--) { |
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.
Please revert
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