上一篇文章我們分析了dyld跟objc的關(guān)聯(lián)中踩萎,已經(jīng)研究到了_dyld_objc_notify_register中會(huì)調(diào)用到map_images哮肚、load_images登夫,并且對(duì)于map_images也做了一些分析。map_images中會(huì)調(diào)用map_images_nolock然后調(diào)用_read_images绽左,_read_images源碼中有這么一段:
// 實(shí)現(xiàn)非懶加載(+load方法及靜態(tài)實(shí)例)
for (EACH_HEADER) {
classref_t const *classlist =
_getObjc2NonlazyClassList(hi, &count);
for (i = 0; i < count; i++) {
Class cls = remapClass(classlist[i]);
if (!cls) continue;
const char *mangledName = cls->mangledName();
const char *clsName = "LGPerson";
if (strcmp(mangledName, clsName)==0) {
printf("%s實(shí)現(xiàn)非懶加載的類悼嫉,對(duì)于load方法和靜態(tài)實(shí)例變量 -%s",__func__,mangledName);
}
addClassTableEntry(cls);
if (cls->isSwiftStable()) {
if (cls->swiftMetadataInitializer()) {
_objc_fatal("Swift class %s with a metadata initializer "
"is not allowed to be non-lazy",
cls->nameForLogging());
}
// fixme also disallow relocatable classes
// We can't disallow all Swift classes because of
// classes like Swift.__EmptyArrayStorage
}
realizeClassWithoutSwift(cls, nil);
}
}
@implementation LGPerson
+ (void)load {
NSLog(@"LGPerson load");
}
@end
在里面我們加了一段打印,判斷如果是我們自己定義的類執(zhí)行到這里就打印拼窥。接下來(lái)我們?cè)贚GPerson中實(shí)現(xiàn)+load方法戏蔑,看看打印:
_read_images實(shí)現(xiàn)非懶加載的類鲁纠,對(duì)于load方法和靜態(tài)實(shí)例變量-LGPerson
2020-10-21 08:32:48.113095+0800 KCObjc[50894:1139354] LGPerson load
接下來(lái)我們把+load方法注釋掉再看看总棵,打印沒(méi)有了,說(shuō)明如果不實(shí)現(xiàn)+load改含,這里面確實(shí)不會(huì)進(jìn)行加載情龄。
這里引出了我們本篇文章分析的一個(gè)話題,懶加載類與非懶加載類捍壤。
懶加載類其實(shí)就是指類的加載在第一次消息發(fā)送之前骤视,但是如果我們?cè)陬愔袑?shí)現(xiàn)了+load方法,那么類的加載就會(huì)提前到pre-main之前,提前加載的類就稱之為非懶加載類。
在上面的源碼中,我們可以找到其中的關(guān)鍵方法realizeClassWithoutSwift辟癌。接下來(lái)我們就繼續(xù)去看看realizeClassWithoutSwift迂曲。
類的加載
realizeClassWithoutSwift我們先看其源碼實(shí)現(xiàn)肉盹,因?yàn)槲覀冞@里主要探究的是加載萨西,其中加載具體做的事情不做過(guò)多說(shuō)明妄壶,把部分源碼進(jìn)行了省略霞扬。
static Class realizeClassWithoutSwift(Class cls, Class previously)
{
runtimeLock.assertLocked();
class_rw_t *rw;
Class supercls;
Class metacls;
if (!cls) return nil;
if (cls->isRealized()) return cls;
ASSERT(cls == remapClass(cls));
// fixme verify class is not in an un-dlopened part of the shared cache?
auto ro = (const class_ro_t *)cls->data();
auto isMeta = ro->flags & RO_META;
if (ro->flags & RO_FUTURE) {
// This was a future class. rw data is already allocated.
rw = cls->data();
ro = cls->data()->ro();
ASSERT(!isMeta);
cls->changeInfo(RW_REALIZED|RW_REALIZING, RW_FUTURE);
} else {
// Normal class. Allocate writeable class data.
rw = objc::zalloc<class_rw_t>();
rw->set_ro(ro);
rw->flags = RW_REALIZED|RW_REALIZING|isMeta;
cls->setData(rw);
}
...
// Realize superclass and metaclass, if they aren't already.
// This needs to be done after RW_REALIZED is set above, for root classes.
// This needs to be done after class index is chosen, for root metaclasses.
// This assumes that none of those classes have Swift contents,
// or that Swift's initializers have already been called.
// fixme that assumption will be wrong if we add support
// for ObjC subclasses of Swift classes.
supercls = realizeClassWithoutSwift(remapClass(cls->superclass), nil);
metacls = realizeClassWithoutSwift(remapClass(cls->ISA()), nil);
...
// Update superclass and metaclass in case of remapping
cls->superclass = supercls;
cls->initClassIsa(metacls);
...
return cls;
}
根據(jù)方法的注釋我們可以解讀出以下信息:
- 對(duì)類cls執(zhí)行首次初始化
- 包括分配讀寫(xiě)數(shù)據(jù)佑笋。
- 不執(zhí)行任何Swift側(cè)初始化翼闹。
- 返回類的實(shí)際類結(jié)構(gòu)。
- 鎖定:runtimeLock必須由調(diào)用者寫(xiě)鎖
對(duì)方法的實(shí)現(xiàn)的一些概念進(jìn)行解讀
ro:干凈內(nèi)存(Clean Memory),存放的是類的原始數(shù)據(jù)
rw:臟內(nèi)存(Dirty Memory) 蒋纬,運(yùn)行時(shí)會(huì)對(duì)類內(nèi)存進(jìn)行動(dòng)態(tài)的修改所以才有rw猎荠,rw最初是從ro中讀取的數(shù)據(jù)。
rwe:新增內(nèi)容颠锉,運(yùn)行時(shí)動(dòng)態(tài)修改類才會(huì)生成rwe法牲,rwe的原始數(shù)據(jù)是從rw中讀取的。
supercls = realizeClassWithoutSwift(remapClass(cls->superclass), nil);沿著繼承鏈遞歸調(diào)用realizeClassWithoutSwift琼掠。
metacls = realizeClassWithoutSwift(remapClass(cls->ISA()), nil);沿著isa走位遞歸調(diào)用realizeClassWithoutSwift。
所以如果一個(gè)類加載了停撞,其繼承鏈上的父類瓷蛙、isa對(duì)應(yīng)的元類等都會(huì)加載。
懶加載類
我們實(shí)現(xiàn)了+load方法類的加載就會(huì)提前戈毒,+load是如何影響類的加載的時(shí)機(jī)的呢艰猬?
load_images源碼中有說(shuō)明在dyld映射的鏡像中處理+load,我們需要去看看load_images中是如何處理+load方法的埋市。
void
load_images(const char *path __unused, const struct mach_header *mh)
{
if (!didInitialAttachCategories && didCallDyldNotifyRegister) {
didInitialAttachCategories = true;
loadAllCategories();
}
// Return without taking locks if there are no +load methods here.
if (!hasLoadMethods((const headerType *)mh)) return;
recursive_mutex_locker_t lock(loadMethodLock);
// Discover load methods
{
mutex_locker_t lock2(runtimeLock);
prepare_load_methods((const headerType *)mh);
}
// Call +load methods (without runtimeLock - re-entrant)
call_load_methods();
}
hasLoadMethods: 如果這里沒(méi)有+load方法冠桃,則返回而不帶鎖。
從對(duì)hasLoadMethods注釋我們知道道宅,load_images是通過(guò)hasLoadMethods方法食听,來(lái)判斷是否有Load方法。
bool hasLoadMethods(const headerType *mhdr)
{
size_t count;
if (_getObjc2NonlazyClassList(mhdr, &count) && count > 0) return true;
if (_getObjc2NonlazyCategoryList(mhdr, &count) && count > 0) return true;
return false;
}
hasLoadMethods中的處理:
- _getObjc2NonlazyClassList:獲取所有類中的Load方法數(shù)量
- _getObjc2NonlazyCategoryList:獲取所有分類中的Load方法數(shù)量
load_images接下來(lái)是調(diào)用了prepare_load_methods來(lái)發(fā)現(xiàn)所有的load方法污茵,并且這里是加了鎖的樱报。
void prepare_load_methods(const headerType *mhdr)
{
size_t count, i;
runtimeLock.assertLocked();
//獲取非懶加載類
classref_t const *classlist =
_getObjc2NonlazyClassList(mhdr, &count);
for (i = 0; i < count; i++) {
//循環(huán)便利加載非懶加載類的load方法到loadable_classes
schedule_class_load(remapClass(classlist[i]));
}
//獲取非懶加載分類列表
category_t * const *categorylist = _getObjc2NonlazyCategoryList(mhdr, &count);
for (i = 0; i < count; i++) {
category_t *cat = categorylist[i];
Class cls = remapClass(cat->cls);
if (!cls) continue; // category for ignored weak-linked class
if (cls->isSwiftStable()) {
_objc_fatal("Swift class extensions and categories on Swift "
"classes are not allowed to have +load methods");
}
//如果類沒(méi)有初始化就去初始化
realizeClassWithoutSwift(cls, nil);
ASSERT(cls->ISA()->isRealized());
// 循環(huán)遍歷去加載非懶加載分類的 load 方法到 loadable_categories
add_category_to_loadable_list(cat);
}
}
prepare_load_methods中分為兩部分:
1.獲取非懶加載類列表,猜測(cè)這里應(yīng)該已經(jīng)加載了對(duì)應(yīng)的類泞当,循環(huán)遍歷加載非懶加載類的load方法到loadable_classes.其中關(guān)鍵的方法schedule_class_load迹蛤、add_class_to_loadable_list。
2.獲取非懶加載分類列表襟士,循環(huán)遍歷去加載非懶加載分類的 load 方法到 loadable_categories盗飒。
其中關(guān)鍵的方法add_category_to_loadable_list。
非懶加載分類遍歷時(shí)陋桂,有一個(gè)處理realizeClassWithoutSwift(cls, nil)逆趣,在遍歷加載非懶加載類的load方法時(shí),會(huì)調(diào)用realizeClassWithoutSwift章喉,如果分類對(duì)應(yīng)的類沒(méi)有記載汗贫,在這里就會(huì)被加載身坐。
懶加載類
對(duì)于懶加載類,是在第一次消息發(fā)送objc_msgSend,調(diào)用到lookUpImpOrForward
IMP lookUpImpOrForward(id inst, SEL sel, Class cls, int behavior)
{
if (slowpath(!cls->isRealized())) {
cls = realizeClassMaybeSwiftAndLeaveLocked(cls, runtimeLock);
// runtimeLock may have been dropped but is now locked again
}
return imp;
}
static Class
realizeClassMaybeSwiftAndLeaveLocked(Class cls, mutex_t& lock)
{
return realizeClassMaybeSwiftMaybeRelock(cls, lock, true);
}
static Class
realizeClassMaybeSwiftMaybeRelock(Class cls, mutex_t& lock, bool leaveLocked)
{
lock.assertLocked();
if (!cls->isSwiftStable_ButAllowLegacyForNow()) {
// Non-Swift class. Realize it now with the lock still held.
// fixme wrong in the future for objc subclasses of swift classes
realizeClassWithoutSwift(cls, nil);
if (!leaveLocked) lock.unlock();
} else {
// Swift class. We need to drop locks and call the Swift
// runtime to initialize it.
lock.unlock();
cls = realizeSwiftClass(cls);
ASSERT(cls->isRealized()); // callback must have provoked realization
if (leaveLocked) lock.lock();
}
return cls;
}
我們可以清晰的看到lookUpImpOrForward中也會(huì)調(diào)用到realizeClassWithoutSwift落包,對(duì)類進(jìn)行加載部蛇。
總結(jié)
懶加載類情況 類加載延遲到第一次消息發(fā)送。
lookUpImOrForward
realizeClassMaybeSwiftMaybeRelock
relizeClassWithoutSwift
methodizeClass
非懶記載類調(diào)用了+load方法咐蝇,類就會(huì)提前加載涯鲁。
getObjc2NonlazyClassList
readClass
realizeClassWithoutSwift
methodizeClass