swift 该死的派发机制

final static

oc类型 多态类型 静态类型 动态函数  静态函数

nsobject:

1、缺省不再使用oc的动态派发机制;

2、可以使用nsobject暴露出来的接口函数;

3、其它行为与swift的class一样;

多态类型:class与protocol

静态类型:值类型;

所有的函数都是静态绑定函数;

动态函数:

1、多态类型的函数

2、扩展中oc修饰的函数;

static与class 都有类型成员的含义;相对于实例成员;

static的另一个意思是静态派发;所以不能被继承。

要使用动态派发和继承的机制必须使用class继承。

https://www.cnblogs.com/feng9exe/p/10497659.html

@objc vs @objc dynamic官方解释

Overriding non-@objc declarations from extensions is not supported

There are two keywords to keep in mind when dealing with interoperability:

  • @objc means you want your Swift code (class, method, property, etc.) to be visible from Objective-C.
  • dynamic means you want to use Objective-C dynamic dispatch.

https://www.cnblogs.com/feng9exe/p/9460336.html

理解Swift的性能

理解Swift的性能,首先要清楚Swift的数据结构,组件关系和编译运行方式。

  • 数据结构

Swift的数据结构可以大体拆分为:Class,Struct,Enum。

  • 组件关系

组件关系可以分为:inheritance,protocols,generics。

  • 方法分派方式

方法分派方式可以分为Static dispatch和Dynamic dispatch。

https://www.cnblogs.com/feng9exe/p/10310050.html

Existential Container

Existential Container是一种特殊的内存布局方式,用于管理遵守了相同协议的数据类型Protocol Type,这些数据类型因为不共享同一继承关系(这是V-Table实现的前提),并且内存空间尺寸不同,使用Existential Container进行管理,使其具有存储的一致性。

Existential Container的构成

结构如下:

  • 三个词大小的valueBuffer 这里介绍一下valueBuffer结构,valueBuffer有三个词,每个词包含8个字节,存储的可能是值,也可能是对象的指针。对于small value(空间小于valueBuffer),直接存储在valueBuffer的地址内, inline valueBuffer,无额外堆内存初始化。当值的数量大于3个属性即large value,或者总尺寸超过valueBuffer的占位,就会在堆区开辟内存,将其存储在堆区,valueBuffer存储内存指针。
  • value witness table的引用 因为Protocol Type的类型不同,内存空间,初始化方法等都不相同,为了对Protocol Type生命周期进行专项管理,用到了Value Witness Table。
  • protocol witness table的引用 管理Protocol Type的方法分派。

内存分布如下:

Protocol Witness TablePWT

为了实现Class多态也就是引用语义多态,需要V-Table来实现,但是V-Table的前提是具有同一个父类即共享相同的继承关系,但是对于Protocol Type来说,并不具备此特征,故为了支持Struct的多态,需要用到protocol oriented programming机制,也就是借助Protocol Witness Table来实现(细节可以点击Vtable和witness table实现,每个结构体会创造PWT表,内部包含指针,指向方法具体实现)。

Point and Line PWT

Value Witness TableVWT

用于管理任意值的初始化、拷贝、销毁。

VWT use existential container

  • Value Witness Table的结构如上,是用于管理遵守了协议的Protocol Type实例的初始化,拷贝,内存消减和销毁的。
  • Value Witness Table在SIL中还可以拆分为%relative_vwtable和%absolute_vwtable,我们这里先不做展开。
  • Value Witness Table和Protocol Witness Table通过分工,去管理Protocol Type实例的内存管理(初始化,拷贝,销毁)和方法调用。

https://www.cnblogs.com/feng9exe/p/10310050.html

Revealed in more detail in Session 416 of WWDC 2016, the container is a wrapper around parameters adhering to a protocol and is used non-generically. The container functions as a box of fixed size (we’ll get back to this in a sec), thus allowing all adherers of a protocol to be of the same size, which is necessary for them to be used interchangeably.

var vehicles: [Drivable]

The fixed size of the container also allows us to store classes/structs that adhere to a protocol in an array of type protocol (as seen above), since the elements are now of the same size and can be stored in contiguous memory.

https://www.cnblogs.com/feng9exe/p/10309983.html

@dynamicMemberLookup

https://www.cnblogs.com/feng9exe/p/9959824.html

These instructions perform dynamic lookup of class and generic methods.

The class_method and super_method instructions must reference Swift native methods and always use vtable dispatch.

The objc_method and objc_super_method instructions must reference Objective-C methods (indicated by the foreignmarker on a method reference, as in #NSObject.description!1.foreign).

https://www.cnblogs.com/feng9exe/p/9682336.html

swift 该死的派发机制--待完成的更多相关文章

  1. swift派发机制的核心是确定一个函数能否进入动态派发列表

    swift派发机制的核心是确定一个函数能否进入动态派发列表

  2. 深入理解 Swift 派发机制

    原文: Method Dispatch in Swift作者: Brain King译者: kemchenj 译者注: 之前看了很多关于 Swift 派发机制的内容, 但感觉没有一篇能够彻底讲清楚这件 ...

  3. swift 即使不使用oc的动态派发机制也应该借鉴isa类型识别机制

    目前的消息派发机制真的很鸡肋. 简直是一堆狗屎. 类型信息中包含所有需要动态派发的函数:这个包含两类:类和protocol: 在编译时,首先搜索动态派发列表: 动态派发列表没有,在搜索静态派发列表: ...

  4. [置顶] Android源码分析-点击事件派发机制

    转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/17339857 概述 一直想写篇关于Android事件派发机制的文章,却一直没 ...

  5. Android源码分析-点击事件派发机制

    转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/17339857 概述 一直想写篇关于Android事件派发机制的文章,却一直没 ...

  6. Android开发学习之路-Handler消息派发机制源码分析

    注:这里只是说一下sendmessage的一个过程,post就类似的 如果我们需要发送消息,会调用sendMessage方法 public final boolean sendMessage(Mess ...

  7. Swift中实现Observable机制

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51917539 ...

  8. [Swift]LeetCode146. LRU缓存机制 | LRU Cache

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  9. Vue2.0父子组件间事件派发机制

    从vue1.x过来的都知道,在vue2.0中,父子组件间事件通信的$dispatch和$broadcase被移除了.官方考虑是基于组件树结构的事件流方式实在是让人难以理解,并且在组件结构扩展的过程中会 ...

随机推荐

  1. HashMap源码解读(JDK1.7)

    哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表,而HashMap的实现原理也常常出 ...

  2. SpringBoot之使用Lettuce集成Redis

    一.Lettuce Redis这里就不多说,服务端的启动之前的博客里面也有提到,这里略过.Lettuce和Jedis都是连接Redis Server的客户端程序,Jedis在实现上是直连redis s ...

  3. MAC ACL、RACL和VACL

    拓扑结构: 配置IP地址.VLAN及路由: SW1(config)#int range f0/1 - 2SW1(config-if-range)#switchport mode accessSW1(c ...

  4. .6-浅析webpack源码之validateSchema模块

    validateSchema模块 首先来看错误检测: const webpackOptionsValidationErrors = validateSchema(webpackOptionsSchem ...

  5. windows BLE编程 net winform 连接蓝牙4.0

    winform 程序调用Windows.Devices.Bluetoot API 实现windows下BLE蓝牙设备自动连接,收发数据功能.不需要使用win10的UWP开发. 先贴图,回头来完善代码 ...

  6. SqlServer 登录和卸载

    一.数据库简介 SQLServer环境配置 安装好数据库以后怎么启用sa账号来访问数据库. 1.先用windows账号登录数据库. 2.启用windows身份验证方式和sql server身份验证方式 ...

  7. 【Java深入研究】7、ThreadLocal详解

    ThreadLocal翻译成中文比较准确的叫法应该是:线程局部变量. 这个玩意有什么用处,或者说为什么要有这么一个东东?先解释一下,在并发编程的时候,成员变量如果不做任何处理其实是线程不安全的,各个线 ...

  8. SSL连接并非完全问题解决

    教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步>>> (原文)SSL 连接并非完全安全问题解决. 更多讨论或者错误提交,也请移步. 最近拿到了 TrustAsia ...

  9. 手把手教你实现Confluence6.7.1安装与破解

    Confluence是一个专业的企业知识管理与协同软件,也可以用于构建企业wiki. 一.准备工作 下载confluence6.7.1 wget https://downloads.atlassian ...

  10. css3 Box model 与 Box-sizing

    1.Box Model(盒模型) CSS中的Box Model分为两种:第一种是W3C的标准模型,另一种是IE的传统模型.它们的相同之处是对元素的width.height.padding.border ...