1. 程式人生 > >swift 該死的派發機制--待完成

swift 該死的派發機制--待完成

ember 初始化 分享 container app indicate proto log can

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 該死的派發機制--待完成