Objc_msgSend 分析(一)

473 阅读1分钟

问题/目标分析

探寻objc_msgSend的真实实现过程。

方法分析

根据objc源码来查找objc_msgSend的真正实现过程。

实验步骤

1、打一个断点。

2、打开debug汇编代码。

3、在msgsend打一个断点。

4、ctrl+下箭头,进入msgsend汇编指令,看到msgsend相关lib。

5、在源码中搜索objc_msgSend 找到带s标志的汇编源码

6、分析源码流程
找到方法就返回调用imp,没有就进入 __objc_msgSend_uncached的MethodTableLookup
进入__objc_msgSend_uncached的MethodTableLookup
在MethodTableLookup中调用c++的 _class_lookupMethodAndLoadCache3
7、在汇编代码中找到对应的文件

结果分析

objc_msgSend
会根据参数 id,SEL获取isa中的缓存信息 buckets
首先会在缓存中搜索是否有缓存的方法 如果有就调用
如果没有 就调用c++的 _class_lookupMethodAndLoadCache3方法进行缓慢查找

总结实验

因为isa中的cachet的缘故,有缓存的方法调用会更快.

至于慢速的方法查找流程看:Objc_msgSend 分析(二)