回顾
紧跟msgSend底层(一)方法的快速查找 cache中,我们探究了Runtime
快速查找缓存的方法,当缓存没有找到时会进行慢速查找
。下面先接着上面的汇编看看
__objc_msgSend_uncached
- 方法快速查找找不到时,会走
MissLabelDynamic
方法,而这个方法对应传入的是__objc_msgSend_uncached
,
STATIC_ENTRY __objc_msgSend_uncached
UNWIND __objc_msgSend_uncached, FrameWithNoSaves
// THIS IS NOT A CALLABLE C FUNCTION
// Out-of-band p15 is the class to search
MethodTableLookup
TailCallFunctionPointer x17
END_ENTRY __objc_msgSend_uncached
- 这里有2个方法,先看下
TailCallFunctionPointer
,只有一个跳转
.macro TailCallFunctionPointer
// $0 = function pointer value
br $0
.endmacro
- 再看下
MethodTableLookup
.macro MethodTableLookup
SAVE_REGS MSGSEND
// lookUpImpOrForward(obj, sel, cls, LOOKUP_INITIALIZE | LOOKUP_RESOLVER)
// receiver and selector already in x0 and x1
mov x2, x16
mov x3, #3
bl _lookUpImpOrForward
// IMP in x0
mov x17, x0 // 将x0赋值,给x17
RESTORE_REGS MSGSEND
.endmacro
先看mov x17, x0
,x0
是第一个寄存器,是接收返回值的,所以imp
肯定是在上面返回的,然后我们定位到_lookUpImpOrForward
注:
- 汇编代码
_lookUpImpOrForward
对应C++代码lookUpImpOrForward
(就是去掉下划线_) - 为什么查找缓存用汇编写呢?
-
- 汇编更接近机器语言,运行的速度比较快,更加动态化\
-
- 比较安全\
-
- 查找方法时,很多参数是动态的不确定的,
C或C++
参数都是确定的,而汇编的参数是动态化的,刚好满足。
- 查找方法时,很多参数是动态的不确定的,
-
lookUpImpOrForward(慢速查找)
lookUpImpOrForward
根据sel
查询imp
,具体是怎么查询imp
IMP lookUpImpOrForward(id inst, SEL sel, Class cls, int behavior)
{
//定义消息转发forward_imp //behavior传入的是 3 = LOOKUP_INITIALIZE|LOOKUP_RESOLVER
const IMP forward_imp = (IMP)_objc_msgForward_impcache;
IMP imp = nil;
Class curClass;
runtimeLock.assertUnlocked();
/*
//判断类是否初始化 没有初始化 behavior = LOOKUP_NOCACHE|LOOKUP_INITIALIZE|LOOKUP_RESOLVER
//发送给类的第一条消息通常是+new或+alloc或+self会初始化类
*/
if (slowpath(!cls->isInitialized())) {
behavior |= LOOKUP_NOCACHE;
}
//加锁防止多线程访问出现错乱
runtimeLock.lock();
checkIsKnownClass(cls); //是否注册类 是否被dyld加载的类
//实现类包括实现isa走位中的父类和元类 //初始化类和父类
cls = realizeAndInitializeIfNeeded_locked(inst, cls, behavior & LOOKUP_INITIALIZE);
runtimeLock.assertLocked();
curClass = cls;
//获取锁后,代码再次查找类的缓存,但绝大多数情况下,证据表明大部分时间都未命中,因此浪费时间。
//唯一没有执行某种缓存查找的代码路径就是class_getInstanceMethod()。
// unreasonableClassCount 类迭代上限 根据翻译来的
for (unsigned attempts = unreasonableClassCount();;) {
//判断是否有共享缓存缓存优化,一般是系统的方法比如NSLog,一般的方法不会走
if (curClass->cache.isConstantOptimizedCache(/* strict */true)) {
#if CONFIG_USE_PREOPT_CACHES
/*
再一次查询共享缓存,目的可能在你查询过程中
别的线程可能调用了这个方法共享缓存中有了直接去查询
*/
imp = cache_getImp(curClass, sel);//缓存中根据sel查询imp
//如果imp存在即缓存中有 跳转到done_unlock流程
if (imp) goto done_unlock;
//具体干啥不知道我感觉是获取父类的里面是进行的偏移
curClass = curClass->cache.preoptFallbackClass();
#endif
}
else {
// curClass method list.
// 在curClass类中采用二分查找算法查找methodlist
Method meth = getMethodNoSuper_nolock(curClass, sel);
if (meth) { // 如果找到了sel对应的方法
imp = meth->imp(false);//获取对应的imp
goto done; //跳转到 done 流程
}
// curClass = curClass->getSuperclass() 直到为nil走if里面的流程,不为nil走下面流程 这时候 curClass被赋值为了父类
if (slowpath((curClass = curClass->getSuperclass()) == nil)) {
//就是在循环里面没有找到对应的sel的方法,把定义息转发forward_imp赋值给imp
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.");
}
// 去父类的缓存中查找imp
imp = cache_getImp(curClass, sel);
if (slowpath(imp == forward_imp)) {
//如果父类返回的是forward_imp 停止查找,那么就跳出循环
break;
}
if (fastpath(imp)) {
//如果缓存中有就跳转done流程
goto done;
}
}
// No implementation found. Try method resolver once.
// 如果查询方法的没有实现,系统会尝试一次方法解析
// behavior = 3 LOOKUP_RESOLVER = 2
// behavior & LOOKUP_RESOLVER = 3 & 2 = 2 所以成立进入条件
// 再次进入behavior = 1 & 2 = 0 不会在进入条件里面只执行一次动态方法决议
if (slowpath(behavior & LOOKUP_RESOLVER)) {
//behavior = behavior ^ LOOKUP_RESOLVER = 3 ^ 2 = 1
behavior ^= LOOKUP_RESOLVER;
//动态方法决议
return resolveMethod_locked(inst, sel, cls, behavior);
}
done:
//(behavior & LOOKUP_NOCACHE) == 0 成立 behavior = 3
//LOOKUP_NOCACHE = 8 所以(behavior & LOOKUP_NOCACHE) = 0
if (fastpath((behavior & LOOKUP_NOCACHE) == 0)) {
#if CONFIG_USE_PREOPT_CACHES
while (cls->cache.isConstantOptimizedCache(/* strict */true)) {
cls = cls->cache.preoptFallbackClass();
}
#endif
//将查询到的sel和imp插入到缓存 注意:插入的是当前类的缓存
log_and_fill_cache(cls, imp, sel, inst, curClass);
}
done_unlock:
//解锁
runtimeLock.unlock();
/*
如果 (behavior & LOOKUP_NIL)成立则 behavior != LOOKUP_NIL
且imp == forward_imp 没有查询到直接返回 nil
*/
if (slowpath((behavior & LOOKUP_NIL) && imp == forward_imp)) {
return nil;
}
return imp;
}
慢速查找流程
- 是否注册类,如果没有直接报错
- 是否实现
cls
,如果没有实现,则先去实现类
以及相关的isa走位链
和isa继承链
中类的实现,目的是方法查找时到父类中去查询 - 类是否初始化,如果没有则初始化,这一步我觉着是创建类对象比如调用类方法时,就是类对象调用实例方法
cls
开始遍历查询
- 判断是否有共享缓存,目的是有可能在查过过程中这个方法被调用缓存了,如果有的话直接从缓存中取,没有共享缓存则开始到本类中查询
- 在类中采用二分查找算法查找
methodlist
中的方法,如果找到插入缓存中,循环结束
父类
缓存中查询
- 如果父类中存在循环则终止查询,跳出循环
- 此时
curClass
=superclass
,到父类的缓存中找,如果找到则插入到本类
的缓存中。如果父类中返回的是forward_imp
则跳出遍历,执行消息转发 - 如果本类中没有找到此时的
curClass
=superclass
进入和cls
类相同的查找流程进行遍历循环,直到curClass
=nil
,imp
=forward_imp
进行消息转发
realizeAndInitializeIfNeeded_locked
static Class
realizeAndInitializeIfNeeded_locked(id inst, Class cls, bool initialize)
{
runtimeLock.assertLocked();
// !cls->isRealized()小概率发生 cls->isRealized()大概率是YES
//判断类是否实现 目的是实现isa走位图中的isa走位链和父类链
if (slowpath(!cls->isRealized())) {
cls = realizeClassMaybeSwiftAndLeaveLocked(cls, runtimeLock);
// runtimeLock may have been dropped but is now locked again
}
// 类是否初始化 没有先去初始化
if (slowpath(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
}
return cls;
realizeClassWithoutSwift
}
getMethodNoSuper_nolock
- 这里根据类拿到方法列表,然后从
beginLists
往endLists
开始遍历,每查找一次,都会调用search_method_list_inline
方法
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)// 从 从beginLists往endLists开始遍历
{
// <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;
}
findMethodInSortedMethodList
二分查找
- 在
search_method_list_inline
里查看排序过的方法,最终找到了findMethodInSortedMethodList
,这是一个著名的二分查找算法方法列表中的方是经过修复的,意思就是按照sel
大小进行过排序的二分法查找算法
其实就是每次找到范围内的中间
位置和keyValue
比较,如果相等直接返回查找到的方法(当然如果有分类方法就返回分类方法)- 如果不相等则继续二分法查询,不断缩小查询的范围,如果最后还是没有查询到则返回
nil
ALWAYS_INLINE static method_t *
findMethodInSortedMethodList(SEL key, const method_list_t *list, const getNameFunc &getName)
{
ASSERT(list);
auto first = list->begin(); //第一个method的位置
auto base = first;
decltype(first) probe; //相当于mid
//把key直接转换成uintptr_t 因为修复过后的method_list_t中的元素是排过序的
uintptr_t keyValue = (uintptr_t)key;
uint32_t count;
//count = 数组的个数 (count >>= 1 = count = count >> 1)
//count >> 1 就相当于(count/2) 取整
//1.假如 count = list->count = 8 //2 count = 7 >> 1 = 3
for (count = list->count; count != 0; count >>= 1) {
/*
1. 首地址 + (下标) //地址偏移 中间值 probe = base + 4
2. 中间值 probe = base(首地址)+ 6
*/
probe = base + (count >> 1);
//获取中间的sel的值也是强转后的值
uintptr_t probeValue = (uintptr_t)getName(probe);
if (keyValue == probeValue) {// 如果 目标key == 中间位置的key 匹配成功
//分类覆盖,分类中有相同名字的方法,如果有分类的方法我们就获取分类的方法,多个分类看编译的顺序
while (probe > first && keyValue == (uintptr_t)getName((probe - 1))) {
probe--;
}
//返回方法的地址
return &*probe;
}
//如果 keyValue > 中间的位置的值
if (keyValue > probeValue) {
/*
1.base = probe + 1 = 4 + 1 = base(首地址) + 5 向上移 一位
2.base = probe + 1 ;向上移 一位
*/
base = probe + 1;
// 8 -1 = 7 因为比过一次没中 然后循环
count--;
}
}
return nil;//查询完没找到返回nil
}
- 二分查找流程图
cache_getImp(没找到imp)
方法快速查找流程是汇编源码实现的
STATIC_ENTRY _cache_getImp
// `GetClassFromIsa_p16`宏定义和我们开始在本类中查询缓存方法一样,但是参数不一样 `needs_auth` = `0`
GetClassFromIsa_p16 p0, 0
CacheLookup GETIMP, _cache_getImp, LGetImpMissDynamic, LGetImpMissConstant
LGetImpMissDynamic:
mov p0, #0
ret
直接走needs_auth
==0
,p0
=curClass
。把p0
寄存器的值赋值给p16
寄存器,p16
= curClass
CacheLookup
方法前面已经探究过了在这不细说了CacheLookup GETIMP
, _cache_getImp
, LGetImpMissDynamic
, LGetImpMissConstant
- 如果缓存没有命中走
LGetImpMissDynamic
流程 - 如果缓存命中
Mode
=GETIMP
LGetImpMissDynamic
流程p0
=0
,就是没有查到缓存就是返回imp
=nil
.macro CacheHit
.if $0 == NORMAL
TailCallCachedImp x17, x10, x1, x16 // authenticate and call imp
.elseif $0 == GETIMP
mov p0, p17
cbz p0, 9f //如果imp = 0直接跳转9流程 return 0
AuthAndResignAsIMP x0, x10, x1, x16 // authenticate imp and re-sign as IMP
9: ret
p17
=imp
,把p17
寄存器的值赋值给p0
寄存器,x0
=p0
=imp
- 如果
imp
=0
直接跳转9
流程return 0
AuthAndResignAsIMP
也是一个宏
,对imp
进行解码,拿到解码后的imp
返回
.macro AuthAndResignAsIMP
// $0 = cache imp , $1 = buckets的地址, $2 = SEL $3 = class
// $0 = $0 ^ $3 = imp ^ class = 解码后的imp
eor $0, $0, $3
.endmacro
缓存中获取imp
是编码过的,此时imp
^ class
= 解码后的imp