• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code optimization with "final"  RSS feed

 
vikey Sharma
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have one problem here ,

We have one class called "GamePlan" has one method called "isCoreGame()" which is extensively used in project in lots of classes.

Now by default this isCoreGame() returns true only.

I want to optimize this code because in project other files lots of if(GamePlan.isCoreGame()){//do something}else{//do something} blocks are there.

I know that if constants are compile time known java compiler replace it in byte code and that way optimize it

So I did following changes :
1.Created one cosntant private static final boolean IS_CORE_GAME=true;
2.I makred "isCoreGame()" as final.

but looking at byte code i realize its not replacing the value of isCoreModel() in bytecode and also "else block" should be removed from byte code.

IF same thing i tried with "GamePlan.IS_CORE_GAME" its giving the expected output in bytecode ( i.e replace its value with true and remvoing "else block" code).

Any body has any idea why its not putting "isCoreGame()" inline...and replacing its value ???
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Similar discussion here on the sun forum.
Basically, javac does not do any optimization. All optimizations are done by the Hotspot JVM. (This is what i got from the discussion on that forum)

However, apart from the academic reasons, why do you want to make sure that your method call is inlined?
I dont think this will have an effect on the over all performance of your system. You would not even notice the time spent for this over other *important* pieces of your code.

"Premature optimization is a root of all evils"
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inlining of final methods happens at runtime by the JVM, not at compile time by the compiler.

In fact, modern JVMs also can inline non-final methods. So I don't think there is much reason to worry about it at all.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!