Objective-C message call efficiency

Member
Posts: 254
Joined: 2005.10
Post: #1
Well the Objective-C vs. C++ thread reminded me of and issue I've always been curious about. In Objective-C telling the compiler what type of object you are sending the message to is supposed to increase the speed of the program right? (ie using myClass variables instead of id variables) Does anyone know what kind of speed benefits we are talking about? How about referring to a variable by a super type (ie animal instead of dog)?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
I don't really know how Obj-C handles method calls, but I would doubt that it is the case. It's more probable that specifying the class as the type simply allows the compiler to do some additional type checking for you.

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: 3,579
Joined: 2003.06
Post: #3
I don't think that's all there is to it. From what I've read, I too have been under the impression that the runtime environment deals with known types faster/better than id (unspecified) types. I hardly ever use id anyway though...
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
Every ObjC message send winds up as a call to objc_msgSend, or one of its variants. The static type of the receiver makes absolutely no difference.

The only benefit of static typing is the "no such method" errors at compile time instead of run-time.

If you're targeting 10.4+, there's a "fast ObjC message dispatch" compiler option which you can turn on for a substantial performance improvement across the board.
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #5
OneSadCookie Wrote:The only benefit of static typing is the "no such method" errors at compile time instead of run-time.
Alrighty then, OSC sets it straight and I stand corrected! (as per the norm)

OneSadCookie Wrote:If you're targeting 10.4+, there's a "fast ObjC message dispatch" compiler option which you can turn on for a substantial performance improvement across the board.
How does that happen? Not to be lazy, but do you have an ADC link handy?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #6
People say the similar things about the 'final' keyword in Java.

I was part of a research project that found that to be largely untrue. By following that wisdom, all people are doing is making their class hierarchies static and impossible to extend.

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: #7
Final in Java could, at least theoretically, be used to speed things up (I don't know enough about the bytecode to say if it can help there, but it definitely can help in the JIT). ObjC is more dynamic than Java, so such optimizations are even less possible. +poseAsClass: is particularly difficult.

On 10.3 and below, the calls to objc_msgSend must go the normal route of a call to a function in a dynamically-linked library, which involves (at best) at least one additional jump than a regular function call.

Mac OS X 10.4 introduces the concept of the "commpage", which stores (processor-optimized) versions of common routines (memcpy, objc_msgSend, etc.). What "fast ObjC message dispatch" does is emit a call to the objc_msgSend on the commpage, which is at a fixed address, and therefore doesn't pay the penalty of a call into a dynamic library.

Incidentally, one of the benefits of the aglMacro/CGLMacro OpenGL optimizations is that they avoid the call into they dynamic library. (The other, probably more significant one is that they avoid looking up the current context).
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #8
OneSadCookie Wrote:Mac OS X 10.4 introduces the concept of the "commpage", which stores (processor-optimized) versions of common routines (memcpy, objc_msgSend, etc.). What "fast ObjC message dispatch" does is emit a call to the objc_msgSend on the commpage, which is at a fixed address, and therefore doesn't pay the penalty of a call into a dynamic library.
I'm still open for a relevant link if it exists...
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #9
Check the Xcode 2.0 release notes, I guess. I don't know precisely where to look, I'm sorry.
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #10
OneSadCookie Wrote:If you're targeting 10.4+, there's a "fast ObjC message dispatch" compiler option which you can turn on for a substantial performance improvement across the board.
why wouldnt *everybody* want that checked?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #11
Because the code it generates will crash on 10.3.9 or less...
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  simple function call not working burrows111 2 3,009 Mar 30, 2010 04:37 PM
Last Post: michaeln1978
  Applescript: Get message content from Mail Yoda 2 4,370 Apr 1, 2006 11:52 AM
Last Post: Yoda