c++ expert question

Member
Posts: 306
Joined: 2009.03
Post: #1
I will be porting a portion of my game engine to c++ assuming its at all succesful and I had a question. My object structure is quite large as an example

Object->Parent->Unit->Boat->Destroyer
or
Object->Child->Weapon->Projectile Weapon->Torpedo Launcher

When I port to c++ from what I understand of how it works, I will have to make most of my functions virtual to get the same kind of override functionality that the majority of my classes use. For example anyone who wants processing to happen each tick can override a tick() function. This is true for a lot of things which lets my engine be very adaptive to new stuff.

So is making everything virtual the way to go and do you see significant performance gains over objective c if everything is virtual(which is slower then non virtual). Any other comments are appreciated too.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #2
There is no generic answer, but an ObjC message dispatch should only be slightly slower than a C++ virtual method call. Don't expect huge gains for anything that can't be inlined.

Also, why on earth do you want to port it to C++?
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #3
Performance. I see obj c message send as using a big chunk of cpu on profiling.
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #4
kendric Wrote:I will be porting a portion of my game engine to c++ assuming its at all succesful and I had a question. My object structure is quite large as an example

Object->Parent->Unit->Boat->Destroyer
or
Object->Child->Weapon->Projectile Weapon->Torpedo Launcher

When I port to c++ from what I understand of how it works, I will have to make most of my functions virtual to get the same kind of override functionality that the majority of my classes use. For example anyone who wants processing to happen each tick can override a tick() function. This is true for a lot of things which lets my engine be very adaptive to new stuff.

So is making everything virtual the way to go and do you see significant performance gains over objective c if everything is virtual(which is slower then non virtual). Any other comments are appreciated too.

For high level object ( Destroyer, Torpedo) it won't make any difference since you are unlikely to have more than 20-30 of them anyway.
For low level stuff (like iterating vertex buffers or particles etc) you will have to arrange your code not to use virtual functions or any of that stuff anyway ( in other words, you will have to black-box the implementation within some convenient class and invoke it thru that class)

I use C++ because:

1. I am familiar with it
2. It lets me use OOP for low level, high frequency code (as long as I am careful and know exactly what kind of code will be generated)
3. There is a ton of high-perf third party code out there (std, boost, templated implementations of various algorithms).
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #5
kendric Wrote:Performance. I see obj c message send as using a big chunk of cpu on profiling.
In that case I assume a lot of what you call are accessors, the methods implement trivial functions, or you use ObjC objects for primitive math, such as dealing with vectors. There's a lot of ways to speed that up in ObjC, depending on what exactly your hotspot is, but again there's no generic answer.

The only benefit you get from C++ is that it can (with caveats) inline non-virtual methods, and instantiate objects on the stack. The cost is having to deal with the language's inherent idiocy.

OTOH, you can mix ObjC and C++, so it might be a good idea to try and factor out your math code, for example, if it is contained in ObjC methods, and use C++ classes instead of ObjC ones for those.
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #6
DoG Wrote:In that case I assume a lot of what you call are accessors, the methods implement trivial functions, or you use ObjC objects for primitive math, such as dealing with vectors. There's a lot of ways to speed that up in ObjC, depending on what exactly your hotspot is, but again there's no generic answer.

The only benefit you get from C++ is that it can (with caveats) inline non-virtual methods, and instantiate objects on the stack. The cost is having to deal with the language's inherent idiocy.

OTOH, you can mix ObjC and C++, so it might be a good idea to try and factor out your math code, for example, if it is contained in ObjC methods, and use C++ classes instead of ObjC ones for those.

Question. Once inside the body of an objective c function, why does it matter that you are doing math there rather then inside the body of a c/c++ function? Isn't math c code? pow,sqrt,*,+ etc?

The two biggest areas of churning in my app are ai and collision detection\movement. For #2 I am thinking about moving to chipmunk or some other engine. I haven't researched yet which ones are better but I need one that is fairly versatile. I do a lot of things like letting objects decide if they can collide with each other(for example friendly bullets disallow collisions with friendly targets). I have all this logic in a canCollide(withWho) type function. I don't like to do any of that logic in the collision function because it should be agnostic of that sort of thing. Other things I do are manipulate velocitys at various times.

I have moved away from NSArray on any performance heavy area to just pointer arrays. I do have a lot of very small function calls which are overriden by a few objects to provide specific behaviour. I also am using protocols a lot(though not using conformsToprotocol as that is very slow). So mainly I am using protocols to allow non specific object interaction in all my code. Because of that I have to use accessors rather then like obj->someVar.

I also see Box2D as a c++ physics engine. Anyone ever compare it to chipmunk, or another engine you like?
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #7
kendric Wrote:Question. Once inside the body of an objective c function, why does it matter that you are doing math there rather then inside the body of a c/c++ function? Isn't math c code? pow,sqrt,*,+ etc?

The problem is with the overhead of an objc-message. If all you do inside a function is "a+b", the performance overhead of dispatching a message is probably making that function exectute several orders of magnitude slower than an "inlined" addition, which is possibly just a single instruction.

As far as physics go, I can recommend Chipmunk, it can do all those things you mentioned, and more.

And as for NSArray, it depends a lot on the usage, how performant it is. I nowadays only need to resort to plain C arrays for vector math type primitives, which typically need to be packed arrays for use in OpenGL, as well.

You can possibly reduce the NSArray overhead by changing how you access and iterate over the array, as usage patterns common in C/C++ might not be optimal.
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #8
As a side note I got large performance boost from switching to the big pointer array rather then using the ns array in my main loop.
Quote this message in a reply
Post Reply