cache_t结构分析

简介: 紧接着我们来分析类结构体中cache_t, 只从单词来看就能猜出来是与缓存有关. 下面我们先看cache_t的源码:

紧接着我们来分析类结构体中cache_t, 只从单词来看就能猜出来是与缓存有关. 下面我们先看cache_t的源码:


struct cache_t {  
#if 1 // Mac   
    struct bucket_t * _buckets;  
    mask_t _mask;  
#elif 1 // 真机  (尽量用真机调试, 因为真机更贴近日常使用)
    uintptr_t _maskAndBuckets;  
    mask_t _mask_unused;  
    // How much the mask is shifted by.  
    static constexpr uintptr_t maskShift = 48;  
    // Additional bits after the mask which must be zero. msgSend  
    // takes advantage of these additional bits to construct the value  
    // `mask << 4` from `_maskAndBuckets` in a single instruction.  
    static constexpr uintptr_t maskZeroBits = 4;  
    // The largest mask value we can store.  
    static constexpr uintptr_t maxMask = ((uintptr_t)1 << (64 - maskShift)) - 1;  
    // The mask applied to `_maskAndBuckets` to retrieve the buckets pointer.  
    static constexpr uintptr_t bucketsMask = ((uintptr_t)1 << (maskShift - maskZeroBits)) - 1;  
#endif  
    uint16_t _flags; // 标志位  
    uint16_t _occupied; // 被占用的  
public:
    static bucket_t *emptyBuckets();  
    struct bucket_t *buckets();  
    mask_t mask();  
    mask_t occupied();  
    void incrementOccupied();  
    void setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask);  
    void initializeToEmpty();  
    unsigned capacity();  
    bool isConstantEmptyCache();  
    bool canBeFreed();  
    void reallocate(mask_t oldCapacity, mask_t newCapacity, bool freeOld);  
    void insert(Class cls, SEL sel, IMP imp, id receiver);  
};


在进行结构分析时, 我们先写了一个例子来分析cache_t的结构. 具体例子如下:


//LGPerson.h文件  
@interface LGPerson : NSObject  
@property (nonatomic, copy) NSString *lgName;  
@property (nonatomic, strong) NSString *nickName;  
- (void)say1;  
- (void)say2;  
- (void)say3;  
- (void)say4;   
//LGPerson.m文件  
#import "LGPerson.h"  
@implementation LGPerson  
- (void)say1{  
    NSLog(@"LGPerson say : %s",__func__);  
}  
- (void)say2{  
    NSLog(@"LGPerson say : %s",__func__);  
}  
- (void)say3{  
    NSLog(@"LGPerson say : %s",__func__);  
}  
- (void)say4{  
    NSLog(@"LGPerson say : %s",__func__);  
}  
@end


具体调用以及打印结果, 见下方代码:


#import <Foundation/Foundation.h>  
#import "LGPerson.h"  
#import <objc/runtime.h>  
typedef uint32_t mask_t;  // x86_64 & arm64 asm are less efficient with 16-bits  
struct lg_bucket_t {  
    SEL _sel;  
    IMP _imp;  
};  
struct lg_cache_t {  
    struct lg_bucket_t * _buckets;  
    mask_t _mask;  
    uint16_t _flags;  
    uint16_t _occupied;  
};  
struct lg_class_data_bits_t {  
    uintptr_t bits;  
};  
struct lg_objc_class {  
    Class ISA;  
    Class superclass;  
    struct lg_cache_t cache;             // formerly cache pointer and vtable  
    struct lg_class_data_bits_t bits;    // class_rw_t * plus custom rr/alloc flags  
};  
int main(int argc, const char * argv[]) {  
    @autoreleasepool {  
        LGPerson *p  = [LGPerson alloc];  
        Class pClass = [LGPerson class];  // objc_clas  
        [p say1];  
        [p say2];  
//        [p say3];  
//        [p say4];  
        struct lg_objc_class *lg_pClass = (__bridge struct lg_objc_class *)(pClass);  
        NSLog(@"%hu - %u",lg_pClass->cache._occupied,lg_pClass->cache._mask);  
        for (mask_t i = 0; i<lg_pClass->cache._mask; i++) {  
            // 打印获取的 bucket  
            struct lg_bucket_t bucket = lg_pClass->cache._buckets[i];  
            NSLog(@"%@ - %p",NSStringFromSelector(bucket._sel),bucket._imp);  
        }  
        NSLog(@"Hello, World!");  
    }  
    return 0;  
}  
//当屏蔽 [p say3];   [p say4];  方法时的打印结果:   
2020-09-17 21:23:31.259617+0800 003-cache_t脱离源码环境分析[3956:50598] LGPerson say : -[LGPerson say1]  
2020-09-17 21:23:31.259990+0800 003-cache_t脱离源码环境分析[3956:50598] LGPerson say : -[LGPerson say2]  
2020-09-17 21:23:31.260029+0800 003-cache_t脱离源码环境分析[3956:50598] 2 - 3  
2020-09-17 21:23:35.029171+0800 003-cache_t脱离源码环境分析[3956:50598] say1 - 0x2828  
2020-09-17 21:23:35.029352+0800 003-cache_t脱离源码环境分析[3956:50598] say2 - 0x2818  
2020-09-17 21:23:35.029409+0800 003-cache_t脱离源码环境分析[3956:50598] (null) - 0x0  
//当打开屏幕 [p say3];   [p say4]; 的打印结果为:  
2020-09-17 21:37:09.315693+0800 003-cache_t脱离源码环境分析[4296:55622] LGPerson say : -[LGPerson say1]  
2020-09-17 21:37:09.315964+0800 003-cache_t脱离源码环境分析[4296:55622] LGPerson say : -[LGPerson say2]  
2020-09-17 21:37:09.316001+0800 003-cache_t脱离源码环境分析[4296:55622] LGPerson say : -[LGPerson say3]  
2020-09-17 21:37:09.316076+0800 003-cache_t脱离源码环境分析[4296:55622] LGPerson say : -[LGPerson say4]  
2020-09-17 21:37:09.316099+0800 003-cache_t脱离源码环境分析[4296:55622] 2 - 7  
2020-09-17 21:37:09.316196+0800 003-cache_t脱离源码环境分析[4296:55622] say4 - 0x29a8  
2020-09-17 21:37:09.316217+0800 003-cache_t脱离源码环境分析[4296:55622] (null) - 0x0  
2020-09-17 21:37:09.316283+0800 003-cache_t脱离源码环境分析[4296:55622] say3 - 0x29d8  
2020-09-17 21:37:09.316315+0800 003-cache_t脱离源码环境分析[4296:55622] (null) - 0x0   
2020-09-17 21:37:09.316330+0800 003-cache_t脱离源码环境分析[4296:55622] (null) - 0x0 
2020-09-17 21:37:09.316366+0800 003-cache_t脱离源码环境分析[4296:55622] (null) - 0x0  
2020-09-17 21:37:09.316382+0800 003-cache_t脱离源码环境分析[4296:55622] (null) - 0x0  
我们先抛出问题:以及最终的得出的结论        
      // _occupied  _mask 是什么  cup - 1  
        // 会变化 2-3 -> 2-7   (说明有做扩容操作)
        // bucket 会有丢失  重新申请  
        // 顺序有点问题  哈希  
        // 当打开屏蔽方法后, 没有打印出 say1 say2, 证明有做刷新释放操作


然后我们从cache_t的源码来分析:  每当对象调用一个方法时, 如果在cache里面没有找到, 就会insert一条缓存:


void cache_t::insert(Class cls, SEL sel, IMP imp, id receiver)  
{  
    // Use the cache as-is if it is less than 3/4 full  
    mask_t newOccupied = occupied() + 1;  
    unsigned oldCapacity = capacity(), capacity = oldCapacity;  
    // 1. 如果Cache 是空的话,会初始化一个 4 个字节的空间  
    if (slowpath(isConstantEmptyCache())) {  
        // Cache is read-only. Replace it.  
        if (!capacity) capacity = INIT_CACHE_SIZE;  
        reallocate(oldCapacity, capacity, /* freeOld */false);  
    }  
    else if (fastpath(newOccupied + CACHE_END_MARKER <= capacity / 4 * 3)) {  
        // Cache is less than 3/4 full. Use it as-is.  
        // 2. newOccupied + CACHE_END_MARKER <= capacity / 4 * 3 ,直接插入  
    }  
    else {  
        // 3. 否则会扩容,  扩容为原空间的 2倍大小
        capacity = capacity ? capacity * 2 : INIT_CACHE_SIZE;  
        if (capacity > MAX_CACHE_SIZE) {  
            capacity = MAX_CACHE_SIZE;  
        }  
        reallocate(oldCapacity, capacity, true);  // // 重新分配空间   存储新的数据,抹除已有缓存
    }  
    // 4. 初始化一个指针数组  
    bucket_t *b = buckets();  
    // 5. 设置掩码为 capacity - 1  
    mask_t m = capacity - 1;  
    // 6. 根据sel 计算 hash 值  
    mask_t begin = cache_hash(sel, m);  
    mask_t i = begin;  
    /*  
     *  扫描第一个未使用的插槽,并且插入   
     */  
    // Scan for the first unused slot and insert there.  
    // There is guaranteed to be an empty slot because the  
    // minimum size is 4 and we resized at 3/4 full.  
    do {  
        // 7. 当前 插槽 取到 的sel 地址为0, 那么插入新的值  
        if (fastpath(b[i].sel() == 0)) {  
            // 8. 增加占用字段并且插入  
            incrementOccupied();  
            b[i].set<Atomic, Encoded>(sel, imp, cls);  
            return;  
        }  
        // 8. 多线程做的判断  
        if (b[i].sel() == sel) {  // 如果找到需要缓存的方法,什么都不做,并退出循环  
            // The entry was added to the cache by some other thread  
            // before we grabbed the cacheUpdateLock.  
            return;  
        }  
        // 9. 如果当前位置已有值,那么就找下一个位置  判断不等于初始下标值 begin 是为了将散列表中的数据全部遍历结束,而cache_next( ) 是为了解决哈希冲突而进行的二次哈希.
    } while (fastpath((i = cache_next(i, m)) != begin));  
}  
// 10. hash 算法, 保证不会越界  
static inline mask_t cache_hash(SEL sel, mask_t mask)   
{  
    return (mask_t)(uintptr_t)sel & mask;  
}


微信图片_20220509104445.png

cache_t缓存流程图


总结一下cache_t的结构:


微信图片_20220509104451.png

cache_t结构


  • _buckets :是一个散列表,用来存储 缓存方法的 sel 和 imp.
  • _mask :  有2个作用,1: 作为当前可存储的最大容量;2: 作为掩码,取已缓存方法在 _buckets 中的下标.
  • _occupied :  _buckets 中 已缓存的方法数量.




目录
相关文章
|
监控 网络协议 网络架构
|
Kubernetes 监控 负载均衡
Istio:微服务开发的终极利器,你还在为繁琐的通信和部署流程烦恼吗?
本文介绍了服务网格(Service Mesh)的概念及其在微服务架构中的重要性。微服务强调围绕业务构建团队和去中心化的数据管理,带来更高的灵活性和扩展性。然而,随着服务数量增加,网络通信成为挑战,包括服务发现、路由和安全等问题。 Service Mesh如Istio应运而生,通过边车代理解决服务间通信,提供服务发现、负载均衡、智能路由、安全和监控等功能。它与Kubernetes结合,增强了容器环境的服务管理能力。Istio的bookinfo示例展示了其在多语言微服务中的应用,简化了代码中的服务调用逻辑,使开发更专注于业务本身。
1038 3
Istio:微服务开发的终极利器,你还在为繁琐的通信和部署流程烦恼吗?
|
算法 程序员 C++
【C++】基础知识点回顾 上:命名空间与输入输出
【C++】基础知识点回顾 上:命名空间与输入输出
119 0
|
JavaScript 前端开发 Python
[BlocklyNukkit入门]#3玩家进服欢迎
首发于Enaium的个人博客 进服欢迎 我们可以在bn的文档里查到PlayerJoinEvent玩家进入服务器的事件 这样我们就可以很“方便”的编写插件了
183 0
|
数据采集 机器学习/深度学习 人工智能
人工智能入门指南:Web 开发者版 (3)数据处理与准备
本节将介绍在人工智能项目中进行数据处理和准备的关键步骤。我们将涵盖数据收集与清洗、数据可视化、特征选择与处理以及数据集划分与交叉验证等内容。
323 0
|
领域建模 数据安全/隐私保护 信息无障碍
领域驱动建模与面向对象建模的差异
领域驱动建模与面向对象建模的差异
领域驱动建模与面向对象建模的差异
|
Python
GUI编程中的tkinter简单案例实操(由易到难)
GUI编程中的tkinter简单案例实操(由易到难)
203 0
GUI编程中的tkinter简单案例实操(由易到难)
|
人工智能 小程序 物联网
通过阿里云服务器开发物联网小程序联控工程
单片机物联网开发,通过mqtt联网来进行人工智能控制。配置阿里云服务器开发微信小程序APP来实现手机控制工程的小实验工程。后续将实现网站控制,敬请期待!
|
API Windows
API各函数作用简介
API各函数作用简介 1.控件与消息函数 AdjustWindowRect 给定一种窗口样式,计算获得目标客户区矩形所需的窗口大小 AnyPopup 判断屏幕上是否存在任何弹出式窗口 ArrangeIconicWindows 排列一个父窗口的最小化子窗口 AttachThreadInput ...
1151 0

热门文章

最新文章