前言
- 這篇主要內(nèi)容是分析消息轉(zhuǎn)發(fā)機(jī)制
objc_msgSend
趁餐。之前分析了cache_t的流程,只是探討了Cache writers蜜徽,這篇就來分析cache_fill之前的objc_msgSend廓奕,并引入方法本質(zhì)的探索勃教。
準(zhǔn)備工作
- 復(fù)習(xí):iOS 底層探索:方法緩存(cache_t)的分析
一 科汗、了解runtime
在分析objc_msgSend 之前酿秸,必須了解一下runtime的定義和基本內(nèi)容。
- runtime的官方文檔:Objective-C Runtime Programming Guide
- runtime版本信息
Runtime有兩個(gè)版本 ?個(gè)Legacy版本(早期版本) 嵌言,?個(gè)Modern版本(現(xiàn)?版本)
- 早期版本對(duì)應(yīng)的編程接?:Objective-C 1.0
- 現(xiàn)?版本對(duì)應(yīng)的編程接?:Objective-C 2.0
- 早期版本?于Objective-C 1.0, 32位的Mac OS X的平臺(tái)上
- 現(xiàn)?版本:iPhone程序和Mac OS X v10.5 及以后的系統(tǒng)中的 64 位程序
- runtime 介紹:
將源代碼轉(zhuǎn)換為可執(zhí)行的程序嗅回,通常要經(jīng)過三個(gè)步驟:
編譯
、鏈接
摧茴、運(yùn)行
绵载。不同的編譯語言,在這三個(gè)步驟中所進(jìn)行的操作又有些不同苛白。
編譯時(shí)
顧名思義就是正在編譯的時(shí)候 . 那啥叫編譯呢?就是編譯器幫你把源代碼翻譯成機(jī)器能識(shí)別的代碼 . 主要是對(duì)語言進(jìn)行最基本的檢查報(bào)錯(cuò)尘分,即詞法分析、語法分析等丸氛,是一個(gè)靜態(tài)的階段
運(yùn)?時(shí)
就是代碼跑起來了.被裝載到內(nèi)存中去了 .
Objective-C 語言
把一些決定性的工作從編譯階段培愁、鏈接階段推遲到運(yùn)行時(shí)階段的機(jī)制
,使得 Objective-C 變得更加靈活缓窜。而實(shí)現(xiàn) Objective-C 語言
運(yùn)行時(shí)機(jī)制
的一切基礎(chǔ)就是Runtime
定续。
Runtime
實(shí)際上是一個(gè)庫(kù)谍咆,這個(gè)庫(kù)使我們可以在程序運(yùn)行時(shí)動(dòng)態(tài)的創(chuàng)建對(duì)象
、檢查對(duì)象
私股,修改類
和對(duì)象的方法
摹察。
-
4 .runtime 在oc中編譯層和底層的關(guān)系圖:
注:其中的compiler就是編譯器LLVM, runtime system libarary 就是底層庫(kù)倡鲸。
- runtime 常用使用方式:
通過OC代碼供嚎,例如 :
[person doSometing]
通過NSObject方法,例如:
isKindOfClass
通過Runtime API峭状,例如:
class_getInstanceSize
二 克滴、方法本質(zhì)的探索
- 利用
clang
查看方法編譯之后的代碼:
- 利用
clang使用步驟: isa與類關(guān)聯(lián)的原理 有提過。
// - (void)sayHello;
// - (void)sayNB;
// + (void)sayCool;
/* @end */
// @implementation LGPerson
static void _I_LGPerson_sayNB(LGPerson * self, SEL _cmd) {
NSLog((NSString *)&__NSConstantStringImpl__var_folders_pw_vld75tl93j1cx5xjwrjd80f00000gn_T_main_350155_mi_1);
}
static void _C_LGPerson_sayCool(Class self, SEL _cmd) {
NSLog((NSString *)&__NSConstantStringImpl__var_folders_pw_vld75tl93j1cx5xjwrjd80f00000gn_T_main_ebda0d_mi_2);
}
// @end
//main.m中方法的調(diào)用
LGPerson *person = [LGPerson alloc];
[person sayNB];
[person sayHello];
[LGPerson sayCool];
//clang編譯后的底層實(shí)現(xiàn)
LGPerson *person = ((LGPerson *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("LGPerson"), sel_registerName("alloc"));
((void (*)(id, SEL))(void *)objc_msgSend)((id)person, sel_registerName("sayNB"));
((void (*)(id, SEL))(void *)objc_msgSend)((id)person, sel_registerName("sayHello"));
((void (*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("LGPerson"), sel_registerName("sayCool"));
可以看出:不論是類方法還是對(duì)象方法优床,被編譯后都是執(zhí)行objc_msgSend劝赔;所以方法的本質(zhì)
就是objc_msgSend
消息發(fā)送;
結(jié)論:Objective-C
方法調(diào)用在編譯時(shí)
都會(huì)轉(zhuǎn)化為對(duì) C 函數(shù)objc_msgSend
的調(diào)用。objc_msgSend(receiver胆敞,selector)
是[receiver selector]
對(duì)應(yīng)的C 函數(shù)着帽。
注:
- 1、直接調(diào)用
objc_msgSend
移层,需要導(dǎo)入頭文件#import <objc/message.h>
,其中包含了#include <objc/runtime.h>
- 2仍翰、需要將target --> Build Setting -->搜索msg -- 將enable strict checking of obc_msgSend calls由YES 改為NO,將嚴(yán)厲的檢查機(jī)制關(guān)掉观话,否則objc_msgSend的參數(shù)會(huì)報(bào)錯(cuò)
3.拓展:子類調(diào)用父類的對(duì)象方法(objc_msgSend 和 objc_msgSendSuper)
注:解析objc_msgSendSuper
OBJC_EXPORT id _Nullable
objc_msgSendSuper(struct objc_super * _Nonnull super, SEL _Nonnull op, ...)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
#endif
/// Specifies the superclass of an instance. 指定實(shí)例的父類歉备。
struct objc_super {
/// Specifies an instance of a class. 指定類的實(shí)例。
__unsafe_unretained _Nonnull id receiver;
/// Specifies the particular superclass of the instance to message. 指定message實(shí)例的特定父類匪燕。
#if !defined(__cplusplus) && !__OBJC2__
/* For compatibility with old objc-runtime.h header */
__unsafe_unretained _Nonnull Class class;
#else
__unsafe_unretained _Nonnull Class super_class;
#endif
/* super_class is the first class to search */
};
#endif
-
objc_msgSendSuper
方法中有兩個(gè)參數(shù)(結(jié)構(gòu)體
蕾羊,sel
),其結(jié)構(gòu)體類型是objc_super
定義的結(jié)構(gòu)體對(duì)象帽驯,且需要指定receiver
和super_class
兩個(gè)屬性
4 : 提出疑問:為什么LGPerson類中沒有sayHello 方法 龟再,卻還是能找到父類LGTeacher的sayHello的呢?是怎么找到的呢尼变?是如何執(zhí)行的呢利凑? 帶著疑問我們開始了objc_msgSend
的快速查找原理
的探索。
二 嫌术、探索objc_msgSend 快速查找流程
老辦法查看源碼:在objc4-781
源碼中哀澈,搜索objc_msgSend,由于我們?nèi)粘i_發(fā)的都是架構(gòu)是arm64度气,所以需要在arm64.s
后綴的文件中查找objc_msgSend源碼實(shí)現(xiàn),打開一看全是匯編割按,瞬間懵逼。arm64匯編指令集如圖:
在源碼進(jìn)行注釋:
/*
---- 消息發(fā)送 -- 匯編入口--objc_msgSend主要是拿到接收者的isa信息
*/
ENTRY _objc_msgSend
/*
---- 無窗口
*/
UNWIND _objc_msgSend, NoFrame
/*
---- p0 和空#0對(duì)比磷籍,即判斷接收者是否存在适荣,其中p0是objc_msgSend的第一個(gè)參數(shù)-消息接收者receiver
*/
cmp p0, #0 // nil check and tagged pointer check
//---- le小于 --支持taggedpointer(小對(duì)象類型)的流程
#if SUPPORT_TAGGED_POINTERS
b.le LNilOrTagged // (MSB tagged pointer looks negative)
#else
//---- p0 等于 0 時(shí)现柠,直接返回 空
b.eq LReturnZero
#endif
//---- p0即receiver 肯定存在的流程
//---- 根據(jù)對(duì)象拿出isa ,即從x0寄存器指向的地址 取出 isa弛矛,存入 p13寄存器
ldr p13, [x0] // p13 = isa
//---- 在64位架構(gòu)下通過 p16 = isa(p13) & ISA_MASK够吩,拿出shiftcls信息,得到class信息
GetClassFromIsa_p16 p13 // p16 = class
LGetIsaDone:
// calls imp or objc_msgSend_uncached
//---- 如果有isa丈氓,走到CacheLookup 即緩存查找流程周循,也就是所謂的sel-imp快速查找流程
CacheLookup NORMAL, _objc_msgSend
#if SUPPORT_TAGGED_POINTERS
LNilOrTagged:
//---- 等于空,返回空
b.eq LReturnZero // nil check
// tagged
adrp x10, _objc_debug_taggedpointer_classes@PAGE
add x10, x10, _objc_debug_taggedpointer_classes@PAGEOFF
ubfx x11, x0, #60, #4
ldr x16, [x10, x11, LSL #3]
adrp x10, _OBJC_CLASS_$___NSUnrecognizedTaggedPointer@PAGE
add x10, x10, _OBJC_CLASS_$___NSUnrecognizedTaggedPointer@PAGEOFF
cmp x10, x16
b.ne LGetIsaDone
// ext tagged
adrp x10, _objc_debug_taggedpointer_ext_classes@PAGE
add x10, x10, _objc_debug_taggedpointer_ext_classes@PAGEOFF
ubfx x11, x0, #52, #8
ldr x16, [x10, x11, LSL #3]
b LGetIsaDone
// SUPPORT_TAGGED_POINTERS
#endif
- 我們想在平時(shí)寫oc的方法的時(shí)候假如需要傳參万俗,是不是也需要對(duì)參數(shù)進(jìn)行判斷湾笛,提高健壯性。當(dāng)然在匯編中也是一樣该编,我們?cè)谑褂?code>objc_msgSend(receiver迄本,selector); 也需要判斷傳入receiver和selector硕淑;
- 我們需要明確我們我們的目的:找到cache中緩存的方法课竣。
弄清這兩點(diǎn)之后,我們?cè)偃ヂ庾x源代碼objc_msgSend置媳。
流程解析:
【步驟1】:判斷objc_msgSend
方法的第一個(gè)參數(shù)receiver
是否為空:
- 如果支持tagged pointer于樟,跳轉(zhuǎn)至LNilOrTagged;
- 如果小對(duì)象為空拇囊,則直接返回空迂曲,即LReturnZero
【步驟2】:receiver
不為空,從receiver
中取出isa
寥袭,通過GetClassFromIsa_p16
中路捧,arm64架構(gòu)下通過isa & ISA_MASK
獲取shiftcls位域
的類信息
,執(zhí)行 CacheLookup NORMAL, _objc_msgSend
注:
ldr p13, [x0] // p13 = isa
GetClassFromIsa_p16 p13 // p16 = class這兩句的意思就是將isa 賦值給13號(hào)寄存器传黄,通過13號(hào)寄存器中的isa杰扫,找到類信息,然后將類信息賦值給16號(hào)寄存器膘掰。
GetClassFromIsa_p16源碼如下:
.macro GetClassFromIsa_p16 /* src */
#if SUPPORT_INDEXED_ISA
// Indexed isa
//---- 將isa的值存入p16寄存器
mov p16, $0 // optimistically set dst = src
tbz p16, #ISA_INDEX_IS_NPI_BIT, 1f // done if not non-pointer isa -- 判斷是否是 nonapointer isa
// isa in p16 is indexed
//---- 將_objc_indexed_classes所在的頁的基址 讀入x10寄存器
adrp x10, _objc_indexed_classes@PAGE
//---- x10 = x10 + _objc_indexed_classes(page中的偏移量) --x10基址 根據(jù) 偏移量 進(jìn)行 內(nèi)存偏移
add x10, x10, _objc_indexed_classes@PAGEOFF
//---- 從p16的第ISA_INDEX_SHIFT位開始章姓,提取 ISA_INDEX_BITS 位 到 p16寄存器,剩余的高位用0補(bǔ)充
ubfx p16, p16, #ISA_INDEX_SHIFT, #ISA_INDEX_BITS // extract index
ldr p16, [x10, p16, UXTP #PTRSHIFT] // load class from array
1:
//--用于64位系統(tǒng)
#elif __LP64__
// 64-bit packed isa
//---- p16 = class = isa & ISA_MASK(位運(yùn)算 & 即獲取isa中的shiftcls信息)
and p16, $0, #ISA_MASK
#else
// 32-bit raw isa ---- 用于32位系統(tǒng)
mov p16, $0
#endif
.endmacro
【步驟3】 :緩存方法的查找
CacheLookup 緩存查找匯編源碼
.macro CacheLookup
// GETIMP:
// The cache-miss is just returning NULL (setting x0 to 0)
//
// NORMAL and LOOKUP:
// - x0 contains the receiver
// - x1 contains the selector
// - x16 contains the isa
// - other registers are set as per calling conventions
//
LLookupStart$1:
/****------------------step1---------------------****/
//---- p1 = SEL, p16 = isa --- #define CACHE (2 * __SIZEOF_POINTER__)识埋,其中 __SIZEOF_POINTER__表示pointer的大小 凡伊,即 2*8 = 16
//---- p11 = mask|buckets -- 從x16(即isa)中平移16字節(jié),取出cache 存入p11寄存器 -- isa距離cache 正好16字節(jié):isa(8字節(jié))-superClass(8字節(jié))-cache(mask高16位 + buckets低48位)
ldr p11, [x16, #CACHE]
/****------------------step2---------------------****/
//---- 64位真機(jī)
#if CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_HIGH_16
//--- p11(cache) & 0x0000ffffffffffff 窒舟,mask高16位抹零系忙,得到buckets 存入p10寄存器-- 即去掉mask,留下buckets
and p10, p11, #0x0000ffffffffffff // p10 = buckets
//--- p11(cache)右移48位惠豺,得到mask(即p11 存儲(chǔ)mask)笨觅,mask & p1(msgSend的第二個(gè)參數(shù) cmd-sel) 拦耐,得到sel-imp的下標(biāo)index(即搜索下標(biāo)) 存入p12(cache insert寫入時(shí)的哈希下標(biāo)計(jì)算是 通過 sel & mask,讀取時(shí)也需要通過這種方式)
and p12, p1, p11, LSR #48 // x12 = _cmd & mask
//--- 非64位真機(jī)
#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_LOW_4
and p10, p11, #~0xf // p10 = buckets
and p11, p11, #0xf // p11 = maskShift
mov p12, #0xffff
lsr p11, p12, p11 // p11 = mask = 0xffff >> p11
and p12, p1, p11 // x12 = _cmd & mask
#else
#error Unsupported cache mask storage for ARM64.
#endif
/****------------------step3---------------------****/
//--- p12是下標(biāo) p10是buckets數(shù)組首地址见剩,下標(biāo) * 1<<4(即16) 得到實(shí)際內(nèi)存的偏移量杀糯,通過buckets的首地址偏移,獲取bucket存入p12寄存器
//--- LSL #(1+PTRSHIFT)-- 實(shí)際含義就是得到一個(gè)bucket占用的內(nèi)存大小 -- 相當(dāng)于mask = occupied -1-- _cmd & mask -- 取余數(shù)
add p12, p10, p12, LSL #(1+PTRSHIFT)
// p12 = buckets + ((_cmd & mask) << (1+PTRSHIFT)) -- PTRSHIFT是3
//--- 從x12(即p12)中取出 bucket 分別將imp和sel 存入 p17(存儲(chǔ)imp) 和 p9(存儲(chǔ)sel)
ldp p17, p9, [x12] // {imp, sel} = *bucket
/****------------------step4---------------------****/
//--- 比較 sel 與 p1(傳入的參數(shù)cmd)
1: cmp p9, p1 // if (bucket->sel != _cmd)
//--- 如果不相等苍苞,即沒有找到固翰,請(qǐng)?zhí)D(zhuǎn)至 2f
b.ne 2f // scan more
//--- 如果相等 即執(zhí)行cacheHit ,直接返回imp
CacheHit $0 // call or return imp
/****------------------step5 和 step6---------------------****/
2: // not hit: p12 = not-hit bucket
//--- 如果一直都找不到羹呵, 因?yàn)槭莕ormal 骂际,跳轉(zhuǎn)至__objc_msgSend_uncached
CheckMiss $0 // miss if bucket->sel == 0
//--- 判斷p12(下標(biāo)對(duì)應(yīng)的bucket) 是否 等于 p10(buckets數(shù)組第一個(gè)元素,)冈欢,如果等于歉铝,則跳轉(zhuǎn)至第3步
cmp p12, p10 // wrap if bucket == buckets
//--- 定位到最后一個(gè)元素(即第一個(gè)bucket)
b.eq 3f
//--- 從x12(即p12 buckets首地址)- 實(shí)際需要平移的內(nèi)存大小BUCKET_SIZE,得到得到第二個(gè)bucket元素凑耻,imp-sel分別存入p17-p9太示,即向前查找
ldp p17, p9, [x12, #-BUCKET_SIZE]! // {imp, sel} = *--bucket
//--- 跳轉(zhuǎn)至第1步,繼續(xù)對(duì)比 sel 與 cmd
b 1b // loop
3: // wrap: p12 = first bucket, w11 = mask
#if CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_HIGH_16
//--- 人為設(shè)置到最后一個(gè)元素
//--- p11(mask)右移44位 相當(dāng)于mask左移4位香浩,直接定位到buckets的最后一個(gè)元素类缤,緩存查找順序是向前查找
add p12, p12, p11, LSR #(48 - (1+PTRSHIFT))
// p12 = buckets + (mask << 1+PTRSHIFT)
#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_LOW_4
add p12, p12, p11, LSL #(1+PTRSHIFT)
// p12 = buckets + (mask << 1+PTRSHIFT)
#else
#error Unsupported cache mask storage for ARM64.
#endif
/****------------------step6---------------------****/
// Clone scanning loop to miss instead of hang when cache is corrupt.
// The slow path may detect any corruption and halt later.
//--- 再查找一遍緩存()
//--- 拿到x12(即p12)bucket中的 imp-sel 分別存入 p17-p9
ldp p17, p9, [x12] // {imp, sel} = *bucket
//--- 比較 sel 與 p1(傳入的參數(shù)cmd)
1: cmp p9, p1 // if (bucket->sel != _cmd)
//--- 如果不相等,即走到第二步
b.ne 2f // scan more
//--- 如果相等 即命中邻吭,直接返回imp
CacheHit $0 // call or return imp
2: // not hit: p12 = not-hit bucket
//--- 如果一直找不到餐弱,則CheckMiss
CheckMiss $0 // miss if bucket->sel == 0
//--- 判斷p12(下標(biāo)對(duì)應(yīng)的bucket) 是否 等于 p10(buckets數(shù)組第一個(gè)元素)-- 表示前面已經(jīng)沒有了,但是還是沒有找到
cmp p12, p10 // wrap if bucket == buckets
b.eq 3f //如果等于囱晴,跳轉(zhuǎn)至第3步
//--- 從x12(即p12 buckets首地址)- 實(shí)際需要平移的內(nèi)存大小BUCKET_SIZE膏蚓,得到得到第二個(gè)bucket元素,imp-sel分別存入p17-p9畸写,即向前查找
ldp p17, p9, [x12, #-BUCKET_SIZE]! // {imp, sel} = *--bucket
//--- 跳轉(zhuǎn)至第1步驮瞧,繼續(xù)對(duì)比 sel 與 cmd
b 1b // loop
LLookupEnd$1:
LLookupRecover$1:
3: // double wrap
//--- 跳轉(zhuǎn)至JumpMiss 因?yàn)槭莕ormal ,跳轉(zhuǎn)至__objc_msgSend_uncached
JumpMiss $0
.endmacro
//以下是最后跳轉(zhuǎn)的匯編函數(shù)
.macro CacheHit
.if $0 == NORMAL
TailCallCachedImp x17, x12, x1, x16 // authenticate and call imp
.elseif $0 == GETIMP
mov p0, p17
cbz p0, 9f // don't ptrauth a nil imp
AuthAndResignAsIMP x0, x12, x1, x16 // authenticate imp and re-sign as IMP
9: ret // return IMP
.elseif $0 == LOOKUP
// No nil check for ptrauth: the caller would crash anyway when they
// jump to a nil IMP. We don't care if that jump also fails ptrauth.
AuthAndResignAsIMP x17, x12, x1, x16 // authenticate imp and re-sign as IMP
ret // return imp via x17
.else
.abort oops
.endif
.endmacro
.macro CheckMiss
// miss if bucket->sel == 0
.if $0 == GETIMP
//--- 如果為GETIMP 艺糜,則跳轉(zhuǎn)至 LGetImpMiss
cbz p9, LGetImpMiss
.elseif $0 == NORMAL
//--- 如果為NORMAL 剧董,則跳轉(zhuǎn)至 __objc_msgSend_uncached
cbz p9, __objc_msgSend_uncached
.elseif $0 == LOOKUP
//--- 如果為L(zhǎng)OOKUP ,則跳轉(zhuǎn)至 __objc_msgLookup_uncached
cbz p9, __objc_msgLookup_uncached
.else
.abort oops
.endif
.endmacro
.macro JumpMiss
.if $0 == GETIMP
b LGetImpMiss
.elseif $0 == NORMAL
b __objc_msgSend_uncached
.elseif $0 == LOOKUP
b __objc_msgLookup_uncached
.else
.abort oops
.endif
.endmacro
以上內(nèi)容分為以下幾步
- step1:通過
cache
首地址平移16
字節(jié)(因?yàn)樵趏bjc_class中破停,首地址
距離cache
正好16
字節(jié)翅楼,即isa
占8
字節(jié),superClass
占8
字節(jié))真慢,獲取cahce
毅臊,cache中高16位存mask
,低48位存buckets
黑界,ldr p11, [x16, #CACHE]
管嬉,將p11賦值cahce
;- CACHE 這是一個(gè)宏皂林;
step2 :從cache中分別取出buckets和mask,并由mask根據(jù)哈希算法計(jì)算出哈希下標(biāo)
通過
cache
和掩碼(即0x0000ffffffffffff)
的&
運(yùn)算蚯撩,將高16位mask抹零
础倍,得到buckets指針地址,即p10 = buckets
將
cache
右移48
位胎挎,得到mask
沟启,即p11 = mask
將
objc_msgSend
的參數(shù)p1
(即第二個(gè)參數(shù)_cmd)& msak
,通過哈希算法
,得到需要查找存儲(chǔ)sel-imp的bucket下標(biāo)index
犹菇,即p12 = index = _cmd & mask
,為什么通過這種方式呢德迹?因?yàn)樵诖鎯?chǔ)sel-imp
時(shí),也是通過同樣哈希算法計(jì)算哈希下標(biāo)進(jìn)行存儲(chǔ)
揭芍,所以讀取也需要通過同樣的方式讀取胳搞,如下
mask_t m = capacity - 1;
mask_t begin = cache_hash(sel, m);
mask_t i = begin;
static inline mask_t cache_hash(SEL sel, mask_t mask)
{
return (mask_t)(uintptr_t)sel & mask;
}
-
step3 :根據(jù)所得的
哈希下標(biāo)index
和buckets首地址
,取出哈希下標(biāo)對(duì)應(yīng)的bucket
其中
PTRSHIFT
等于3称杨,左移1+3位(即2^4 = 16字節(jié))的目的是計(jì)算出一個(gè)bucket
實(shí)際占用的大小,結(jié)構(gòu)體bucket_t
中sel
占8
字節(jié)肌毅,imp
占8
字節(jié)根據(jù)計(jì)算的哈希下標(biāo)
index 乘以
單個(gè)bucket占用的內(nèi)存大小
,得到buckets
首地址在實(shí)際內(nèi)存
中的偏移量
通過
首地址 + 實(shí)際偏移量
列另,獲取哈希下標(biāo)index對(duì)應(yīng)的bucket
step4 :根據(jù)獲取的
bucket
芽腾,取出其中的sel
存入p17
旦装,即p17 = sel
页衙,取出imp
存入p9
,即p9 = imp
-
step5 :第一次遞歸循環(huán)
比較獲取的
bucket
中sel
與objc_msgSend
的第二個(gè)參數(shù)的_cmd(即p1)
是否相等如果相等阴绢,則執(zhí)行緩存命中
CacheHit
店乐,返回imp
-
如果不相等,有以下兩種情況
如果一直都找不到呻袭,直接跳轉(zhuǎn)至
CheckMiss
眨八,因?yàn)?code>$0是normal
,會(huì)跳轉(zhuǎn)至__objc_msgSend_uncached
左电,進(jìn)入慢速查找流程如果
根據(jù)index獲取的bucket
等于buckets的第一個(gè)元素
廉侧,則人為的將當(dāng)前bucket設(shè)置為buckets的最后一個(gè)元素
(通過buckets首地址+mask右移44位
(等同于左移4位)直接定位到bucker的最后一個(gè)元素
),然后繼續(xù)向前查找篓足,進(jìn)行遞歸循環(huán)
step6 :第二次遞歸循環(huán):重復(fù)step5的操作段誊,與step5中
唯一區(qū)別
是,如果當(dāng)前的bucket等于 buckets的第一個(gè)元素
栈拖,則直接跳轉(zhuǎn)至JumpMiss
连舍,此時(shí)的$0
是normal
,也是直接跳轉(zhuǎn)至__objc_msgSend_uncached
涩哟,即進(jìn)入慢速查找流程
三 索赏、總結(jié)
- objc_msgSend 用匯編寫的原因:
- 1.因?yàn)閏語言中不可能通過寫一個(gè)函數(shù)來保留未知的參數(shù)盼玄,并且跳轉(zhuǎn)到一個(gè)任意的函數(shù)指針。c語言沒有滿足這件事的必要特性潜腻;
- objc_msgSend調(diào)用頻繁埃儿,必須足夠快,在 Objective-C Runtime 中調(diào)用頻率較高的函數(shù)好多都用匯編寫的融涣。
- objc_msgSend流程圖