F
Frank
Just curious if anyone knows how well a compiler can optimize a variable
once it has been declared final. I know that, final declarations for
classes can be optimized to allow faster method invocation, and final
fields can be optimized especially with regard to threading
implementations.
Further, a good compiler should recognize that
for (long i=0; i<i_max; i++) {
int x0=//something
//do stuff here
}
should get compiled as
{
int x0;
for (long i=0; i<i_max; i++) {
x0=//something
//do stuff here
}}
to save stack activity. However, I recently found it usefull to start
writing some code as:
for (long i=0; i<i_max; i++) {
final int x0=//something - final just for the scope of the loop
//do stuff with (but not to!) x0 here
}
Can anyone comment on how this might affect the performance of this loop?
Also, sometimes it usefull to declare method parameters as final:
public void myMethod(final int x) {...}
I guess I'm just wondering what kind of optimizations the compiler can (or
can't) do with these declarations... and if it sees that a non-final
parameter is not changed, will it optimize the parameter as final? Or is
final, in this case, nothing more than a checkpoint to force an error on
assignment?
Thanks,
Frank
once it has been declared final. I know that, final declarations for
classes can be optimized to allow faster method invocation, and final
fields can be optimized especially with regard to threading
implementations.
Further, a good compiler should recognize that
for (long i=0; i<i_max; i++) {
int x0=//something
//do stuff here
}
should get compiled as
{
int x0;
for (long i=0; i<i_max; i++) {
x0=//something
//do stuff here
}}
to save stack activity. However, I recently found it usefull to start
writing some code as:
for (long i=0; i<i_max; i++) {
final int x0=//something - final just for the scope of the loop
//do stuff with (but not to!) x0 here
}
Can anyone comment on how this might affect the performance of this loop?
Also, sometimes it usefull to declare method parameters as final:
public void myMethod(final int x) {...}
I guess I'm just wondering what kind of optimizations the compiler can (or
can't) do with these declarations... and if it sees that a non-final
parameter is not changed, will it optimize the parameter as final? Or is
final, in this case, nothing more than a checkpoint to force an error on
assignment?
Thanks,
Frank