Does Swift use messaging for methods?

I am sure my terminology is disabled, here is an example:

  • C / C ++ has methods and virtual methods. Both have the ability to be inline at compile time.
  • C # CIL has call and callvirt (which are very similar to C ++ methods and virtual methods). Although almost all method calls in C # become callvirt (due to langauge snafu), the JIT compiler can optimize most back to call instructions, and then (if necessary) also include them.
  • Objective-C method calls are made differently (and inefficiently); the message object is passed through objc_msgsend every time you call the method, it is a dynamic submit form and can never be inlined.

Reading the function specification language for Swift, I don’t know if Swift uses the same messaging system as Objective-C or something else.

+6
source share
2 answers

Sometimes yes, sometimes no. If you have clean fast code and don’t show your classes / protocols before Objective-C with the @objc decoration, it seems that calls with a clean quick call are not sent via objc_msgSend , but in other cases they are. If the protocol accepted by your fast object is declared in Objective-C, or if the fast protocol is decorated with @objc , then method calls for protocol methods, even from fast objects to other fast objects, are sent via objc_msgSend .

The documentation is currently a bit thin; I'm sure there are other nuances ... but empirically speaking (i.e. I tried this) some quick method calls go through objc_msgSend and others not. I think that getting the best performance will depend on how clean and fast your code is and crossing the Obj-C / swift border as little as possible, as well as through bottleneck interfaces / protocols to limit the number of quick calls that should be Shipped dynamically.

I am sure that more detailed documents will appear sooner or later.

+11
source

Unlike C ++, there is no need to indicate that the method is virtual in Swift. The compiler will develop the following:

Performance indicators, of course, depend on the equipment.

  • Embed Method: 0 ns
  • Static Dispatch: <1.1ns
  • Virtual dispatch 1.1ns (for example, Java, C # or C ++ at assignment).
  • 4.9ns dynamic dispatch (e.g. Objective-C).

Objective-C, of ​​course, always uses the latter. 4.9ns overhead is usually not a problem, as this will represent a small fraction of the total execution time of the method. However, when necessary, developers can easily return to C or C ++ where necessary. This is still an option in Swift, however, the compiler will analyze which of the fastest can be used, and try to make a decision on your behalf.

One of the side effects of this is that some of the powerful features provided by dynamic dispatch may not be available, as, as previously assumed, this was the case for any Objective-C method. Dynamic dispatch is used to intercept a method, which in turn is used:

  • Cocoa property watchers.
  • Instrumentation device model CoreData.
  • Aspect Oriented Programming

With the latest version of Swift, even if the object is marked as "@objc" or extends NSObject, the compiler may still not necessarily use dynamic dispatch. There is a dynamic attribute that can be added to the method for failure.

+1
source

Source: https://habr.com/ru/post/970262/


All Articles