-
cache_t
源碼結(jié)構(gòu)
精簡后的cache_t
源碼如下:
struct cache_t {
#if CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_OUTLINED
// MAC
struct bucket_t * _buckets;
uint32_t _mask;
#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_HIGH_16
//真機(jī)
uintptr_t _maskAndBuckets;
mask_t _mask_unused;
static constexpr uintptr_t maskShift = 48;
static constexpr uintptr_t maskZeroBits = 4;
static constexpr uintptr_t maxMask = ((uintptr_t)1 << (64 - maskShift)) - 1;
static constexpr uintptr_t bucketsMask = ((uintptr_t)1 << (maskShift - maskZeroBits)) - 1;
#else
#error Unknown cache mask storage type.
#endif
uint16_t _flags;
uint16_t _occupied;//占用的個(gè)數(shù)
public:
static bucket_t *emptyBuckets();
struct bucket_t *buckets();
uint32_t mask();
uint32_t occupied();
void incrementOccupied();
void setBucketsAndMask(struct bucket_t *newBuckets, uint32_t newMask);
void initializeToEmpty();
unsigned capacity();
bool isConstantEmptyCache();
bool canBeFreed();
};
void cache_t::incrementOccupied()
{
_occupied++;
}
可以看到有個(gè)重要的函數(shù)void incrementOccupied();
,字面意思增加占用的個(gè)數(shù),該函數(shù)只對內(nèi)部成員變量加1操作哎壳。在源碼中全局搜索調(diào)用,即可看到在cache_t::insert
函數(shù)中調(diào)用蔬充。
ALWAYS_INLINE
void cache_t::insert(Class cls, SEL sel, IMP imp, id receiver)
{
// Use the cache as-is if it is less than 3/4 full
//occupied() 獲取_occupied變量
mask_t newOccupied = occupied() + 1;//因?yàn)橐略鲆粋€(gè)緩存羽莺,所以+1
unsigned oldCapacity = capacity(), capacity = oldCapacity;
if (slowpath(isConstantEmptyCache())) {
// Cache is read-only. Replace it.
//如果之前沒有緩存,開辟大小為4的內(nèi)存
if (!capacity) capacity = INIT_CACHE_SIZE;
reallocate(oldCapacity, capacity, /* freeOld */false);
}
else if (fastpath(newOccupied + CACHE_END_MARKER <= capacity / 4 * 3)) {
// Cache is less than 3/4 full. Use it as-is.
//如果新的占用個(gè)數(shù)加1 小于等于 capacity的四分之三 則繼續(xù)
}
else {
capacity = capacity ? capacity * 2 : INIT_CACHE_SIZE;
//否則進(jìn)行擴(kuò)容到 capacity的二倍
if (capacity > MAX_CACHE_SIZE) {
capacity = MAX_CACHE_SIZE;
}
reallocate(oldCapacity, capacity, true);
}
bucket_t *b = buckets();
mask_t m = capacity - 1;
mask_t begin = cache_hash(sel, m);//通過hash計(jì)算出要存放的index
mask_t i = begin;
// Scan for the first unused slot and insert there.
// There is guaranteed to be an empty slot because the
// minimum size is 4 and we resized at 3/4 full.
//確保計(jì)算出存放的index沒有被占用猬错,否則繼續(xù)計(jì)算index
do {
if (fastpath(b[i].sel() == 0)) {
incrementOccupied();
b[i].set<Atomic, Encoded>(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::bad_cache(receiver, (SEL)sel, cls);
}
2. 模仿cache_t結(jié)構(gòu)窗看,進(jìn)行探索
typedef uint32_t mask_t; // x86_64 & arm64 asm are less efficient with 16-bits
struct bucket_t {
SEL _sel;
IMP _imp;
};
struct ls_cache_t {
struct bucket_t * _buckets;
mask_t _mask;
uint16_t _flags;
uint16_t _occupied;
};
//不影響cache_t獲取,所以寫個(gè)空結(jié)構(gòu)體類型
struct ls_class_data_bits_t {
};
struct ls_objc_class {
Class ISA;
Class superclass;
struct ls_cache_t cache; // formerly cache pointer and vtable
struct ls_class_data_bits_t bits; // class_rw_t * plus custom rr/alloc flags
};
void printCachet(Class pClass) {
struct ls_objc_class *ls_class = (__bridge struct ls_objc_class *)(pClass);
uint16_t occupied = ls_class->cache._occupied;
mask_t mask = ls_class->cache._mask;
printf("cache_t._occupied = %d cache_t._mask = %d \n", occupied, mask);
for (int i = 0; i < mask; i++) {
struct bucket_t bucket = ls_class->cache._buckets[i];
printf("sel=%s imp=%p\n", [NSStringFromSelector(bucket._sel) UTF8String], bucket._imp);
}
printf("----------------------------------\n");
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
LSPerson *person = [LSPerson alloc];
Class pClass = [LSPerson class];
printCachet(pClass);
[person eat];
[person run];
printCachet(pClass);
[person sleep];
[person smile];
printCachet(pClass);
}
return 0;
}
//output
//不調(diào)用方法時(shí)倦炒,_occupied = 0显沈, _mask = 0
cache_t._occupied = 0 cache_t._mask = 0
----------------------------------
2020-09-17 17:43:11.080167+0800 Cache_t_Analyze[64281:11027140] -[LSPerson eat]
2020-09-17 17:43:11.080673+0800 Cache_t_Analyze[64281:11027140] -[LSPerson run]
//調(diào)用2個(gè)方法后 _occupied = 2,_mask = 4 - 1
cache_t._occupied = 2 cache_t._mask = 3
sel=run imp=0x2c90
sel=(null) imp=0x0
sel=eat imp=0x2ce0
----------------------------------
2020-09-17 17:43:11.080923+0800 Cache_t_Analyze[64281:11027140] -[LSPerson sleep]
2020-09-17 17:43:11.080973+0800 Cache_t_Analyze[64281:11027140] -[LSPerson smile]
//調(diào)用4個(gè)方法逢唤,由于在調(diào)用第三個(gè)方法時(shí)觸發(fā)了擴(kuò)容拉讯,清空之前的緩存,所以只緩存了調(diào)用的第三個(gè)和第四個(gè)方法鳖藕,但容量擴(kuò)容到了8
//所以 _occupied = 2 _mask = 8 - 1
cache_t._occupied = 2 cache_t._mask = 7
sel=smile imp=0x2c70
sel=(null) imp=0x0
sel=(null) imp=0x0
sel=(null) imp=0x0
sel=(null) imp=0x0
sel=(null) imp=0x0
sel=sleep imp=0x2c40
----------------------------------
Program ended with exit code: 0