final, private, static and code inlining
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Rahul_Sawhney
Posted On:   Thursday, March 7, 2002 09:52 AM

Do final classes and private + static methods help in code inlining when compiled in optimzed mode using Sun's java compilers?
Where can i find more info?

Re: final, private, static and code inlining

Posted By:   Christopher_Schultz  
Posted On:   Thursday, March 7, 2002 11:21 AM

Although I've never read anything one way or the other, I'm pretty sure that using final, private, and static can offer very good optimization hints to the JIT.



Think about it: every 'final' class basically means that all methods are 'final'. If a method is final, it cannot be virtual. If it it not virtual, it is a static call. Static calls are pretty fast.



'static' methods are also static calls, because they are always implemented in one place: they cannot be overridden (they are implicitly 'final').



Finally, 'private' methods are also final, since they cannot be overridden, and convert into static calls.



With these hints, the JIT can highly optimize calls to these methods. If the methods are short enough (like in accessors and mutators), then can even be inlined, so that there is no overhead for the method call.



The downside to declaring methods or classes to be final is that you are limiting extensibility. I think it only makes sense to explicitly make a class (or method) final if you have an architectural reason for doing so.



Let the JIT authors do the dirty little tricks. :)



-chris
About | Sitemap | Contact