Deprecated: Assigning the return value of new by reference is deprecated in /home/w3hja33/public_html/wp-includes/cache.php on line 99

Deprecated: Assigning the return value of new by reference is deprecated in /home/w3hja33/public_html/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /home/w3hja33/public_html/wp-includes/theme.php on line 576
C1 | w3hJava

w3hJava

What, Why, When and How of Java, JavaFX and related technologies


Published June 4th, 2008

Final can improve performace !

Last month, me and Abhishek was discussing about some performance issue. Finally we came on a good conclusion and so I decided to write a blog. One of the Orkut users posted a question on our community

“As we know that by declaring method as final, we cant override them. By performing this function, it provides a performance enhancement but i unable to understand how ????? can anyone help me out…… “

(I don’t know why people are using so many of dots and question marks)

Yes this is correct, writing final (can) improve the performance. WHY ?
When you define a method final, that means it can’t be overridden into its derived classes. Knowing that into the fact complier (can) inline that method into its derived classes. But but but, this is not a safe way to inline a method. WHY AGAIN ?
Because, if by any chance if method will not be final (fool the VM) in base class, then inline entry in derived class will be wrong and things can be screwed. Now very rare but this is possible in Java. Remember in Java you can write your own ClassLoader and you can make a ClassLoader where you can ignore(any how) the final declaration.
So, for those compiler in which final method be inlined before loaded into VM(unlike JIT), there is quite a high risk and hence compiler can’t do this. But in JIT compiler, this is what is happening !

In 2 line, I want to write about JIT(Just in time compiler) - at the time of interpretation,interpreter saw there is a for loop, for say 50 times. It takes the code out from interpreter and give it to runtime compiler(c1 and c2 compiler). As we know compilation is faster than interpreter. Compiler do the work and then control goes back to Interpreter. Details are quite interesting.

And this is how we finally ended our discussion :). Correct me if I am wrong.

In next post, we will discuss how to write a custom ClassLoader.