Pure methods are those without side effects: their only effect is to return a value which is a function of their arguments.
Two calls to the same pure method with the same arguments will return the same value. So, given two calls to a pure method with identical arguments, can HotSpot optimize away the second call, simply re-using the value from the first call?
For example:
int add(int x, int y) {
return x + y;
}
int addTwice(int x, int y) {
return add(x, y) + add(x, y);
}
If HotSpot doesn't inline add
inside addTwice
does it understand that add
is pure and thus call add
only once and double the return value?
Of course, such a trivial [mcve] isn't likely to be of direct interest, but similar situations can occur in practice due to inlining, divergent control flow, auto-generated code, etc.