美文网首页
cache_t分析

cache_t分析

作者: 浪的出名 | 来源:发表于2020-09-18 14:50 被阅读0次

cache_t的结构

struct cache_t {
#if CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_OUTLINED // mac环境、模拟器
    explicit_atomic<struct bucket_t *> _buckets;
    explicit_atomic<mask_t> _mask;
#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_HIGH_16 // 真机环境,64位系统
    explicit_atomic<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;
#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_LOW_4 // 真机环境,低于64位系统
    // _maskAndBuckets stores the mask shift in the low 4 bits, and
    // the buckets pointer in the remainder of the value. The mask
    // shift is the value where (0xffff >> shift) produces the correct
    // mask. This is equal to 16 - log2(cache_size).
    explicit_atomic<uintptr_t> _maskAndBuckets;
    mask_t _mask_unused;
#if __LP64__
    uint16_t _flags;
#endif
    uint16_t _occupied;
    ......
};
  • 主要包含4个成员_buckets、_mask、_flags、_occupied
  • 真机环境下对_buckets和_mask进行了优化,从成员名字也可以看出_maskAndBuckets,是将两个成员放到一起了,从下面几个static成员猜测_mask应该是占16位,_buckets占48
  • _buckets是一个bucket_t类型的散列表,bucket_t里面有方法名,及其地址,bucket_t的结构如下
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__
    explicit_atomic<uintptr_t> _imp;
    explicit_atomic<SEL> _sel;
#else
    explicit_atomic<SEL> _sel;
    explicit_atomic<uintptr_t> _imp;
#endif
}
  • _mask表示散列表的长度-1
  • _flags
  • _occupied表示缓存的方法数

cache的插入流程

  • 方法查找lookUpImpOrForward->log_and_fill_cache->cache_fill->cache->insert->reallocate
  • 我们着重看下ache->insert的实现
void cache_t::insert(Class cls, SEL sel, IMP imp, id receiver)
{
#if CONFIG_USE_CACHE_LOCK
    cacheUpdateLock.assertLocked();
#else
    runtimeLock.assertLocked();
#endif

    ASSERT(sel != 0 && cls->isInitialized());

    // Use the cache as-is if it is less than 3/4 full
    mask_t newOccupied = occupied() + 1;// 已有缓存数+1
    unsigned oldCapacity = capacity(), capacity = oldCapacity;
    if (slowpath(isConstantEmptyCache())) {// 当缓存为空的时候,创建缓存,小概率事件
        // Cache is read-only. Replace it.
        if (!capacity) capacity = INIT_CACHE_SIZE; // 缓存空间初始值为4
        reallocate(oldCapacity, capacity, /* freeOld */false);// 开辟缓存空间,最后一个参数传false表示新创建的不用释放原来的空间,后面的扩容需要释放旧的缓存
    }
    else if (fastpath(newOccupied + CACHE_END_MARKER <= capacity / 4 * 3)) {
        // Cache is less than 3/4 full. Use it as-is.
        // 如果小于等于缓存空间大小的3/4直接往下执行
    }
    else {// 如果超过了空间的3/4进行2倍扩容
        capacity = capacity ? capacity * 2 : INIT_CACHE_SIZE;
        if (capacity > MAX_CACHE_SIZE) {// 判断是否超出最大缓存空间
            capacity = MAX_CACHE_SIZE;
        }
        reallocate(oldCapacity, capacity, true);// 开辟空间,并释放旧的缓存
    }

    bucket_t *b = buckets();// 缓存方法表
    mask_t m = capacity - 1; // mask 为最大空间数-1
    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 {
        if (fastpath(b[i].sel() == 0)) {// 如果i下标对应的数据为空则存入到i位置
            incrementOccupied();
            b[i].set<Atomic, Encoded>(sel, imp, cls);
            return;
        }
        if (b[i].sel() == sel) {
            // The entry was added to the cache by some other thread
            // before we grabbed the cacheUpdateLock.
            return;
        }
    } while (fastpath((i = cache_next(i, m)) != begin));// 在_arm64架构下cache_next就是返回`i ? i-1 : mask;`

    cache_t::bad_cache(receiver, (SEL)sel, cls);
}

相关文章

  • 七、cache_t 分析

    主要内容:cache_t的底层原理:分析cache_t缓存的内家及怎样缓存的。一、分析cache_t主要存储的是什...

  • cache_t分析

    cache_t分析 cache_t的作用:用于快速查找方法 cache_t的特点:是可增量扩展的hash表结构ha...

  • 类(三)-- cache_t分析

    类(一)-- 底层探索类(二)-- method归属类(三)-- cache_t分析 cache_t作用 用来缓存...

  • objc_class中的cache_t分析

    本文探索的的主要是两点 1、cache_t的结构 2、cache_t里存储的哪些 cache_t结构分析 打开源码...

  • 探索Cache_t

    Cache_t 的整体分析 Cache_t的源码 在objc/objc-runtime-new源码下查找结构体ca...

  • cache_t结构探一探

    接上文类的结构分析 一.cache_t结构 1.cache_t结构 cache是cache_t类型,那么cache...

  • objc_msgSend消息流程之快速查找

    在上一篇文章中我们分析cache_t的写入流程,有写入那么就会有读取,这篇文章分析cache_t的读取流程 什么是...

  • iOS底层原理 08 : cache_t的分析

    (一)cache_t的结构分析 我们首先来看cache_t的定义: bucket_t的结构 在结构体cache_t...

  • iOS底层之cache_t探索

    前言 这篇文章主要是分析cache_t流程。通过源码探索下类的cache_t主要缓存了哪些信息,又是怎么缓存的。分...

  • cache_t的探究

    前言 今天我们要探究的cache_t在之前的类的结构分析中看到过,在objc_class中存在一个cache_t类...

网友评论

      本文标题:cache_t分析

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