目录
方法缓存cache_t
-
在方法被调用后,
cache_t
会进行一次缓存 -
缓存流程<
cache_fill_nolock
代码流程>- 1、缓存入口
cache_fill_nolock
进入,判断是否存在缓存,如果有,则return
- 2、如果不存在缓存,则调用
cache_key_t key = getKey(sel);
,将sel
转化为对应key
,方便后续处理 - 3、读取占用的空间
mask_t newOccupied = cache->occupied() + 1;
,以及目前的容纳能力mask_t capacity = cache->capacity();
- 3.1、根据
cache->isConstantEmptyCache()
,判断cache
是否是一个空的缓存表- 3.1.1、如果不是,则判断< 3.2 >
- 3.1.2、如果是一个空的缓存,则进行
cache->reallocate(capacity, capacity ?: INIT_CACHE_SIZE);
申请一个新的缓存空间bucket
,必将旧的进行释放,然后进行流程< 4 >
- 3.2、判断
newOccupied <= capacity / 4 * 3
是否成立,如果成立,则继续流程< 4 > - 3.3、如果步骤< 3.1 >、< 3.2 >均不成立,则进行
cache->expand();
,进行缓存空间的扩容,扩容到原来的两倍
,并将旧的bucket
进行释放。扩容完成,继续流程< 4 >
- 3.1、根据
- 4、 正常添加
bucket
- 1、缓存入口
部分代码
cache_t
的结构
struct cache_t {
struct bucket_t *_buckets;
mask_t _mask;
mask_t _occupied;
public:
struct bucket_t *buckets();
mask_t mask();
mask_t occupied();
void incrementOccupied();
void setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask);
void initializeToEmpty();
mask_t capacity();
bool isConstantEmptyCache();
bool canBeFreed();
static size_t bytesForCapacity(uint32_t cap);
static struct bucket_t * endMarker(struct bucket_t *b, uint32_t cap);
void expand();
void reallocate(mask_t oldCapacity, mask_t newCapacity);
struct bucket_t * find(cache_key_t key, id receiver);
static void bad_cache(id receiver, SEL sel, Class isa) __attribute__((noreturn));
};
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__
MethodCacheIMP _imp;
cache_key_t _key;
#else
cache_key_t _key;
MethodCacheIMP _imp;
#endif
public:
inline cache_key_t key() const {
return _key; }
inline IMP imp() const {
return (IMP)_imp; }
inline void setKey(cache_key_t newKey) {
_key = newKey; }
inline void setImp(IMP newImp) {
_imp = newImp; }
void set(cache_key_t newKey, IMP newImp);
};
bucket_t
查找缓存
cache_t
查找
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_t::expand()
空间扩容
void cache_t::expand()
{
cacheUpdateLock.assertLocked();
uint32_t oldCapacity = capacity();
uint32_t newCapacity = oldCapacity ? oldCapacity*2 : INIT_CACHE_SIZE;
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);
}
cache_t::reallocate()
开辟空间
void cache_t::reallocate(mask_t oldCapacity, mask_t newCapacity)
{
bool freeOld = canBeFreed();
bucket_t *oldBuckets = buckets();
bucket_t *newBuckets = allocateBuckets(newCapacity);
// Cache's old contents are not propagated.
// This is thought to save cache memory at the cost of extra cache fills.
// fixme re-measure this
assert(newCapacity > 0);
assert((uintptr_t)(mask_t)(newCapacity-1) == newCapacity-1);
setBucketsAndMask(newBuckets, newCapacity - 1);
if (freeOld) {
cache_collect_free(oldBuckets, oldCapacity);
cache_collect(false);
}
}
cache_fill_nolock
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);
}