• Post Reply Bookmark Topic Watch Topic
  • New Topic

Construction optimized

 
Michael Breuer
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will initialising of a classes fields with default-values be optimized by the compiler or does ist cost performance ?

e.G.

[ November 19, 2007: Message edited by: Michael Breuer ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my old copy of Java Performance Tuning, by Shirazi, it advises one to avoid unnecessary initialisations. However, this same book has a variety of advice that is not correct for today's JVMs; for instance, it is keen on avoiding constructing and destructing objects, whereas modern JVMs are so good at this that the advice is now inappropriate.

Despite being unsure of its importance today, I generally write code to avoid unnecessary initialisations, but comment the default initialisation: -



I have not actually checked to see if the latest Java still generates additional bytecode, for unnecessary initialisations. Perhaps someone would like to do that check...?

Even if unnecessary bytecode is generated, I would guess that HotSpot will optimise it away, in frequently-executed pieces of code. And, of course, in infrequently-executed code, it doesn't matter.

Overall, not an issue of great importance, but I would be interested to hear from anyone who knows for sure whether it is still relevant at all.
[ November 19, 2007: Message edited by: Peter Chase ]
 
Alexander Bischof
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

the overhead in the byte code is still generated but i don't know if it less performant. For my own i'm trying to avoid it.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will initialising of a classes fields with default-values be optimized by the compiler or does ist cost performance ?


When the JVM grabs a hunk of memory for a new object it gets zeroed out - thus creating the default values for all variable types. This has nothing to do with your program or the compiler. Adding o = null only takes time (unless the compiler optimizes it away) and contributes nothing. Local variables are of course a different story.

Bill
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William,

I think we all understood what it was about. The two questions were: -

(a) do the latest compilers still generate bytecode for unnecessary initialisations, where the source code includes them? This seems to have been answered, in the positive.

(b) does HotSpot optimise away the unnecessary initialisations? No definite answer here, yet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!