• 详解objc_msgSend


    详解objc_msgSend

     1、为了性能,objc_msgSend用汇编写成。存在于objc-msg-x86_64.s中。

      

     2、在上图代码中可以看到,objc_msgSend被分为2个过程:1)在cache中寻找SEL。2)在MethodTable寻找SEL。

     3、CacheLookup中,不断地拿SEL与cache中的缓存比较,比较失败,则跳转到 LCacheMiss标签继续在MethodTable中搜索。

      

      如果想手动查找cache,则需要调用_cache_getimp函数(汇编实现),此函数是个对外接口层,用于保存与准备环境。

      

      _cache_getImp在头文件中objc-private.h中,链接后objc/c代码可以直接调用。

      

     4、MethodTableLookup 是个接口层宏,主要用于保存环境与准备参数,来调用 __class_lookupMethodAndLoadCache3函数(此函数实现于objc-class.mm)。

      

     5、__class_lookupMethodAndLoadCache3函数也是个接口层(C编写),此函数提供相应参数配置,实际功能在lookUpMethod函数中。

      

     6、lookUpMethod函数实现遍历method_list_t,从子类开始,一直遍历到根类。此函数代码较大,不贴图了。文件在objc-class中。

    Cache Of lookUpMethod

      To speed the messaging process, the runtime system caches the selectors and addresses of methods as they are used. There’s a separate cache for each class, and it can contain selectors for inherited methods as well as for methods defined in the class. Before searching the dispatch tables, the messaging routine first checks the cache of the receiving object’s class (on the theory that a method that was used once may likely be used again). If the method selector is in the cache, messaging is only slightly slower than a function call. Once a program has been running long enough to “warm up” its caches, almost all the messages it sends find a cached method. Caches grow dynamically to accommodate new messages as the program runs.

  • 相关阅读:
    linux中WDCP的日志彻底删除技巧
    jquery中页面Ajax方法$.load的功能
    PHP与MYSQL中UTF8 中文排序例子
    mysql获取group by的总记录行数方法
    jquery获取radio值
    python中数组与多维数组用法介绍
    php开启与关闭错误提示
    asp.net url址址中中文汉字参数传递乱码解决方法
    js事件之event.preventDefault()与(www.111cn.net)event.stopPropagation()用法区别
    css中position:fixed实现div居中
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3094549.html
Copyright © 2020-2023  润新知