marwen Bakkar wrote:
How is field read more than once in the case where it's already initialized? And how is it 25x slower?
The field would be read first time in the first
if statement and second time in the
return statement. The field is declared
volatile, which means it has to be refetched from memory every time it is accessed (roughly speaking, even more processing might be required to access
volatile variables) and can not be stored into a register by the compiler. When copied to the local variable and then used in both statements (
if and
return), the register optimization can be done by the JVM.
25
percent faster means executing in about 0.75 of the time compared to the slower code, not (1/25) of the time.