Can javac or Hotspot automatically add "final" as an optimization of invariant variables?

The consensus seems to be that the efficiency of using member variables is finite, since they never need to be reloaded from main memory. My question is: do javac or Hotspot automatically do this for me when it is obvious that the variable cannot change. for example, javac will make "x" final in this class below ...

public class MyClass { private String x; MyClass(String x) { this.x = x; } public String getX() { return x; } } 

At some secondary point, did anyone produce empirical evidence that marking members as finite makes code faster? Any advantage, of course, is negligible in any application making remote calls or database searches?

+5
java compiler-construction final jvm-hotspot
source share
4 answers

Like many performance improvements, it is usually best to ask; What is easier to understand and reason? for example, if the field is final, I know that it will not be changed anywhere. This often leads to more optimistic code, but, more importantly, it should be more convenient code.;)

Of course, I make any field that can be final as final. Personally, I would prefer final to be the default, and you had to use a keyword like var to make it mutable.

+4
source share

Allowing javac to do this would be a mistake. Since there may be code in another bank that can rely on compiled code (modularity), changing the code at compile time for optimization is not an option.

As for the second argument, โ€œnever need to be overloaded from main memoryโ€, remember that most instance variables are cached. final only indicates immutability, this does not guarantee volatility (volatile == always gets the latest from main memory). Therefore, the need for locks and the volatile keyword in a multi-threaded environment.

As for the hot spot case, I have no idea, and would like to know more about it. final constants can be inserted at compile time, resulting in moderate performance. Link to a question about embedding in java

Edit:

Note that the final one indicates immutability that needs to be taken with salt. It does not guarantee that the state cannot change, it only indicates that the reference to the object can be changed. final indicates immutable primitive data types
+2
source share

AFAIK, they donโ€™t do this, and thus you incur a minor penalty. However, this can be done automatically using IDE tools such as Eclipse Cleanup.

+1
source share

I believe that the modern JVM (Hotspot compiler) detects that the value does not change, so there is no performance benefit when creating final parameters or variables yourself. (If this is not the case, provide a link or test case.) There is one exception: constants ( static final ).

However, this may differ from the final methods and classes. This can improve performance in this case (I'm not quite sure in which cases). By the way, something that improves performance puts a bit of static (if possible) functions.

The problem with final is that it clutters the code. It would be nice if the final version was by default.

0
source share

All Articles