Forums Register Login

JVM Optimization

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
? 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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
 

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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
The anonymous block is just to restrict the visibility of MyObject obj1 to the least possible size, while pulling it out of the loop.
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1172 times.
Similar Threads
sorting
hot spot optimization techniques?
Testing a loop
Session Object space problem
JUnit; is there a way to order tests?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 23:05:35.