美文网首页
方法查找

方法查找

作者: Code_人生 | 来源:发表于2019-09-29 16:15 被阅读0次
  • 1、搜索lookUpImpOrForward
IMP lookUpImpOrForward(Class cls, SEL sel, id inst, 
                       bool initialize, bool cache, bool resolver)
{
//省略
    // Try this class's method lists.
    {
        //Method(SEL IMP)
        Method meth = getMethodNoSuper_nolock(cls, sel);
        if (meth) {
            log_and_fill_cache(cls, meth->imp, sel, inst, cls);
            imp = meth->imp;
            goto done;
        }
    }
//省略
}
  • 2、点击getMethodNoSuper_nolock
static method_t *
getMethodNoSuper_nolock(Class cls, SEL sel)
{
    runtimeLock.assertLocked();

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

    //类对象当中的方法列表~
    for (auto mlists = cls->data()->methods.beginLists(), 
              end = cls->data()->methods.endLists(); 
         mlists != end;
         ++mlists)
    {
        //Method_t : sel  = sel
        method_t *m = search_method_list(*mlists, sel);
        if (m) return m;
    }

    return nil;
}
  • 3、点击search_method_list
  • __builtin_expect(methodListIsFixedUp && methodListHasExpectedSize, 1) 绝大多数是有序的,如果是无序的就遍历
static method_t *search_method_list(const method_list_t *mlist, SEL sel)
{
    int methodListIsFixedUp = mlist->isFixedUp();
    int methodListHasExpectedSize = mlist->entsize() == sizeof(method_t);
    
    
    if (__builtin_expect(methodListIsFixedUp && methodListHasExpectedSize, 1)) {
        return findMethodInSortedMethodList(sel, mlist);
    } else {
        // Linear search of unsorted method list
        // run
        for (auto& meth : *mlist) {
            if (meth.name == sel) return &meth;
        }
    }

#if DEBUG
    // sanity-check negative results
    if (mlist->isFixedUp()) {
        for (auto& meth : *mlist) {
            if (meth.name == sel) {
                _objc_fatal("linear search worked when binary search did not");
            }
        }
    }
#endif

    return nil;
}
  • 4、点击findMethodInSortedMethodList
//二分查找!!!
static method_t *findMethodInSortedMethodList(SEL key, const method_list_t *list)
{
    assert(list);

    const method_t * const first = &list->first;
    const method_t *base = first;
    //临时变量
    const method_t *probe;
    uintptr_t keyValue = (uintptr_t)key;
    uint32_t count;
    
    //count :偶数/ 2
    //二分查找!!!
    //count >>= 1
    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 (method_t *)probe;
        }
        
        if (keyValue > probeValue) {
            base = probe + 1;
            count--;
        }
    }
    
    return nil;
}

相关文章

  • iOS 消息转发流程

    runtime方法查找流程及消息转发 方法查找 方法查找的流程:缓存查找-->当前类查找-->父类逐级查找 1.缓...

  • runtime方法查找流程及消息转发

    方法查找 方法查找的流程:缓存查找-->当前类查找-->父类逐级查找 1.缓存 看看缓存中是否有对应的方法实现...

  • js和JQuery对DOM增删改查的对比

    查找 JS方法1 查找节点1 查找节点组1 JS方法2 查找节点2 查找节点组2 小结 根据JS和JQuery的对...

  • 消息查找

    类查找 先查找类中的方法然后再查找分类方法,分类中的方法会添加到类中的方法列表后面 lookUpImpOrForw...

  • iOS 消息转发机制

    上节(iOS 消息查找流程)我们讲到,在iOS中对象调用方法,会经历方法的查找,如果查找到方法的IMP,那么就返回...

  • 方法查找

    1、搜索lookUpImpOrForward 2、点击getMethodNoSuper_nolock 3、点击se...

  • 检索算法

    顺序查找 自组织数据查找方法 二分查找方法 前提是要对数组进行排好序

  • Kotlin 链式存储的二叉树中查找节点

    1. 查找方法 链式存储的二叉树中查找节点的方法可分为三种:前序查找、中序查找、后序查找,下面使用 Kotlin ...

  • HashMap源码解析 (HashMap类-get方法)

    查找元素方法 get( ) 查找方法,通过元素的 key 找到 value get 方法主要调用的是 getNod...

  • (2021 objc4-818源码分析)方法查找流程-动态决议&

    方法查找流程 【第一步】 方法查找流程-快速查找流程[https://www.jianshu.com/p/8cfa...

网友评论

      本文标题:方法查找

      本文链接:https://www.haomeiwen.com/subject/jwolyctx.html