我用 Swift 测试了一些 一种滋滋作响的感觉,发现它只在 NSObject 是超类(直接或进一步)或使用“@objecc”修饰时才能工作。否则,它将遵循静态和 vtable 分派风格,如 C + + 。
定义一个没有 Cocoa/NSObject 基类的 Swift 类正常吗?如果我担心的话,这意味着放弃了 Objective-C 的许多动态性,比如方法拦截和运行时自省。
动态运行时行为位于特性的核心,比如属性观测器、核心数据、 面向方面编程、 高阶信息、分析和日志框架等等。
使用 Objective-C 的方法调用风格为一个方法调用添加了大约20个机器代码操作数,因此在某些情况下(对带有小主体的方法的许多紧凑调用) C + + 风格的静态和 vtable 分派可以执行得更好。
但是考虑到一般的95-5规则(95% 的性能增益来自5% 的代码调优) ,从强大的动态特性开始并在必要的地方变硬不是很有意义吗?