-
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++;
}
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 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.
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