Unit Type

Unit Type

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

Post Reply
zerver
Spring Developer
Posts: 1358
Joined: 16 Dec 2006, 20:59

Unit Type

Post by zerver »

I can't seem to find a way to detemine unit type other than using dynamic_cast. Is this correct? If so, I think we desperately need a flag in the Unit base class to tell which type of unit it is.

Code: Select all

if(unit->unitType == CUnit::FACTORY)
  CFactory *fac = (CFactory *)unit;
Dynamic_casts are slow and ugly...
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: Unit Type

Post by imbaczek »

they aren't THAT slow in our case, we have a very flat hierarchy, so it's usually one-two lookups away. i don't believe there's any significant speedup to gain by removing dynamic_cast and IIRC profiles confirm that.
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: Unit Type

Post by Tobi »

Are dynamic casts really slower then an if and a static_cast? Do you have measurements to support that? Do the dynamic_casts occur in time critical code in which any speed difference actually matters?

After all an if and a static_cast is pretty much the only thing the compiler has to generate to implement a dynamic_cast.

I don't think dynamic_casts are (can be) uglier then static_cast or const_cast, and those seem to be introduced into the codebase, rather then removed, so better keep that consistent. Either remove all C++ style casts or also keep the dynamic_casts.
zerver
Spring Developer
Posts: 1358
Joined: 16 Dec 2006, 20:59

Re: Unit Type

Post by zerver »

Maybe it's compiler dependent, but this

http://www.nerdblog.com/2006/12/how-slo ... ccast.html
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: Unit Type

Post by imbaczek »

the benchmark is interesting but rather useless - it doesn't compare gcc and, more importantly, omits code... speed of dynamic_cast is very dependent on the class hierarchy it has to traverse.
zerver
Spring Developer
Posts: 1358
Joined: 16 Dec 2006, 20:59

Re: Unit Type

Post by zerver »

Right, I'll test myself with gcc then.
zerver
Spring Developer
Posts: 1358
Joined: 16 Dec 2006, 20:59

Re: Unit Type

Post by zerver »

Tested. Dynamic_cast is about 10 times slower with gcc.

So, I'm making a unitType member.
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: Unit Type

Post by Tobi »

What about a more generic solution?

E.g. an objectType member in CObject or CWorldObject? (There are more things that use dynamic_cast then units...)
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: Unit Type

Post by imbaczek »

I'm not sure it's worth your time to change it all across the code. I'd like to see a profile of a /skipped replay before and after to see if it was indeed worth it.
zerver
Spring Developer
Posts: 1358
Joined: 16 Dec 2006, 20:59

Re: Unit Type

Post by zerver »

The CommandAI + LUA may benefit a bit but you're probably right there is not much to gain at all. I'll see if there's something more urgent...
Post Reply

Return to “Engine”