Swift 有动态调度和虚方法吗? [英] Does Swift have dynamic dispatch and virtual methods?

查看:40
本文介绍了Swift 有动态调度和虚方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 C++/Java/C# 背景,我期待在 Swift 中看到虚拟方法,但是阅读 swift 文档我没有提到虚拟方法.

Coming form a C++/Java/C# background I was expecting to see virtual methods in Swift, however reading the swift documentation I see no mention of virtual methods.

我错过了什么?

由于浏览量很大,我决定为最新且非常清晰/详细的答案提供奖励.

Due to large number of views, I have decided to offer a reward for an upto date and very clear/detail answer.

推荐答案

与 C++ 不同,Swift 中没有必要指定方法是虚拟的.编译器将确定要使用以下哪个:

Unlike C++, it is not necessary to designate that a method is virtual in Swift. The compiler will work out which of the following to use:

(性能指标当然取决于硬件)

  • 内联方法:0 ns
  • 静态调度:<1.1ns
  • Virtual dispatch 1.1ns(如指定的 Java、C# 或 C++).
  • Dynamic Dispatch 4.9ns(类似于 Objective-C).

Objective-C 当然总是使用后者.4.9ns 的开销通常不是问题,因为这仅占整个方法执行时间的一小部分.但是,在必要时,开发人员可以无缝回退到 C 或 C++.然而,在 Swift 中,编译器将分析可以使用哪个最快,并尝试代表您做出决定,支持内联、静态和虚拟,但保留消息传递以实现 Objective-C 互操作性.可以使用 dynamic 标记方法以鼓励消息传递.

Objective-C of course always uses the latter. The 4.9ns overhead is not usually a problem as this would represent a small fraction of the overall method execution time. However, where necessary developers could seamlessly fall-back to C or C++. In Swift, however the compiler will analyze which of the fastest can be used and try to decide on your behalf, favoring inline, static and virtual but retaining messaging for Objective-C interoperability. Its possible to mark a method with dynamic to encourage messaging.

这样做的一个副作用是,动态分派提供的一些强大功能可能不可用,因为以前可能会假设任何 Objective-C 方法都是这种情况.动态分派用于方法拦截,依次被使用:

One side-effect of this, is that some of the powerful features afforded by dynamic dispatch may not be available, where as this could previously have been assumed to be the case for any Objective-C method. Dynamic dispatch is used for method interception, which is in turn used by:

  • Cocoa 风格的属性观察器.
  • CoreData 模型对象检测.
  • 面向方面的编程

上述功能类型是由 后期绑定 语言提供的.请注意,虽然 Java 使用 vtable 调度进行方法调用,但它仍然被认为是一种后期绑定语言,因此能够凭借虚拟机和类加载器系统实现上述功能,这是提供运行时检测的另一种方法.纯"Swift(没有 Objective-C 互操作)就像 C++,因为它是一种直接到可执行的编译语言,具有静态调度,那么这些动态特性在运行时是不可能的.在 ARC 的传统中,我们可能会看到更多此类功能转移到编译时间,这在每瓦性能"方面具有优势 - 这是移动计算中的一个重要考虑因素.

The kinds of features above are those afforded by a late binding language. Note that while Java uses vtable dispatch for method invocation, its still considered a late binding language, and therefore capable of the above features by virtue of having a virtual machine and class loader system, which is another approach to providing run-time instrumentation. "Pure" Swift (without Objective-C interop) is like C++ in that being a direct-to-executable compiled language with static dispatch, then these dynamic features are not possible at runtime. In the tradition of ARC, we might see more of these kinds of features moving to compile time, which gives an edge with regards to "performance per watt" - an important consideration in mobile computing.

这篇关于Swift 有动态调度和虚方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆