Thursday, November 29, 2007

Performance - Final Keyword ?

Last night, 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. Straight away question - 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 ! (If you want to know more about JIT, please let me know :) ).

And this is how we finally ended our discussion :)

4 comments:

Vaibhav Bajpai said...

I have some doubts I would wish to get cleared on this topic

* in C++, compiler creates a virtual table for classes that have overriden methods (virtual methods) . I assume similar technique is used in Java, so if I use final the so called virtual table will not be created , so the methods would asymtotically operate efficiently in terms of space

* I fail to understand why all the methods in Java were "overridable" by default and requires a need of "final" for otherwise. Why not reverse the technique and make all methods not overridable by default, and allow a keyword to make them overridable ( C++ and C# both use virtual keywords to achieve this )

well, the advantage would be, most programmers tend to forget to write final everytime and it is tedious too

Vaibhav Choudhary said...

Coming on second question, I guess you made the answer : its pretty difficult to put final before everything but yes its true if method is final its better to write final before it.

Now on first question, no Java don't have any concept for VTable in the sense you talked there is something else called OOP(Ordinary Object Pointers) which we are using in Java. I will let you know more about this soon.

Anonymous said...

using final for methods does not guarantee performance. http://www.ibm.com/developerworks/java/library/j-jtp04223.html

Anonymous said...

Can anyone recommend the top Remote Management & Monitoring tool for a small IT service company like mine? Does anyone use Kaseya.com or GFI.com? How do they compare to these guys I found recently: N-able N-central remote pc access software
? What is your best take in cost vs performance among those three? I need a good advice please... Thanks in advance!