oc中的cache_t浅析

145 阅读4分钟

本篇章要讨论的问题有两个:cache是什么?为什么需要cache?

首先,关于cache的含义,顾名思义,它是缓存,但是其中缓存的是哪些内容?缓存的规则是什么?下面我们来做一个简要分析。

一、缓存内容

我们从cache_t的定义说起,其定义如下:

struct cache_t {
    struct  bucket_t *_buckets;//哈希表,缓存了方法列表
    mask_t _mask;//一个无符号整数值,其值等于哈希表的长度-1
    mask_t _occupied;//缓存的方法数量
    }

实际上,_buckets是一个哈希表,_mask的值等于哈希表的长度-1,_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;
#else
    cache_key_t _key;
    MethodCacheIMP _imp;
#endif
}

由此可见,buckets内部缓存的是方法列表,这个key实际上是方法的sel,对应方法名,imp是方法对应函数的内存地址。

二、缓存机制

了解了缓存内容,那么苹果是如何对方法进行缓存的呢?可以通过源码做一些探究。

static void cache_fill_nolock(Class cls, SEL sel, IMP imp, id receiver)
{
    cacheUpdateLock.assertLocked();

    // Never cache before +initialize is done
    //如果还没有初始化,一定没有缓存,直接返回
    if (!cls->isInitialized()) return;

    // Make sure the entry wasn't added to the cache by some other thread 
    // before we grabbed the cacheUpdateLock.
    //如果已经有其他线程缓存过,不再进行缓存
    if (cache_getImp(cls, sel)) return;

    cache_t *cache = getCache(cls);
    cache_key_t key = getKey(sel);

    // Use the cache as-is if it is less than 3/4 full
    mask_t newOccupied = cache->occupied() + 1;
    mask_t capacity = cache->capacity();
    if (cache->isConstantEmptyCache()) {
        // Cache is read-only. Replace it.
        //如果缓存区是只读的,就重新开辟缓存区
        cache->reallocate(capacity, capacity ?: INIT_CACHE_SIZE);
    }
    else if (newOccupied <= capacity / 4 * 3) {//容量不满,继续缓存
        // Cache is less than 3/4 full. Use it as-is.
    }
    else {
        // Cache is too full. Expand it.//扩容
        cache->expand();
    }

    //寻找一个空位插入
    // 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.
    bucket_t *bucket = cache->find(key, receiver);
    if (bucket->key() == 0) cache->incrementOccupied();
    bucket->set(key, imp);
}

这个方法需要注意的地方有以下几点:

1、扩容

在已缓存的方法容量达到当前缓存总容量的3/4时候,就会执行扩容操作expand()操作。

void cache_t::expand()
{
    cacheUpdateLock.assertLocked();
    
    uint32_t oldCapacity = capacity();
    uint32_t newCapacity = oldCapacity ? oldCapacity*2 : INIT_CACHE_SIZE;//这里有个初始的容量值4

    if ((uint32_t)(mask_t)newCapacity != newCapacity) {
        // mask overflow - can't grow further
        // fixme this wastes one bit of mask
        newCapacity = oldCapacity;
    }

    reallocate(oldCapacity, newCapacity);
}

需要注意的是,缓存区扩容是开辟了一块新的空间,旧的缓存区的内容不会复制到新的缓存区。

2、寻找插入空位

bucket_t * cache_t::find(cache_key_t k, id receiver)
{
    assert(k != 0);

    bucket_t *b = buckets();
    mask_t m = mask();
    // 通过cache_hash函数【begin  = k & m】计算出key值 k 对应的 index值 begin,用来记录查询起始索引
    mask_t begin = cache_hash(k, m);
    // begin 赋值给 i,用于切换索引
    mask_t i = begin;
    do {
        if (b[i].key() == 0  ||  b[i].key() == k) {
            //用这个i从散列表取值,如果取出来的bucket_t的 key = k,则查询成功,返回该bucket_t,
            //如果key = 0,说明在索引i的位置上还没有缓存过方法,同样需要返回该bucket_t,用于中止缓存查询。
            return &b[i];
        }
    } while ((i = cache_next(i, m)) != begin);
    
    // 这一步其实相当于 i = i-1,回到上面do循环里面,相当于查找散列表上一个单元格里面的元素,再次进行key值 k的比较,
    //当i=0时,也就i指向散列表最首个元素索引的时候重新将mask赋值给i,使其指向散列表最后一个元素,重新开始反向遍历散列表,
    //其实就相当于绕圈,把散列表头尾连起来,不就是一个圈嘛,从begin值开始,递减索引值,当走过一圈之后,必然会重新回到begin值,
    //如果此时还没有找到key对应的bucket_t,或者是空的bucket_t,则循环结束,说明查找失败,调用bad_cache方法。
 
    // hack
    Class cls = (Class)((uintptr_t)this - offsetof(objc_class, cache));
    cache_t::bad_cache(receiver, (SEL)k, cls);
}

当通过cache->find返回的bucket->key() == 0,就说明该位置上是空的,没有缓存过方法,是一个unused slot(未使用的槽口),可以进行插入操作bucket->set(key, imp),也就是将方法缓存到这个位置上。

三、缓存的意义

缓存的出现,就是为了提高方法查找的速度。

如果没有缓存,类对象,每次调用方法,先到本类的方法列表中去查找,

如果本类方法列表中没有,就去父类的方法列表中查找;

......

如果根类方法列表中没有,再去NSObject的方法列表中去查找;

如果该方法在程序内部被频繁调用,那么这种逐层遍历查找的方式肯定是效率低下的,因此苹果设计了cache。当方法第一次被调用的时候,会按照常规流程查找,找到之后,就会被加入到cache中。当方法再次被调用的时候,系统就会先到cache中查找,找到就直接调用,这样便大大提升了查找的效率。

而且cache的设计也是利用散列表,其查找的时间复杂度是O(1)的,方便快捷。