Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective-c: is method calling inside a class still with selectors or pure calls?

I am comparing different programming languages and development platforms. One important difference between objective-c and other languages is that it uses selectors, messages, so calling objc_msgSend every time, crossing the boundary of shared library, so introducing a measurable overhead, plus further overhead (little in the cached case) for the objc_msgSend internal functioning, like explained here: http://www.mulle-kybernetik.com/artikel/Optimization/opti-3.html I am not an expert so I have to trust this article, found on this forum. On official Apple guide I find that "dot syntax" is just "syntactic sugar", that is the same method calling mechanism is used. Question: I would like to know if, instead, pure calls are performed inside a class instance, where it would be a waste to call objc_msgSend. That is when one class instance's method is called from a same class instance's method. Thanks

like image 446
P5music Avatar asked Dec 06 '25 18:12

P5music


1 Answers

Consider this situation in a hypothetical OOP language:

class A {
     say_something(){
         print("A!")
     }
     do_something(){
         say_something()
     }
}

and

class B : extends A {
     say_something(){
         print("B!")
     }
}

Now suppose you have an instance of B, and call do_something :

B b;
b.do_something();

Should b print A or B? If the call to say_something in the implementation of do_something goes through the vtable (in C++) or obj_msgSend (in Obj-C), it will print B, but if the compiler decides a call to a method in the same class should call to the method in the same class, it will print A.

Which is desirable depends on the situation. So, in C++, you can have a choice, by marking a member function virtual or not. In Objective-C, every method is virtual.

So, even in C++, the optimization you suggested is not done to virtual methods. I won't say a language without virtual methods a proper OOP language.

Anyway, now you might worry it would degrade the performance of an Obj-C app. Apple knows the danger, so they have lots and lots of optimization. For example, in a recent runtime, objc_msgSend lives at a fixed address, so that you don't first need to go through the shared-library boundary.

The Mulle-Kybernetic article was also nice, but note also it's very dated: it's whopping ten years ago, which is in this business a pre-historic times. You can read about recent optimizations put in to the implementation of objc_msgSend in bbum's blog posts or Hamster's blog posts.

If you are writing codes managing UI, using the standard messaging (i.e. objc_msgSend behind the scenes) is perfectly adequate. It even works very smoothly on mobile devices! So, don't do a premature optimization by getting the IMP, etc., until it turns out to be absolutely necessary after measuring the performance of a code.

If you're writing a calculation-intensive code involving, say, tens of thousands of particles moving, yes I would advice against using the Obj-C messaging framework. In that case, just simply write a C code (which can always be used in Objective-C ) or C++ code (which can be used in Objective-C++.)

like image 153
Yuji Avatar answered Dec 08 '25 07:12

Yuji



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!