Reputation: 3038
I have code that all over the place(probably 20-30 instances) does this:
<widget>.setVisible((condition == VIEW) || (condition == EDIT));
Logically I do understand that the Java compiler should and probably will optimize this to calculate it up front and simply use the calculated boolean.
My question is is there any way to actually verify that this is the case?
CLARIFICATION
condition
is a private
class member with no way to modify it past construction.
Upvotes: 0
Views: 290
Reputation: 500267
is there any way to actually verify that this is the case?
You could try to verify this by looking at the bytecode disassembly, then at the machine code produced by HotSpot, etc.
However, I think this strategy is fraught with difficultly. It is also fragile, since the result depends on the compiler/JVM and on the exact nature of condition
(is it local? a class member? final
? volatile
?)
If you care about the optimization for performance reasons [1], my advice would be to perform it manually, by factoring out the common expression.
[1] I assume that you've profiled the code, and know for a fact that this is a bottleneck.
Upvotes: 1
Reputation: 160181
Disassemble the byte code.
That said, these kinds of optimizations may be done at run time, may depend on the declaration of condition
, depend on where the conditions are located, and so on. Tracking down optimizations such as this is non-trivial in all but the simplest use cases.
Upvotes: 0