Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

11995 Posts in 1588 Topics- by 3508 Members - Latest Member: PienueDut

26. May 2012, 11:25:14 am
Xith3D CommunityXith3D InternalsDeveloper discussion (Moderators: Marvin Fröhlich, 'n ddrylliog)fields initialized to default value
Pages: [1]
Print
Author Topic: fields initialized to default value  (Read 1107 times)
Tiamat
Just dropped in

Offline Offline

Posts: 2



View Profile
« on: 05. May 2009, 01:31:31 pm »

Hi everyone,

while browsing through the sourcecode, I noticed that almost everywhere fields are initialized to their default value, e.g.
Code:
class something{
  private boolean bla = false;
  private int x = 0;
...

This is redundant and may not be optimized by all compilers, causing the fields to be set twice. Initializing fields to their default value should better not be done in Java.

Here is a more thorough explenation with a bytecode example:
http://www.javapractices.com/topic/TopicAction.do?Id=14

And here is a table with the default values for Java types:
http://www.janeg.ca/scjp/lang/defaults.html

I would suggest removing default value initializations whenever possible.
Logged

Only a dead bug is a good bug
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #1 on: 05. May 2009, 05:22:35 pm »

I really don't think, that this would improve performance in any way. There aren't many instances created (if any) during the rendering process. So this doesn't hurt a bit. And setting the start value explicitly is cleaner.

Marvin
Logged
Tiamat
Just dropped in

Offline Offline

Posts: 2



View Profile
« Reply #2 on: 05. May 2009, 05:45:03 pm »

The performance gain is quite possibly neglible. I seriously don't know. But the Sun compiler (and possibly others) does produce duplicate code that way, possibly making the Xith3D Jar a few K larger than it should be.

Where I work the size of our libaries matters quite a bit, because they have to be downloaded by our users every time something is updated... so maybe I'm a little bit paranoid about keeping it compact.   Wink

edit: I guess the performance gain depends on the type of object that is used. If you have a singleton class, those few duplicate opcodes resetting the default value will of course be completely meaningless. But if you have a situation where you create many objects in a short amount of time, those extra duplicate instructions probably will make a difference. And since Xith3D is about game-development, and game-dev is about performance, I think it should strive to be as performant as possible.
« Last Edit: 05. May 2009, 05:54:58 pm by Tiamat » Logged

Only a dead bug is a good bug
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #3 on: 05. May 2009, 07:00:03 pm »

I do agree, that xith should be as performant as possible. But I really don't think this would boost performace by any noticable amount. So I prefer to have nice code, which isn't noticeable slower.

And I don't think, the jar sice would be so much smaller. It is only about 2 MB of size and there is a lot of code in it. I guess, removing all those default initializations would not even bring us 1 kb. So we shouldn't worry about it.

The Xith3D philosophy is not about creating thousands of instances at runtime, but creating them at loading time and then just using them.

If you give me an example, where performance is increased by far and where the (compressed) jar size is meaningfully smaller, I would be open for this.

Marvin
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic