为什么 Apple 在 iOS 运行时对 class 类型结构中的方法列表使用双指针

Why Apple uses double pointers for method lists in class-type struct on iOS runtime

我读到在 iOS 运行时,class 变成结构(其他 oc 对象也是),如下所示:

struct objc_class {

    Class isa  OBJC_ISA_AVAILABILITY;

#if !__OBJC2__

    Class super_class                       OBJC2_UNAVAILABLE;  

    const char *name                        OBJC2_UNAVAILABLE; 

    long version                            OBJC2_UNAVAILABLE;  

    long info                               OBJC2_UNAVAILABLE;  

    long instance_size                      OBJC2_UNAVAILABLE;  

    struct objc_ivar_list *ivars            OBJC2_UNAVAILABLE; 

    struct objc_method_list **methodLists   OBJC2_UNAVAILABLE;  

    struct objc_cache *cache                OBJC2_UNAVAILABLE;

    struct objc_protocol_list *protocols    OBJC2_UNAVAILABLE;

#endif

} OBJC2_UNAVAILABLE;

在上面的结构中,有这样的:

struct objc_method_list **methodLists   OBJC2_UNAVAILABLE;  

我是菜鸟C,不太明白OC为什么要用指向指针**的指针来做方法列表。

为什么它不使用与缓存相同的结构:

struct objc_cache *cache                OBJC2_UNAVAILABLE;

因为缓存也包含了之前使用的方法。

我认为 Apple 使用双指针 ** 表示 method lists 而不是单指针 * 表示 cacheprotocols 是有原因的。

为什么叫 methodLists 而不是 methodList

我现在正在比较 cacheprotocolsmethodLists

我认为出于技术原因,Apple 将 ** 用于方法列表而不是其他方法列表 *,我想知道。

如果一个指针 * 足以用于方法列表,为什么 Apple 会费心使用双 **

类似问题:Whats is methodLists attribute of the structure objc_class for?

根据上面的post和这个article,Apple设计methodLists双指针的原因(OBJC2.0也是一样)是,简而言之, 双指针结构在运行时将 category 方法添加到 class 的方法列表中更快。

由于双指针是针对一个方法列表的列表,所以考虑到每个类别都有一个方法列表,并且在运行时将一个或多个类别添加到class时,而不是一个一个地添加方法,一个可以简单地将指向类别方法列表的指针添加到 class methodLists,它有一个指向每个 list/array 指针的指针。

我不确定我是否解释得足够清楚。