C function performance boost

Member
Posts: 65
Joined: 2009.03
Post: #1
I'm looking to see if anyone has a view on the benefit of having a C function within an Objective-C class.

I know that calling methods inside an Objective-C class has a messaging overhead. When I run my game through Shark the most work by far is around messaging.

I have some regularly called functions defined as global C functions and I was wondering if I were to convert some of the most called methods inside my Objective-C classes to C Functions would actually speed things up any. The code would still be part of the Objective-C class.

Hope that makes sense and interested in peoples views.

Cheers

MikeD

iPhone Game Development Blog - 71Squared
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
C functions are faster yes, but only if you aren't calling other methods within the function. Before you go replacing all your methods with functions, you should profile which parts are using the most CPU. Using better algorithms is the best way to speed things up, but rewriting parts that make a lot of allocations or method calls will speed things up too.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #3
There's also a halfway solution. You can get an IMP for an Objective-C method (using +[NSObject instanceMethodForSelector:]), which is a C function pointer you can call for the method. Calling a function pointer isn't as fast as calling a C function directly, but it's faster than a full Obj-C message dispatch.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #4
ThemsAllTook Wrote:There's also a halfway solution. You can get an IMP for an Objective-C method (using +[NSObject instanceMethodForSelector:]), which is a C function pointer you can call for the method. Calling a function pointer isn't as fast as calling a C function directly, but it's faster than a full Obj-C message dispatch.

You sure about that? I think the difference between using a function pointer and a compiled reference to a function is pretty trivial.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
Core 2 Duo and more recent Intel chips can branch-predict indirect calls. Other hardware can't. That means that function pointer calls are significantly more expensive on non-C2D hardware, and only potentially more expensive on C2D hardware.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #6
Skorche Wrote:You sure about that? I think the difference between using a function pointer and a compiled reference to a function is pretty trivial.

I haven't measured recently, but if I'm remembering correctly, my numbers from testing a few years ago were that function pointer calls were 3 times slower than regular function calls, and Obj-C message dispatches were 7 times slower than regular function calls. Pretty easy to write a test app to measure on current hardware.
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #7
Sheesh... And here I was thinking all this time that my abuse of function pointers was of no real concern performance-wise. Glad to hear that Core2 Duo and newer stuff is pretty good with them though.
Quote this message in a reply
Member
Posts: 26
Joined: 2010.01
Post: #8
That's roughly in line with what I've seen, in terms of function pointers vs regular function calls. The difference is mainly down to the function pointer requiring a little more staging and potentially a code cache miss, while the regular function call is directly embedded and easy for the compiler to pipeline.

A C++ virtual function call should be slightly slower again than a function pointer, since it's basically an redirect to a table of function pointers, and then a call to one of those function pointers. But both times your mileage may vary according to what the cache is doing, or the inlining behaviour of the optimising compiler. A call to a function pointer, like a virtual, can never be inlined...
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #9
Hmm. I guess even with using function pointers everywhere in Chipmunk it never has come up as a sizable performance hit. I figured that it was pretty much an indirect jump instead of a direct one. (I don't remember the assemble terminology maybe)

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
yes, an indirect jump that if incorrectly predicted or not predicted will cause a large pipeline stall and potentially an instruction cache miss.
Quote this message in a reply
Member
Posts: 65
Joined: 2009.03
Post: #11
Thanks for the discussion guys, that has been useful. Based on your feedback I think I'll stick with making heavily used methods C functions, making sure of course they don't call other Objective-C methods from within.

This is mainly around things like my collision detection functions e.g. circle-to-circle, rectangle-to-circle etc.

As usual, the help on the forum never disappoints Grin

Thanks

MikeD

iPhone Game Development Blog - 71Squared
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #12
OneSadCookie Wrote:yes, an indirect jump that if incorrectly predicted or not predicted will cause a large pipeline stall and potentially an instruction cache miss.

I guess that makes sense. I figured that could be predicted pretty easily.

@MikeD Yup. Collision detection is a good place to do it. If you make the relevant instance variables of the classes public, then you can get all your numbers and do the collision computations without any expensive method calls. That should be a place where you can make it quite a bit faster.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Can I use BOOST on the iPhone? riruilo 1 1,804 Jan 22, 2009 12:38 AM
Last Post: bmantzey