final boolean runF = ...; //In *this* program run, it works out to be false.
...
if (runF) {
f(x);
}
The JIT can just eliminate this branch entirely. More interestingly: if (cond) {
myVar = f(x); //f(x) has provably no side effects
}
Supposing cond is nearly always true, the JIT can optimistically run f and in parallel check cond. In the unlikely event that cond is false, the JIT will still compute the return value of f(x), but just not do the assignment.It surprised me when I saw all this happen, but the JVM's JIT is actually pretty smart and works very well for long running server processes.