Prev: inheriting a main method
Next: Adding int to a float
From: rossum on 15 Sep 2009 08:03 A question for the Java gurus. When coding in C or C++ I use 'register' to hint to the compiler that something might usefully be kept in a place with easy/quick access: for (int i = 0; i < 20; ++i) { register int iSquared = i * i; for (int j = 0; j < 250; ++j) { // Calculations involving i, iSquared and j } } That makes sense because the compiler is directly writing machine code and can make use of the hint if it wants to. In Java there is no direct equivalent of 'register', the closest probably is 'final' to indicate that the local variable will not change. The approximate Java equivalent is: for (int i = 0; i < 20; ++i) { final int iSquared = i * i; for (int j = 0; j < 250; ++j) { // Calculations involving i, iSquared and j } } However, in Java the compiler is creating byte code and the machine code is created by the JIT from the bytecode so there is a less direct connection between my source and the machine code. At last, my question: does the hint given by the 'final' keyword on a local variable get through to the JIT or is it just wasted typing? Some days I add a 'final' to my unchanging local variables, other days I can't be bothered. Which is the better practice? Is there likely to be any impact on execution speed? I am talking about local variables, not member variables. Thanks, rossum
From: Joshua Cranmer on 15 Sep 2009 08:41 rossum wrote: > At last, my question: does the hint given by the 'final' keyword on a > local variable get through to the JIT or is it just wasted typing? Pretty much everything about local variables is only seen by the compiler. The bytecode only "sees" local variables as a mere set of slots which some instructions write to or read from. That is, unless you compile with -g, which does save another attribute containing the variable names and types. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
From: markspace on 15 Sep 2009 09:16 rossum wrote: > > I am talking about local variables, not member variables. I was just re-reading in Java Concurrency in Practice that some modifiers are important, because the JIT/javac will hoist variables out of loops: volatile boolean done = false; while(!done) processData(); "done" will be hoist out of the loop with out the volatile modifier, resulting in an infinite loop. This only happens when optimized at runtime when you run with the -server flag. The -client flag won't preform that optimization. So I can't say for sure, but at least your intent might be more clear to a programmer reading your code, if you do use the final modifier. I'm guessing though that the JIT can spot final variables on it's own. Although perhaps the final flag might be useful when running with -client, where less optimization is done.
From: Patricia Shanahan on 15 Sep 2009 09:21 rossum wrote: > A question for the Java gurus. When coding in C or C++ I use > 'register' to hint to the compiler that something might usefully be > kept in a place with easy/quick access: .... Have you done before/after checks to see whether it is really helping? In some of my C code back in the early 1990's, measurements showed it was better to let the compiler make its own choices about what to put in registers when. I suspect C compilers have, if anything, got better since then. That goes even further for a Java system, because much of the optimization is done at run time by a JVM that can see what is really going on. Patricia
From: Lew on 15 Sep 2009 10:04
Patricia Shanahan wrote: > rossum wrote: >> A question for the Java gurus. When coding in C or C++ I use >> 'register' to hint to the compiler that something might usefully be >> kept in a place with easy/quick access: > ... > > Have you done before/after checks to see whether it is really helping? > > In some of my C code back in the early 1990's, measurements showed it > was better to let the compiler make its own choices about what to put in > registers when. I suspect C compilers have, if anything, got better > since then. > > That goes even further for a Java system, because much of the > optimization is done at run time by a JVM that can see what is really > going on. The reason to use 'final' on a local variable is, as should always be the case for all idioms, to document and enforce a design decision. Rumor has it that 'final' makes optimization easier for Hotspot, but come on, optimizing assignment to a local variable? Use 'final' on a local variable to enforce that it's /definitely assigned/ exactly once, or to make it usable in an inner class. -- Lew |