cache數(shù)據(jù)結(jié)構(gòu)
我們先從objc源碼查看一下,由于結(jié)構(gòu)體里面內(nèi)容太多,看一下簡化版
struct cache_t {
private:
explicit_atomic<uintptr_t> _bucketsAndMaybeMask;
union {
struct {
explicit_atomic<mask_t> _maybeMask;
#if __LP64__
uint16_t _flags;
#endif
uint16_t _occupied;
};
explicit_atomic<preopt_cache_t *> _originalPreoptCache;
};
struct bucket_t *buckets() const;
void insert(SEL sel, IMP imp, id receiver);
...
}
1.cache_t里面保存了兩個成員,_bucketsAndMaybeMask 和一個聯(lián)合體,這兩個成員都占有8字節(jié),所有cache_t總共16字節(jié),其他都是方法和常量
-
_bucketsAndMaybeMask,實際上是一塊內(nèi)存地址的首地址,這塊內(nèi)存存儲的是方法的sel和imp,這個存儲的結(jié)構(gòu)式bucket_t。通過這個屬性的名字其實我們不難看出有兩層意思,一個是bucket,一個是mask(掩碼)
mask:用于在哈希算法或者哈希沖突算法中哈希下標 _maybeMask = capacity -1
void cache_t::setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask)
{
uintptr_t buckets = (uintptr_t)newBuckets;
uintptr_t mask = (uintptr_t)newMask;
ASSERT(buckets <= bucketsMask);
ASSERT(mask <= maxMask);
_bucketsAndMaybeMask.store(((uintptr_t)newMask << maskShift) | (uintptr_t)newBuckets, memory_order_relaxed);
_occupied = 0;
}
static constexpr uintptr_t maskShift = 48;
為了節(jié)省空間,其中 mask 占用高16位,buckets占用48位
-
_occupied當前緩存占用的數(shù)量
void cache_t::insert(SEL sel, IMP imp, id receiver){
do {
if (fastpath(b[i].sel() == 0)) {
incrementOccupied();
b[i].set<Atomic, Encoded>(b, sel, imp, cls());
return;
}
if (b[i].sel() == sel) {
// The entry was added to the cache by some other thread
// before we grabbed the cacheUpdateLock.
return;
}
} while (fastpath((i = cache_next(i, m)) != begin));
}
cache_t 在整個類中結(jié)構(gòu)

圖一.png
lldb驗證
驗證代碼:
@interface QHPerson : NSObject
- (void)say1;
- (void)say2;
+ (void)sayHello;
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
//0x00007ffffffffff8
QHPerson *p = [[QHPerson alloc] init];
NSLog(@"%p",p);
}
return 0;
}
類地址偏移16字節(jié)拿到cache_t地址:

地址偏移.png
嘗試獲?。?br>

嘗試獲取.png
最后通過查找cache_t源碼發(fā)現(xiàn)有一個buckets的方法:

buckets.png
調(diào)用
[p say1]方法,讓后重新打印,發(fā)現(xiàn)occpued 和maskmaybe 又遠了 1->2 3->7
數(shù)量變化.png
通過
bucket_t里面的sel(),獲取方法編號imp()獲取方法實現(xiàn):
截屏2021-06-24 下午4.51.30.png
cache_t insert 流程分析
我們上面分析了如何獲從cache_t獲取方法,但是需要先插入,源碼分析
第一步:如果緩存里面為空,先分配4個容量大小的空間,調(diào)用reallocate
if (slowpath(isConstantEmptyCache())) {
// Cache is read-only. Replace it.
if (!capacity) capacity = INIT_CACHE_SIZE;
reallocate(oldCapacity, capacity, /* freeOld */false);
}
調(diào)用reallocate->setBucketsAndMask
void cache_t::setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask)
{
...
//初始化bucketsAndMaybeMask的值
_bucketsAndMaybeMask.store((uintptr_t)newBuckets, memory_order_relaxed);
_maybeMask.store(newMask, memory_order_relaxed);
_occupied = 0;
}
跳出reallocate,繼續(xù)往下有一個判斷
else if (fastpath(newOccupied + CACHE_END_MARKER <= cache_fill_ratio(capacity))) {
// Cache is less than 3/4 or 7/8 full. Use it as-is.
}
#if CACHE_ALLOW_FULL_UTILIZATION
else if (capacity <= FULL_UTILIZATION_CACHE_SIZE && newOccupied + CACHE_END_MARKER <= capacity) {
// Allow 100% cache utilization for small buckets. Use it as-is.
}
#endif
else {
capacity = capacity ? capacity * 2 : INIT_CACHE_SIZE;
if (capacity > MAX_CACHE_SIZE) {
capacity = MAX_CACHE_SIZE;
}
reallocate(oldCapacity, capacity, true);
}
大致意思就是Occupied >0.75*capacity, 就會進行擴容,清理掉之前數(shù)據(jù),從新分配 reallocate(oldCapacity, capacity, true)
繼續(xù)下走:
bucket_t *b = buckets();
mask_t m = capacity - 1;
mask_t begin = cache_hash(sel, m);
mask_t i = begin;
do {
if (fastpath(b[i].sel() == 0)) {
incrementOccupied();
b[i].set<Atomic, Encoded>(b, sel, imp, cls());
return;
}
if (b[i].sel() == sel) {
// The entry was added to the cache by some other thread
// before we grabbed the cacheUpdateLock.
return;
}
} while (fastpath((i = cache_next(i, m)) != begin));
先通過hash算法,得到一個索引。然后通過這個索引找方法,如果不存在就保存,并且occupied+1,如果有hash沖突,重新計算。直到成功。
下面附上一張cache流程圖:

cache_流程圖.png