- 大家都知道
objc_msgSend核心方法通过汇编实现,汇编查找缓存是快速查找,今天我们了解一波慢速查找!
1. 定位查找核心方法
话不多说上代码:
@interface XGTest : NSObject
- (void)say5;
@end
@implementation XGTest
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
XGTest *test = [XGTest alloc];
[test say5];
}
return 0;
}
-
- 我们今天的测试是调用一个没有实现的方法
say5, 这样可以更仔细的观察他走过的流程!(如果方法实现了,可以找到,那我们流程研究就不够细致!)
- 我们今天的测试是调用一个没有实现的方法
-
- 我们断点看汇编,看他如何查找方法!
- 我们断点看汇编,看他如何查找方法!
-
- 然后断住之后,我们查看汇编(操作方法如图)
- 然后断住之后,我们查看汇编(操作方法如图)
-
- 我们跟进去
- 我们跟进去
-
- 这样我们找到了我们要研究的方法
lookUpImpOrForward
- 这样我们找到了我们要研究的方法
2. lookUpImpOrForward 分析
老规矩,上代码
IMP lookUpImpOrForward(id inst, SEL sel, Class cls, int behavior)
{
const IMP forward_imp = (IMP)_objc_msgForward_impcache;
IMP imp = nil;
Class curClass;
runtimeLock.assertUnlocked();
// Optimistic cache lookup
if (fastpath(behavior & LOOKUP_CACHE)) {
imp = cache_getImp(cls, sel);
if (imp) goto done_nolock;
}
// runtimeLock is held during isRealized and isInitialized checking
// to prevent races against concurrent realization.
// runtimeLock is held during method search to make
// method-lookup + cache-fill atomic with respect to method addition.
// Otherwise, a category could be added but ignored indefinitely because
// the cache was re-filled with the old value after the cache flush on
// behalf of the category.
runtimeLock.lock();
// We don't want people to be able to craft a binary blob that looks like
// a class but really isn't one and do a CFI attack.
//
// To make these harder we want to make sure this is a class that was
// either built into the binary or legitimately registered through
// objc_duplicateClass, objc_initializeClassPair or objc_allocateClassPair.
checkIsKnownClass(cls);
if (slowpath(!cls->isRealized())) {
cls = realizeClassMaybeSwiftAndLeaveLocked(cls, runtimeLock);
// runtimeLock may have been dropped but is now locked again
}
if (slowpath((behavior & LOOKUP_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
}
runtimeLock.assertLocked();
curClass = cls;
// The code used to lookpu the class's cache again right after
// we take the lock but for the vast majority of the cases
// evidence shows this is a miss most of the time, hence a time loss.
//
// The only codepath calling into this without having performed some
// kind of cache lookup is class_getInstanceMethod().
for (unsigned attempts = unreasonableClassCount();;) {
// curClass method list.
Method meth = getMethodNoSuper_nolock(curClass, sel);
if (meth) {
imp = meth->imp(false);
goto done;
}
if (slowpath((curClass = curClass->superclass) == nil)) {
// No implementation found, and method resolver didn't help.
// Use forwarding.
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.");
}
// Superclass cache.
imp = cache_getImp(curClass, sel);
if (slowpath(imp == forward_imp)) {
// Found a forward:: entry in a superclass.
// Stop searching, but don't cache yet; call method
// resolver for this class first.
break;
}
if (fastpath(imp)) {
// Found the method in a superclass. Cache it in this class.
goto done;
}
}
// No implementation found. Try method resolver once.
if (slowpath(behavior & LOOKUP_RESOLVER)) {
behavior ^= LOOKUP_RESOLVER;
return resolveMethod_locked(inst, sel, cls, behavior);
}
done:
log_and_fill_cache(cls, imp, sel, inst, curClass);
runtimeLock.unlock();
done_nolock:
if (slowpath((behavior & LOOKUP_NIL) && imp == forward_imp)) {
return nil;
}
return imp;
}
2.1. 大致分析一波
-
- 这段代码最终返回
imp,说明,他的核心思想就是寻找他的imp
- 这段代码最终返回
-
- 我们简单的浏览下代码:首先赋值 定义
forward_imp,然后判断是否去缓存中查找。然后走到了for!
- 我们简单的浏览下代码:首先赋值 定义
-
- 大家看见这个
for循环不要慌,后面条件没有写,那他默认就是个死循环,只能通过break或者goto跳出去
- 大家看见这个
我们可以先大概了解一波流程,然后慢慢分析~
-
我上图,先看个大概!
-
- 我用普通话表示一波(😆):大致流程就是现在自身列表找,找到了就跳出去,找不到就向他的
superclass(父类)找,找到了也done,找不到继续,直到找到NSObject,还是找不到那就赋值forward_imp
- 我用普通话表示一波(😆):大致流程就是现在自身列表找,找到了就跳出去,找不到就向他的
2.2. 具体分析
那么,我们仔细研究下,具体代码是怎么实现的!!
-
- 获取方法的代码
Method meth = getMethodNoSuper_nolock(curClass, sel);
我们看下他怎么实现的吧:
-
- 好了,终于找到他怎么查找的了!
ALWAYS_INLINE static method_t *
findMethodInSortedMethodList(SEL key, const method_list_t *list)
{
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)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 &*probe;
}
if (keyValue > probeValue) {
base = probe + 1;
count--;
}
}
return nil;
}
核心思想不就是个二分查找法吗(二分查找还是快)
好多兄弟可能不了解 count >>= 1,: count = count>>1( 0000 1000 --> 0000 0100 : 8—>4)
其实就是count = count/2, 不过是人家用的位运算符。
-
- 这个时候,就需要来一波二分查找的图
- 这个时候,就需要来一波二分查找的图
-
- 我们继续看代码。
imp = cache_getImp(curClass, sel);
我们看里面的参数:
这个已经指到他的
superclass了
-
- 那么我们看下
cache_getImp已经点不进去了~(这个时候我们会想到,会不会是汇编处理了。那我们全局搜索)
- 那么我们看下
-
- 汇编
_cache_getImp
- 汇编
STATIC_ENTRY _cache_getImp
GetClassFromIsa_p16 p0
CacheLookup GETIMP, _cache_getImp
结合汇编的代码可以看出,会从父类找缓存。找不到的话就
ret(return),所以cache_getImp不走递归,而是通过for的的死循环遍历继承链
-
- for 循环基本说的差不多了
那就继续看图
- for 循环基本说的差不多了
方法查找流程差不多说完了~(毕竟我水平有限,可能讲的不细致,希望兄弟们开恩,😆)
3. 慢速查找流程总结
对于慢速查找,我画了个简陋的流程图~(我美术水平低的要死,兄弟萌不要嘲笑我)