These days I have been working on performance measurements of code sequences. In this context I noticed that the where-used list of the Orchestrator, Find Usages and Find Dependencies, does not resolve variablized method calls of getModule.
Variablized Modules, a Blind Spot of Find Usages/Dependencies
Here an example. At first I created an Action testStefan001, which delivers an everyday term:
At the second step I created an Action testStefan002, which calls this action on two different ways:
var ret = System.getModule("de.stschnell").testStefan001();
var module = System.getModule("de.stschnell");
for (var i = 0; i < 5; i++) {
ret += module.testStefan001();
}
ret += System.getModule("de.stschnell").testStefan001();
return ret;
|
The first way is the direct call and the second way is an indirect way via an object variable. The second way offers a better performantce, because the instantiation is done only once, before the loop. So far so good. Now let's take a look at the where-used list.
The calling module shows only the lines in which the call is made directly.
The called module also shows only the direct calls. The indirect method call, via a variablized module, is not considered.
Conclusion
As can be seen, the variabilization of the module has its advantages and disadvantages. When using the where-used list, we just have to be aware that the displayed lists do not have to be fully comprehensive. We may need to bring more code analysis techniques to use.
Addendum
The System.getModule method should not be used in loops because it is an expensive call.
for (var i = 0; i <= Infinity; i++) {
System.getModule("com.vmware.library...").getBambi();
}
|
Instead the call of the method System.getModule should be moved before the loop, so that it is executed only once. This can significantly improve performance when the loop runs through many iterations.
var module = System.getModule("com.vmware.library...");
for (var i = 0; i <= Infinity; i++) {
module.getBambi();
}
|
Using the indirect call in the following example results in a performance increase of nearly 22%.
/** Performance comparison between a direct call of getModule and an
* indirect call.
*/
var result = null;
// Direct call
const start1 = Date.now();
for (var i = 1; i < 50000; i++) {
result = System.getModule("com.vmware.library...").getModuleInvoked();
}
const end1 = Date.now();
System.log("Execution time 1: " + (end1 - start1) + " ms");
// Indirect call
const module = System.getModule("com.vmware.library...");
const start2 = Date.now();
for (var i = 1; i < 50000; i++) {
result = module.getModuleInvoked();
}
const end2 = Date.now();
System.log("Execution time 2: " + (end2 - start2) + " ms");
|