Handler invocation speed: Objective-C vs virtual functions

Posted by Kerido on Stack Overflow See other posts from Stack Overflow or by Kerido
Published on 2010-03-13T15:07:07Z Indexed on 2010/03/13 15:15 UTC
Read the original article Hit count: 471

Filed under:
|
|
|

I heard that calling a handler (delegate, etc.) in Objective-C can be even faster than calling a virtual function in C++. Is it really correct? If so, how can that be?

AFAIK, virtual functions are not that slow to call. At least, this is my understanding of what happens when a virtual function is called:

  1. Compute the index of the function pointer location in vtbl.
  2. Obtain the pointer to vtbl.
  3. Dereference the pointer and obtain the beginning of the array of function pointers.
  4. Offset (in pointer scale) the beginning of the array with the index value obtained on step 1.
  5. Issue a call instruction.

Unfortunately, I don't know Objective-C so it's hard for me to compare performance. But at least, the mechanism of a virtual function call doesn't look that slow, right? How can something other than static function call be faster?

© Stack Overflow or respective owner

Related posts about c++

Related posts about objective-c