cache数据结构
我们先从objc
源码查看一下,由于结构体里面内容太多,看一下简化版
struct cache_t {
private:
explicit_atomic _bucketsAndMaybeMask;
union {
struct {
explicit_atomic _maybeMask;
#if __LP64__
uint16_t _flags;
#endif
uint16_t _occupied;
};
explicit_atomic _originalPreoptCache;
};
struct bucket_t *buckets() const;
void insert(SEL sel, IMP imp, id receiver);
...
}
1.cache_t
里面保存了两个成员,_bucketsAndMaybeMask
和一个联合体
,这两个成员都占有8字节
,所有cache_t
总共16字节,其他都是方法和常量
-
_bucketsAndMaybeMask
,实际上是一块内存地址的首地址,这块内存存储的是方法的sel
和imp
,这个存储的结构式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;
为了节省空间,其中 mask 占用高16
位,buckets占用48
位
-
_occupied
当前缓存占用的数量
void cache_t::insert(SEL sel, IMP imp, id receiver){
do {
if (fastpath(b[i].sel() == 0)) {
incrementOccupied();
b[i].set(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 在整个类中结构
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字节拿到cache_t地址:
尝试获取:
最后通过查找cache_t源码发现有一个buckets的方法:
调用
[p say1]
方法,让后重新打印,发现occpued 和maskmaybe 又远了 1->2
3->7
通过
bucket_t
里面的sel(),获取方法编号imp()
获取方法实现:
cache_t insert 流程分析
我们上面分析了如何获从cache_t
获取方法,但是需要先插入,源码分析
第一步:如果缓存里面为空,先分配4个容量大小的空间,调用reallocate
if (slowpath(isConstantEmptyCache())) {
// Cache is read-only. Replace it.
if (!capacity) capacity = INIT_CACHE_SIZE;
reallocate(oldCapacity, capacity, /* freeOld */false);
}
调用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
,继续往下有一个判断
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, 就会进行扩容,清理掉之前数据,从新分配 reallocate(oldCapacity, capacity, true)
继续下走:
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(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流程图: