方法缓存
其实这个文章网上有很多解释也有很多人写过相关文章,但是我还是有一些疑问,所以我打算从另外一个角度来学习一下这个方法缓存。
首先我找到了直接对方法缓存进行操作的一个文件 objc-cache.h/m
,其中的头文件定义如下
extern IMP cache_getImp(Class cls, SEL sel); ## 获取方法
extern void cache_fill(Class cls, SEL sel, IMP imp, id receiver); ## 添加cache
extern void cache_erase_nolock(Class cls); ## 擦除 cache,重制缓存信息,并且重新申请缓存桶
extern void cache_delete(Class cls); ## 删除cache ,删除缓存信息,释放缓存桶
extern void cache_collect(bool collectALot); ## 垃圾回收
cache_getImp - 获取缓存方法
这个方法顾名思义是从缓存中获取方法,它的实现是汇编语句,比较难懂,但是其实我们可以根据注释了解到他的行为。
/********************************************************************
* IMP cache_getImp(Class cls, SEL sel)
*
* On entry: r0 = class whose cache is to be searched
* r1 = selector to search for
*
* If found, returns method implementation.
* If not found, returns NULL.
********************************************************************/
STATIC_ENTRY cache_getImp
mov r9, r0
CacheLookup GETIMP // returns IMP on success
LCacheMiss:
mov r0, #0 // return nil if cache miss
bx lr
LGetImpExit:
END_ENTRY cache_getImp
- 如果有缓存则放回
imp
- 反之返回为
nil
接下来看下什么情况下会调用获取缓存方法呢?
-
lookUpImpOrForward
: 查找方法,找不到去父类找 -
cache_fill_nolock
: 这个是添加cache_fill
的不加锁的方法,主要为了避免重复添加方法
cache_fill - 添加缓存
这个方法就是添加缓存方法了
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);
}
void cache_fill(Class cls, SEL sel, IMP imp, id receiver)
{
#if !DEBUG_TASK_THREADS
mutex_locker_t lock(cacheUpdateLock);
cache_fill_nolock(cls, sel, imp, receiver);
#else
_collecting_in_critical();
return;
#endif
}
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;
}
# 如果达到最大值之后,只会创建刷新 bucket
reallocate(oldCapacity, newCapacity);
}
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);
}
}
- 判断
cls
是否初始化 - 判断已经加过缓存了
- 判断是否只读,只读会重新创建一个
bucket_t
- 判断是否超过
3/4
,超过会expand
,重新创建一个2
倍容量的但是不会带着原来的缓存,所以重新创建之后缓存数量是0
- 然后添加到
blucket
然后我们来看看什么情况下会调用添加缓存呢?
-
log_and_fill_cache
: 字面意思,主要lookUpImpOrForward
使用 -
lookUpImpOrForward
: 查找方法,找不到去父类找 -
lookupMethodInClassAndLoadCache
: 和上面的方法区别就是不回forword
,不去父类找
cache_erase_nolock - 删除缓存
这里是删除缓存,其实我好奇的原因也主要是这个,我想知道他什么时候删除的缓存,先看看删除缓存吧
// Reset this entire cache to the uncached lookup by reallocating it.
// This must not shrink the cache - that breaks the lock-free scheme.
void cache_erase_nolock(Class cls)
{
cacheUpdateLock.assertLocked();
cache_t *cache = getCache(cls);
mask_t capacity = cache->capacity();
if (capacity > 0 && cache->occupied() > 0) {
auto oldBuckets = cache->buckets();
auto buckets = emptyBucketsForCapacity(capacity);
cache->setBucketsAndMask(buckets, capacity - 1); // also clears occupied
cache_collect_free(oldBuckets, capacity);
cache_collect(false);
}
}
- 清除
bucket
里面的内容
什么时候会擦除缓存呢?
- flushCaches: 刷新
cache
的时候,什么时候会触发刷新cache
呢- attachCategories: 加载分类的时候
- _method_setImplementation: 设置方法
imp
的时候 - method_exchangeImplementations: 方法交换
imp
的时候,注意这里会清除所有的cache
(不仅仅是当前的类) - addMethod: 添加方法的时候
- setSuperclass: 设置父类
- _objc_flush_caches:instrumentObjcMessageSends(runtime的log开关)的时候触发
这里我们可以看到对类的方法和父类操作的时候就会擦除缓存,当时困扰我的问题是为什么存父类方法到子类里面,那么子类突然实现不就出问题了,现在看来是不会出问题的,安全的。
cache_delete - 删除缓存
这里的删除缓存只有在类被销毁的时候才会调用
void cache_delete(Class cls)
{
mutex_locker_t lock(cacheUpdateLock);
if (cls->cache.canBeFreed()) {
if (PrintCaches) recordDeadCache(cls->cache.capacity());
free(cls->cache.buckets());
}
}
调用时机也比较简单
- free_class: 释放类,至于类的生命周期,我们在类的生命周期里面展开把
cache_collect - 垃圾回收
这个方法的作用也就是回收垃圾,一般和cache_collect_free
配合使用
static void cache_collect_free(bucket_t *data, mask_t capacity)
{
cacheUpdateLock.assertLocked();
if (PrintCaches) recordDeadCache(capacity);
_garbage_make_room ();
garbage_byte_size += cache_t::bytesForCapacity(capacity); //这里收集了需要释放的cache
garbage_refs[garbage_count++] = data;//这里收集了需要释放的cache
}
void cache_collect(bool collectALot)
{
cacheUpdateLock.assertLocked();
// Done if the garbage is not full
if (garbage_byte_size < garbage_threshold && !collectALot) {
return;
}
// Synchronize collection with objc_msgSend and other cache readers
if (!collectALot) {
if (_collecting_in_critical ()) {
// objc_msgSend (or other cache reader) is currently looking in
// the cache and might still be using some garbage.
if (PrintCaches) {
_objc_inform ("CACHES: not collecting; "
"objc_msgSend in progress");
}
return;
}
}
else {
// No excuses.
while (_collecting_in_critical())
;
}
// No cache readers in progress - garbage is now deletable
// Log our progress
if (PrintCaches) {
cache_collections++;
_objc_inform ("CACHES: COLLECTING %zu bytes (%zu allocations, %zu collections)", garbage_byte_size, cache_allocations, cache_collections);
}
// Dispose all refs now in the garbage
while (garbage_count--) {
free(garbage_refs[garbage_count]); //这里free了 ----对应free的收集
}
// Clear the garbage count and total size indicator
garbage_count = 0;
garbage_byte_size = 0;
if (PrintCaches) {
size_t i;
size_t total_count = 0;
size_t total_size = 0;
for (i = 0; i < countof(cache_counts); i++) {
int count = cache_counts[i];
int slots = 1 << i;
size_t size = count * slots * sizeof(bucket_t);
if (!count) continue;
_objc_inform("CACHES: %4d slots: %4d caches, %6zu bytes",
slots, count, size);
total_count += count;
total_size += size;
}
_objc_inform("CACHES: total: %4zu caches, %6zu bytes",
total_count, total_size);
}
}
- 在
cache_collect_free
中收集垃圾 - 在
cache_collect
中free
什么时候调用呢?
-
cache_erase_nolock
:擦除缓存的时候 -
reallocate
:重新创建缓存的时候 -
_objc_flush_caches
: 刷新缓存的时候