• Post Reply Bookmark Topic Watch Topic
  • New Topic

JVM Optimization

 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know if there is site which documents the Hotspot code optimization?. If I have a code like this

boolean first=true;
MyObject obj1=null;
for(i=0;i<100;i++)
{
if(first)
{
obj1 = new MyObject();
first = false;
}
//do more thing
}

to something like?
MyObject obj1 = new MyObject();
for(i=0;i<100;i++)
{
//do more thing
}

I don't mean exact steps however if I have a condition which is executed only once inside the loop will the condition be swaped before the loop?
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without beeing familiar with the hotspot-compiler, I can't imagine it will resort your first example.
And the hotspot optimization varies from release to release, so we can't give a final answer.

If you use an anonymous block, you reduce the visibility of obj1 to the bare minimum and avoid the (cheap) execution of 100 if-statements.


Note: please use code-tags as described here:
http://www.javaranch.com
[ May 31, 2006: Message edited by: Stefan Wagner ]
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan.. but putting them in anonymous block makes the whole code as "dead block" which JVM may not even have to execute. I know it's bad programming but should programmer avoid this or can JVM take care of optimizing this is what I am looking for.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
? dead block?
Why should the JVM not execute it?

With 'anonymous block' I mean a block, surrounded by '{' and '}', without a controlling statement like 'do' or 'if' before the opening brace.
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you consider this


Nothing is happening inside the code to change the state of the system. It's basically no effect code. Would it make sense to execute this code?
[ May 31, 2006: Message edited by: Purushothaman Thambu ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Purushothaman Thambu:
Would it make sense to execute this code?


But your original code was quite different -- it included a constructor call and some other unknown code. That can't be optimized away. Talking about compiler optimizations is tricky enough without changing the code under discussion half-way through!
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest, The orginal code is about avoiding 99 conditional checks. What will happen if a conditional block is true only at first iteration. I am not clear how anonymous block will improve things. The second anonmyous block code can be considered as a dead block since the code is not changing the state of the system (Heap) but just consumes the cpu cycles.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea why Stefan recommended that to you, but it's not really related to your original question, so let's forget about it for now.

I'd guess for the specific case you've shown that Hotspot might indeed be smart enough to do the optimization you've shown. But remember that Hotspot won't do anything until it sees that a particular method is being called frequently enough to warrant optimization, so it's generally best to do these kinds of code-motion optimizations yourself if they don't obfuscate the code -- and indeed, here it makes the code shorter and clearer.

Further, even if Hotspot is able to do this kind of thing, it probably couldn't do it if the loop upper limit is a variable or function return value; it's never good to try to second-guess undocumented software!
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The anonymous block is just to restrict the visibility of MyObject obj1 to the least possible size, while pulling it out of the loop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!