方法快取與查詢

不會騎名字發表於2018-06-11

前面我們去檢視了類的結構,其中有個cache_t cache;欄位沒有去分析,現在我們就去原始碼中探尋一下。
cache顧名思義,就是快取的意思,它是對曾經呼叫過的方法進行快取,為什麼要快取呢?首先我們先說下方法的呼叫,在我們呼叫[objc message]的時候會編譯成objc_msgSend(objc,@selector(message)),這時候就物件就會根據自己的isa指標去尋找自己的類,在找到類後根據類得到class_rw_t結構體,然後遍歷class_rw_t中的method_array_t methods陣列,如果找不到則根據superclass指標去遍歷父類的方法列表直到找到為止(類方法同理),如果每次呼叫方法都去查下的話是一個很浪費資源的做法,所以OC就設計了一個cache去快取曾經呼叫過的方法列表。
cache_t是一個雜湊表,所以可以使用鍵值對的方式去讀取,這樣的效率就肯定比遍歷快的多,我們簡單看下方法是怎麼快取的

方法呼叫

首先我們在lookUpImpOrForward打個斷點

方法快取與查詢
可以看到呼叫_objc_msgSend後會呼叫該方法

IMP lookUpImpOrForward(Class cls, SEL sel, id inst, 
                       bool initialize, bool cache, bool resolver)
{
    Class curClass;
    IMP imp = nil;
    Method meth;
    bool triedResolver = NO;

    runtimeLock.assertUnlocked();
    if (cache) {
    //1,如果本身有快取就直接返回快取中的
        imp = cache_getImp(cls, sel);
        if (imp) return imp;
    }

    if (!cls->isRealized()) {
   // 2,檢視類是否載入,若未載入 直接載入
        rwlock_writer_t lock(runtimeLock);
        realizeClass(cls);
    }

    if (initialize  &&  !cls->isInitialized()) {
    //3,呼叫initialize方法
        _class_initialize (_class_getNonMetaClass(cls, inst));
    }
 retry:
    runtimeLock.read();

    // Try this class iss cache.
    //4,在類快取中查詢,如果存在 直接返回
    imp = cache_getImp(cls, sel);
    if (imp) goto done;

    // Try this class is method lists.
    //5,在自己的方法列表中查詢
    meth = getMethodNoSuper_nolock(cls, sel);
    if (meth) {
    //6,如果查詢到了,載入進快取,結束查詢
        log_and_fill_cache(cls, meth->imp, sel, inst, cls);
        imp = meth->imp;
        goto done;
    }

    // Try superclass caches and method lists.

    curClass = cls;
    //7,迴圈在類的父類中查詢方法
    while ((curClass = curClass->superclass)) {
        // Superclass cache.
        //8,在父類快取中查詢
        imp = cache_getImp(curClass, sel);
        if (imp) {
            if (imp != (IMP)_objc_msgForward_impcache) {
            //9,在父類中找到方法並且不是`_objc_msgForward_impcache`(訊息轉發)就把方法新增的自身的快取中,並返回
                // Found the method in a superclass. Cache it in this class.
                log_and_fill_cache(cls, imp, sel, inst, curClass);
                goto done;
            }
            else {
                // Found a forward:: entry in a superclass.
                // Stop searching, but do not cache yet; call method 
                // resolver for this class first.
                break;
            }
        }
        // Superclass method list.
        //10,快取中查不到在父類的方法列表中查詢
        meth = getMethodNoSuper_nolock(curClass, sel);
        if (meth) {
        //11查詢到,就把方法新增的自身的快取中,並返回
            log_and_fill_cache(cls, meth->imp, sel, inst, curClass);
            imp = meth->imp;
            goto done;
        }
    }


    if (resolver  &&  !triedResolver) {
    //12,方法解析
        runtimeLock.unlockRead();
        _class_resolveMethod(cls, sel, inst);
        triedResolver = YES;
        goto retry;
    }
    //13,查詢不到直接返回`_objc_msgForward_impcache`(方法轉發)
    imp = (IMP)_objc_msgForward_impcache;
    cache_fill(cls, sel, imp, inst);

 done:
    runtimeLock.unlockRead();

    return imp;
}

複製程式碼

1,如果本身有快取就直接返回快取中的

    if (cache) {
        imp = cache_getImp(cls, sel);
        if (imp) return imp;
    }
複製程式碼

2,檢視類是否載入,若未載入 直接載入

  if (!cls->isRealized()) {
        rwlock_writer_t lock(runtimeLock);
        realizeClass(cls);//類如果沒有加入記憶體 則加入
    }
複製程式碼

3,呼叫initialize方法

if (initialize  &&  !cls->isInitialized()) {
 _class_initialize (_class_getNonMetaClass(cls, inst));
 }
複製程式碼

由此也能看出initialize與類載入是否到記憶體中無關,只要實現了該方法切第一次呼叫類就會執行該方法

4,在類快取中查詢,如果存在 直接返回

    imp = cache_getImp(cls, sel);
    if (imp) goto done;
複製程式碼

5,在自己的方法列表中查詢
6,如果查詢到了,載入進快取,結束查詢

    meth = getMethodNoSuper_nolock(cls, sel);
    if (meth) {
        log_and_fill_cache(cls, meth->imp, sel, inst, cls);
        imp = meth->imp;
        goto done;
    }
複製程式碼

7,迴圈在類的父類中查詢方法
8,在父類快取中查詢
9,在父類中找到方法並且不是_objc_msgForward_impcache(訊息轉發)就把方法新增的自身的快取中,並返回

 while ((curClass = curClass->superclass)) {
        // Superclass cache.
        imp = cache_getImp(curClass, sel);
        if (imp) {
            if (imp != (IMP)_objc_msgForward_impcache) {
                // Found the method in a superclass. Cache it in this class.
                log_and_fill_cache(cls, imp, sel, inst, curClass);
                goto done;
            }
            else {
                // Found a forward:: entry in a superclass.
                // Stop searching, but do not cache yet; call method 
                // resolver for this class first.
                break;
            }
        }
    }
複製程式碼

10,快取中查不到在父類的方法列表中查詢
11,查詢到,就把方法新增的自身的快取中,並返回

while ((curClass = curClass->superclass)) {
  meth = getMethodNoSuper_nolock(curClass, sel);
        if (meth) {
            log_and_fill_cache(cls, meth->imp, sel, inst, curClass);
            imp = meth->imp;
            goto done;
        }
    }
複製程式碼

12,方法解析

    if (resolver  &&  !triedResolver) {
		//12,方法解析
        runtimeLock.unlockRead();
        _class_resolveMethod(cls, sel, inst);
        // Don't cache the result; we don't hold the lock so it may have 
        // changed already. Re-do the search from scratch instead.
        triedResolver = YES;
        goto retry;
    }
複製程式碼

在這個函式中實現了複雜的方法解析邏輯。如果 cls 是元類則會傳送 +resolveClassMethod,然後根據 lookUpImpOrNil(cls, sel, inst, NO/*initialize*/, YES/*cache*/, NO/*resolver*/) 函式的結果來判斷是否傳送 +resolveInstanceMethod;如果不是元類,則只需要傳送 +resolveInstanceMethod 訊息。這裡呼叫 +resolveInstanceMethod+resolveClassMethod 時再次用到了 objc_msgSend,而且第三個引數正是傳入 lookUpImpOrForward 的那個 sel。在傳送方法解析訊息之後還會呼叫 lookUpImpOrNil(cls, sel, inst, NO/*initialize*/, YES/*cache*/, NO/*resolver*/) 來判斷是否已經新增上 sel 對應的 IMP 了,列印出結果。

13,查詢不到直接返回_objc_msgForward_impcache(方法轉發)

    imp = (IMP)_objc_msgForward_impcache;
    cache_fill(cls, sel, imp, inst);
複製程式碼

方法快取

在上面程式碼中我們可以看到有呼叫cache_fill方法

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
}
複製程式碼

又呼叫了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 was not added to the cache by some other thread 
    // before we grabbed the cacheUpdateLock.
	//讀取快取 如果存在,直接返回
    if (cache_getImp(cls, sel)) return;

	//獲取快取表,以`sel`地址為key
    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) {
		//如果快取小於3/4
        // Cache is less than 3/4 full. Use it as-is.
    }
    else {
        // Cache is too full. Expand it.
		//快取超過3/4 則要重新分配快取空間
        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_t *bucket = cache->find(key, receiver);
	//判斷當前的位置是否為空,如果是的 則將佔用量+1
    if (bucket->key() == 0) cache->incrementOccupied();
	//存入快取
    bucket->set(key, imp);
}

複製程式碼

這個方法主要就是去查詢表中的空餘位置,然後將對應的方法放入該位置
我們看到快取如果超出了,則需要重新分配空間

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 - ca not grow further
        // fixme this wastes one bit of mask
        newCapacity = oldCapacity;
    }

    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 is 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);
    }
}
複製程式碼

在上述程式碼中可以看到,重新分配快取空間是原先空間的2倍,然後後將原先的空間釋放了,但是沒有將原先的資料儲存下來,這是為什麼呢?主要因為這個表是個雜湊表,它獲取到的位置是拿keymask去進行&運算得出的,我們可以在find方法中檢視到

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

    bucket_t *b = buckets();
    mask_t m = mask();
    //根據`key`與`mask`獲取到儲存在表中的位置
    mask_t begin = cache_hash(k, m);
    mask_t i = begin;
    do {
        if (b[i].key() == 0  ||  b[i].key() == k) {
            return &b[i];
        }
    } while ((i = cache_next(i, m)) != begin);

    // hack
    Class cls = (Class)((uintptr_t)this - offsetof(objc_class, cache));
    cache_t::bad_cache(receiver, (SEL)k, cls);
}
//該方法就是進行簡單的`&`運算獲取,因為是`&`運算,所以得到的結果肯定比`mask`小
static inline mask_t cache_hash(cache_key_t key, mask_t mask) 
{
    return (mask_t)(key & mask);
}
複製程式碼

mask其實就是當前最大儲存量-1

void cache_t::reallocate(mask_t oldCapacity, mask_t newCapacity)
{
    bool freeOld = canBeFreed();

    bucket_t *oldBuckets = buckets();
    bucket_t *newBuckets = allocateBuckets(newCapacity);

    assert(newCapacity > 0);
    assert((uintptr_t)(mask_t)(newCapacity-1) == newCapacity-1);

   //設定新表,設定mask值
    setBucketsAndMask(newBuckets, newCapacity - 1);
    
    if (freeOld) {
        cache_collect_free(oldBuckets, oldCapacity);
        cache_collect(false);
    }
}
void cache_t::setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask)
{
    mega_barrier();

    _buckets = newBuckets;
    
    // ensure other threads see new buckets before new mask
    mega_barrier();
    
    _mask = newMask;
    _occupied = 0;
}
複製程式碼

因為是個mask就只有那麼大,如果容量變大了,那麼mask的值也會變話,那樣再拿key與新的mask進行&運算得出的結果肯定就不是原先的值了
因為key值不確定,進行&運算後很可能得到相同的值,這也是我們常說的hash衝突,蘋果是以cache_next的方式去解決衝突(arm64)

static inline mask_t cache_next(mask_t i, mask_t mask) {
    return i ? i-1 : mask;
}
複製程式碼

從當前位子一直向下找,找到了空位置,就填充進去,如果找不到,就從後面向前遍歷查詢mask,一直找到(i = cache_next(i, m)) != begin


總結
1,當方法呼叫的時候首先回去快取中查詢,找不到會遍歷方法列表,在找不到會查詢父類方法快取,然後查詢父類方法列表,一直找到rootClass,找到後會加入本身類的方法快取中,如果一直找不到就進行訊息轉發;
2,方法快取是使用cache_t快取,它是一個雜湊表,當快取空間大於3/4時就需要擴容,並將先前儲存的所有方法清空;
3,當第二次呼叫該方法時會在objc_msgSend中查詢一遍然後呼叫,所以可以打斷點嘗試下第二次呼叫的時候就不會在呼叫lookUpImpOrForward

相關文章