美文网首页
cache_t分析

cache_t分析

作者: 志在交流 | 来源:发表于2021-11-01 19:31 被阅读0次

    类的缓存,其中包含
    struct objc_class:objc_object {
    //Class isa,
    Class superclass,
    cache_t cache
    ,class_data_bits_t bits bits}

    OC中的方法调用本质上是消息转发,但是每次都去遍历查找是很耗时的操作,这就解释了cache_t存在的意义了。

    cache_t顾名思义是缓存,其底层是通过一个哈希表来实现读取的,调用过的方法会直接从cache_t缓存中读取,大大提升了查找速度。

    struct cache_t {
    struct bucket_t buckets;//8
    mask_t mask; //4
    mask_t occupied;}
    //buckets是一个结构体
    struct bucket_t {
    private:
    // IMP-first is better for arm64e ptrauth and no worse for arm64.
    // SEL-first is better for armv7
    and i386 and x86_64.

    if arm64

    MethodCacheIMP _imp;
    cache_key_t _key;
    

    }
    imp:MethodCacheIMP类型,记录方法的指针。
    key:由方法名name转换而成,作为缓存方法的关键字。

    所以通过结构可知cache_t缓存的是bucket_t结构数据,bucket_t结构又是对方法的一层封装,所以cache_t缓存的就是方法。
    //缓存的是方法
    struct method_t {
    SEL name;
    const char *types;
    MethodListIMP imp;}

    类定义一些东西: 属性-方法,1、成员变量 2、属性 3、对象方法 4、类方法。

    在发送objc_msgSend的时候,先去缓存中查找,如果没有找到,那么进行缓存。
    下面看一个极为重要的方法,在调用方法的时候,底层首先判断这个方法有没有被缓存过,如果有,直接返回方法的imp,如果没有则要先进行方法缓存。
    缓存流程 cache_fill_nolock 缓存入口。

    1、判断是否有缓存 return
    2、cache_key_t key = getKey(sel);
    3、读取开辟占用occupied
    4、cache->isConstantEmptyCache() 1、cache->reallocate
    缓存bucket 没有的时候就创建0+1,达到3/4就扩容是原来的两倍。
    find -> key通过哈希存储;bucket。

    缓存的时候为什么删除旧的缓存,重新分配缓存:因为考虑到内存布局太费时间了,这是快速流程,所以直接重新分配更快。

    cache_t.png

    相关文章

      网友评论

          本文标题:cache_t分析

          本文链接:https://www.haomeiwen.com/subject/melaaltx.html