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 中 已缓存的方法数量.




目录
相关文章
|
存储 数据采集 机器学习/深度学习
主数据管理的前世 今生 未来(一文深入了解主数据管理)(下)
根据Gartner的定义,“主数据管理(MDM)是一种技术支持的规程,业务和IT部门共同工作,以确保企业共享主数据资产的唯一性、准确性、语义一致性和可靠性……”
主数据管理的前世 今生 未来(一文深入了解主数据管理)(下)
|
9月前
|
数据采集 搜索推荐 安全
网站搭建好并部署阿里云服务器上,如何用好Swap让服务器保持稳定
本机配置2核6GB内存,新增3GB Swap作为应急缓冲。Swap虽比内存慢,但能有效避免内存耗尽时OOM Killer误杀Tomcat导致全站502。正常情况下几乎不启用,高峰时自动腾挪冷数据,显著提升稳定性,是低成本高收益的关键优化。
661 1
|
5月前
|
SQL 存储 关系型数据库
MySQL/SQL Server TDE透明加密技术详解与医疗HIS系统落地实践
本文详解数据库透明加密(TDE)技术原理、SQL Server等主流数据库实施方案、性能影响(&lt;5%损耗)及防勒索实战价值:有效抵御数据文件窃取与备份加密,满足等保2.0存储加密要求,兼顾透明性、安全性与合规性。(239字)
|
7月前
|
人工智能 搜索推荐 C++
面向少儿的AI英语背单词APP
专为3-12岁儿童打造的AI英语背单词APP,以多感官沉浸体验替代机械记忆:AI实时生成兴趣定制插图(如恐龙跑“run”)、儿童专属语音纠音+趣味反馈、动态遗忘曲线智能复习。游戏化闯关、听音辨物、拼词气球等交互设计,兼顾科学性与童趣,首版聚焦“AI生图+口语跟读”闭环。(239字)
|
6月前
|
机器学习/深度学习 编解码 算法
PyTorch深度学习实战 | 标准化和归一化
本文介绍了数据预处理中的标准化和归一化方法,包括计算过程、特性对比及实际应用。以波士顿房价数据集为例,展示了原始数据、标准化和归一化后的模型效果差异。同时讲解了深度学习中常用的层归一化(LN)和批量归一化(BN)技术,分别适用于文本和图像处理任务,并给出了PyTorch实现示例。LN通过调整词向量分布缓解梯度消失等问题,而BN则对图像各通道单独处理。两种技术都能提升模型训练效果,但适用场景不同。
376 0
|
6月前
|
运维 监控 安全
为什么越来越多服务器开始被“自动化扫描”?
企业常误以为“小业务不会被盯上”,实则公网服务器开服10分钟即遭自动化扫描。SSH暴力破解、Redis未授权、Docker裸奔等探测日均发生,根源在于全网已成“自动化攻击牧场”。漏洞披露几小时内,扫描脚本已席卷全网。中小企业因缺乏安全运维能力,极易沦为挖矿、后门温床。
|
9月前
|
自然语言处理 搜索推荐 BI
企业级 Agent 解决方案
瓴羊企业级Agent解决方案以AgentOne为智能中枢,打通数据孤岛、工具链断裂与业务闭环缺失三大痛点,构建“感知-决策-执行-反馈”全链路数智闭环。集成Quick Service、智能小Q、Quick Audience及Dataphin四大专业智能体,实现服务、分析、营销与数据治理的全域协同。(239字)
|
10月前
|
缓存 前端开发 NoSQL
知识付费系统开发核心架构拆解:从内容管理到支付闭环实现
本文直击知识付费平台核心痛点,摒弃华而不实的前端包装,从技术架构底层拆解内容管理、权限控制、订单支付、分账结算等关键模块。详解分层/微服务架构设计、数据库建模、鉴权播放、幂等回调、缓存优化等实战方案,强调“内容安全、交易稳定、权限精确、可扩展升级”四大目标,助你打造高可用、可持续迭代的硬核系统。(239字)
|
人工智能 安全 数据安全/隐私保护
集之互动AI视频服务助力企业实现降本增效
集之互动依托自研大模型,打造高控制性AI视频服务,深度内化品牌VI,实现AI TVC与商业大片的精准生成。制作周期缩短50%,成本降低2/3,兼顾品质、合规与安全,已服务肯德基、UGG等500强企业,推动AIGC从创意尝鲜迈向商业级量产。
460 3
|
12月前
|
传感器 存储 人工智能
2026年企业级网络监控选型指南
2026年十大IT监控工具深度解析:从OpManager的全能均衡,到Zabbix的极致灵活,再到Datadog的云原生领先,覆盖混合云、AIOps与成本控制。无论你是中小企业求稳,还是大型企业追优,亦或技术极客自研,都能找到适配之选。选型不再盲从,洞察趋势方能致远。
826 0