阅读 398

iOS底层探索--方法慢速查找分析

小谷底层探索合集

  • 大家都知道objc_msgSend核心方法通过汇编实现,汇编查找缓存是快速查找,今天我们了解一波慢速查找!

1. 定位查找核心方法

话不多说上代码:

@interface XGTest : NSObject
- (void)say5;
@end

@implementation XGTest
@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        
        XGTest *test = [XGTest alloc];
        [test say5];
    }
    return 0;
}
复制代码
    1. 我们今天的测试是调用一个没有实现的方法say5, 这样可以更仔细的观察他走过的流程!(如果方法实现了,可以找到,那我们流程研究就不够细致!)
    1. 我们断点看汇编,看他如何查找方法!

    1. 然后断住之后,我们查看汇编(操作方法如图)

    1. 我们跟进去

    1. 这样我们找到了我们要研究的方法lookUpImpOrForward

2. lookUpImpOrForward 分析

老规矩,上代码

IMP lookUpImpOrForward(id inst, SEL sel, Class cls, int behavior)
{
    const IMP forward_imp = (IMP)_objc_msgForward_impcache;
    IMP imp = nil;
    Class curClass;

    runtimeLock.assertUnlocked();

    // Optimistic cache lookup
    if (fastpath(behavior & LOOKUP_CACHE)) {
        imp = cache_getImp(cls, sel);
        if (imp) goto done_nolock;
    }

    // runtimeLock is held during isRealized and isInitialized checking
    // to prevent races against concurrent realization.

    // runtimeLock is held during method search to make
    // method-lookup + cache-fill atomic with respect to method addition.
    // Otherwise, a category could be added but ignored indefinitely because
    // the cache was re-filled with the old value after the cache flush on
    // behalf of the category.

    runtimeLock.lock();

    // We don't want people to be able to craft a binary blob that looks like
    // a class but really isn't one and do a CFI attack.
    //
    // To make these harder we want to make sure this is a class that was
    // either built into the binary or legitimately registered through
    // objc_duplicateClass, objc_initializeClassPair or objc_allocateClassPair.
    checkIsKnownClass(cls);

    if (slowpath(!cls->isRealized())) {
        cls = realizeClassMaybeSwiftAndLeaveLocked(cls, runtimeLock);
        // runtimeLock may have been dropped but is now locked again
    }

    if (slowpath((behavior & LOOKUP_INITIALIZE) && !cls->isInitialized())) {
        cls = initializeAndLeaveLocked(cls, inst, runtimeLock);
        // runtimeLock may have been dropped but is now locked again

        // If sel == initialize, class_initialize will send +initialize and 
        // then the messenger will send +initialize again after this 
        // procedure finishes. Of course, if this is not being called 
        // from the messenger then it won't happen. 2778172
    }

    runtimeLock.assertLocked();
    curClass = cls;

    // The code used to lookpu the class's cache again right after
    // we take the lock but for the vast majority of the cases
    // evidence shows this is a miss most of the time, hence a time loss.
    //
    // The only codepath calling into this without having performed some
    // kind of cache lookup is class_getInstanceMethod().

    for (unsigned attempts = unreasonableClassCount();;) {
        // curClass method list.
        Method meth = getMethodNoSuper_nolock(curClass, sel);
        if (meth) {
            imp = meth->imp(false);
            goto done;
        }

        if (slowpath((curClass = curClass->superclass) == nil)) {
            // No implementation found, and method resolver didn't help.
            // Use forwarding.
            imp = forward_imp;
            break;
        }

        // Halt if there is a cycle in the superclass chain.
        if (slowpath(--attempts == 0)) {
            _objc_fatal("Memory corruption in class list.");
        }

        // Superclass cache.
        imp = cache_getImp(curClass, sel);
        if (slowpath(imp == forward_imp)) {
            // Found a forward:: entry in a superclass.
            // Stop searching, but don't cache yet; call method
            // resolver for this class first.
            break;
        }
        if (fastpath(imp)) {
            // Found the method in a superclass. Cache it in this class.
            goto done;
        }
    }

    // No implementation found. Try method resolver once.

    if (slowpath(behavior & LOOKUP_RESOLVER)) {
        behavior ^= LOOKUP_RESOLVER;
        return resolveMethod_locked(inst, sel, cls, behavior);
    }

 done:
    log_and_fill_cache(cls, imp, sel, inst, curClass);
    runtimeLock.unlock();
 done_nolock:
    if (slowpath((behavior & LOOKUP_NIL) && imp == forward_imp)) {
        return nil;
    }
    return imp;
}
复制代码

2.1. 大致分析一波

    1. 这段代码最终返回imp,说明,他的核心思想就是寻找他的imp
    1. 我们简单的浏览下代码:首先赋值 定义forward_imp,然后判断是否去缓存中查找。然后走到了for!
    1. 大家看见这个for循环不要慌,后面条件没有写,那他默认就是个死循环,只能通过break或者 goto 跳出去

我们可以先大概了解一波流程,然后慢慢分析~

  • 我上图,先看个大概!

    1. 我用普通话表示一波(😆):大致流程就是现在自身列表找,找到了就跳出去,找不到就向他的superclass(父类)找,找到了也done,找不到继续,直到找到NSObject,还是找不到那就赋值 forward_imp

2.2. 具体分析

那么,我们仔细研究下,具体代码是怎么实现的!!

    1. 获取方法的代码
Method meth = getMethodNoSuper_nolock(curClass, sel);
复制代码

我们看下他怎么实现的吧:

    1. 好了,终于找到他怎么查找的了!
ALWAYS_INLINE static method_t *
findMethodInSortedMethodList(SEL key, const method_list_t *list)
{
    ASSERT(list);

    auto first = list->begin();
    auto base = first;
    decltype(first) probe;

    uintptr_t keyValue = (uintptr_t)key;
    uint32_t count;
    
    for (count = list->count; count != 0; count >>= 1) {
        probe = base + (count >> 1);
        
        uintptr_t probeValue = (uintptr_t)probe->name();
        
        if (keyValue == probeValue) {
            // `probe` is a match.
            // Rewind looking for the *first* occurrence of this value.
            // This is required for correct category overrides.
            while (probe > first && keyValue == (uintptr_t)(probe - 1)->name()) {
                probe--;
            }
            return &*probe;
        }
        
        if (keyValue > probeValue) {
            base = probe + 1;
            count--;
        }
    }
    
    return nil;
}
复制代码

核心思想不就是个二分查找法吗(二分查找还是快)

好多兄弟可能不了解 count >>= 1,: count = count>>1( 0000 1000 --> 0000 0100 : 8—>4)

其实就是count = count/2, 不过是人家用的位运算符。

    1. 这个时候,就需要来一波二分查找的图

    1. 我们继续看代码。
imp = cache_getImp(curClass, sel);
复制代码

我们看里面的参数: 这个已经指到他的superclass

    1. 那么我们看下 cache_getImp

已经点不进去了~(这个时候我们会想到,会不会是汇编处理了。那我们全局搜索)

    1. 汇编 _cache_getImp
	 STATIC_ENTRY _cache_getImp

     GetClassFromIsa_p16 p0
     CacheLookup GETIMP, _cache_getImp
复制代码

结合汇编的代码可以看出,会从父类找缓存。找不到的话就ret(return),所以cache_getImp不走递归,而是通过for的的死循环遍历继承链

    1. for 循环基本说的差不多了那就继续看图

方法查找流程差不多说完了~(毕竟我水平有限,可能讲的不细致,希望兄弟们开恩,😆)

3. 慢速查找流程总结

对于慢速查找,我画了个简陋的流程图~(我美术水平低的要死,兄弟萌不要嘲笑我)