iOS

iOS底层探索之Runtime(三): lookUpImpOrF

2021-07-01  本文已影响0人  俊而不逊

1. 回顾

iOS底层探索之Runtime(一):运行时&方法的本质
iOS底层探索之Runtime(二): objc_msgSend&汇编快速查找分析

在前面的文章中介绍了消息发送(objc_msgSend)流程,主要是汇编快速查找cache的过程,并对汇编源码进行了分析,本章内容主要分析慢速查找_lookUpImpOrForward流程。

2. _lookUpImpOrForward

在汇编的快速查找没有找到缓存,就会进入__objc_msgSend_uncached,在__objc_msgSend_uncached 里面最主要的是对MethodTableLookup的处理。

2.1 MethodTableLookup

MethodTableLookup

_lookUpImpOrForward在源码里面没有找到汇编的实现,因为_lookUpImpOrForward不是汇编写的,是C++写的,所以去掉下划线就可以搜索🔍找到了

lookUpImpOrForward

lookUpImpOrForward的函数实现里面,确实发现了,lookUpImpOrForward返回的是imp,也就有验证了上面👆的汇编分析

lookUpImpOrForward返回imp

缓存找不到了,就进入慢速查找流程,遍历method_list方法列表,遍历是个耗时间的流程,所以就放入了C++中,下面重点分析lookUpImpOrForward👇

2.2 isKnownClass

lookUpImpOrForward->checkIsKnownClass(cls)->checkIsKnownClass->isKnownClass

isKnownClass(Class cls)
{
    if (fastpath(objc::dataSegmentsRanges.contains(cls->data()->witness, (uintptr_t)cls))) {
        return true;
    }
    auto &set = objc::allocatedClasses.get();
    return set.find(cls) != set.end() || dataSegmentsContain(cls);
}

lookUpImpOrForward->realizeAndInitializeIfNeeded_locked->realizeClassMaybeSwiftAndLeaveLocked->realizeClassMaybeSwiftMaybeRelock->realizeClassWithoutSwift

auto ro = (const class_ro_t *)cls->data();
    auto isMeta = ro->flags & RO_META;
    if (ro->flags & RO_FUTURE) {
        // This was a future class. rw data is already allocated.
        rw = cls->data();
        ro = cls->data()->ro();
        ASSERT(!isMeta);
        cls->changeInfo(RW_REALIZED|RW_REALIZING, RW_FUTURE);
    } else {
        // Normal class. Allocate writeable class data.
        rw = objc::zalloc<class_rw_t>();
        rw->set_ro(ro);
        rw->flags = RW_REALIZED|RW_REALIZING|isMeta;
        cls->setData(rw);
    }

    cls->cache.initializeToEmptyOrPreoptimizedInDisguise();
realizeClassWithoutSwift

我在iOS底层探索之类的结构(上):ISA文章中已经介绍了isa的走位,和元类的继承关系。当对象调用方法的时候,判断当前类是否初始化,父类、元类是否初始化。目的是,如果当前类中没有实现方法,就去父类查找。如果元类中没有实现类方法,就去根元类查找。递归操作,遍地开花。

到底是怎么递归,怎么循环找方法的呢?请耐心往下看

划重点 for循环

我的天哪!开什么玩笑啊?这是循环吗?不要蒙我,我可是学过编程的啊,循环有三个条件语句的啊!这就一个,后面两个都没有啊!

靓仔,你没有看错,这确实是循环,死循环!


图不重要

这真的是for循环,只是循环体里面,有gotobreak等语句打破死循环

3. 慢速查找分析

3.1 慢速查找流程大纲

  1. 查找自己方法列表Method_list -> sel-imp
  2. 父类中查找 -> NSObject -> nil -> 跳出循环

大概就是这么个流程,那么我们下面去验证下

3.2 分析源码

进入for循环首先就是一个if判断,是否有共享缓存。

 if (curClass->cache.isConstantOptimizedCache(/* strict */true)) {
#if CONFIG_USE_PREOPT_CACHES
            imp = cache_getImp(curClass, sel);
            if (imp) goto done_unlock;
            curClass = curClass->cache.preoptFallbackClass();
#endif

为什么又要去缓存里面查找啊?之前不是已经汇编查找过了啊?因为在操作ro/rw的时候有可能写入了方法,所以这时候再去查看一遍,以防万一。

那么如果没有写入呢?没有就没有呗!那就继续往下执行代码。

// curClass method list.
            Method meth = getMethodNoSuper_nolock(curClass, sel);
            if (meth) {
                imp = meth->imp(false);
                goto done;
            }
getMethodNoSuper_nolock(Class cls, SEL sel)
{
    runtimeLock.assertLocked();

    ASSERT(cls->isRealized());
    // fixme nil cls? 
    // fixme nil sel?

    auto const methods = cls->data()->methods();
    for (auto mlists = methods.beginLists(),
              end = methods.endLists();
         mlists != end;
         ++mlists)
    {
        // <rdar://problem/46904873> getMethodNoSuper_nolock is the hottest
        // caller of search_method_list, inlining it turns
        // getMethodNoSuper_nolock into a frame-less function and eliminates
        // any store from this codepath.
        method_t *m = search_method_list_inline(*mlists, sel);
        if (m) return m;
    }

    return nil;
}

3.3 二分查找分析

在分析之前我们玩个小游戏,《猜猜猜》

在一次户外活动中,有编号1到100的盒子,其中有一个里面有奖品,猜是几号盒子有奖品,一共有五次机会。是你,你会怎么猜呢?下面是活动中胜出的猜测方法。

第一次猜: RENO:50 KC:小了
第二次猜: RENO:75 KC:大了
第三次猜: RENO:60 KC:大了
第四次猜: RENO:55 KC:对了

KC: 一共五次机会,第四次就猜中了,厉害啊!

厉害
这就是著名的二分查找法(Binary Search),也叫折半查找。下面源码里面findMethodInSortedMethodList就是这种算法实现的,可能刚刚那个游戏,你还无感知二分查找的魅力,看完下面👇的分析,你就能感知了。

search_method_list_inline - > findMethodInSortedMethodList - >
非M1电脑的找big的

findMethodInSortedMethodList
findMethodInSortedMethodList(SEL key, const method_list_t *list, const getNameFunc &getName)
{
    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)getName(probe);
        
        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)getName((probe - 1))) {
                probe--;
            }
            return &*probe;
        }
        
        if (keyValue > probeValue) {
            base = probe + 1;
            count--;
        }
    }
    
    return nil;
}

方法缓存查找到了就执行,goto done

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

既然找到方法了,不可能再去执行二分查找了,就会调用log_and_fill_cache方法,把它写入缓存中,提高下次查找速度。

done:
    if (fastpath((behavior & LOOKUP_NOCACHE) == 0)) {
#if CONFIG_USE_PREOPT_CACHES
        while (cls->cache.isConstantOptimizedCache(/* strict */true)) {
            cls = cls->cache.preoptFallbackClass();
        }
#endif
        log_and_fill_cache(cls, imp, sel, inst, curClass);
    }
log_and_fill_cache(Class cls, IMP imp, SEL sel, id receiver, Class implementer)
{
#if SUPPORT_MESSAGE_LOGGING
    if (slowpath(objcMsgLogEnabled && implementer)) {
        bool cacheIt = logMessageSend(implementer->isMetaClass(), 
                                      cls->nameForLogging(),
                                      implementer->nameForLogging(), 
                                      sel);
        if (!cacheIt) return;
    }
#endif
    cls->cache.insert(sel, imp, receiver);
}

4.总结

更多内容持续更新

🌹 喜欢就点个赞吧👍🌹

🌹 觉得学习到了的,可以来一波,收藏+关注,评论 + 转发,以免你下次找不到我😁🌹

🌹欢迎大家留言交流,批评指正,互相学习😁,提升自我🌹

上一篇 下一篇

猜你喜欢

热点阅读