1.objc_class结构体
iOS中类对象和元类对象的本质都是结构体objc_class, objc_class数据结构如下:
上面我们探索了bits,知道对象的实例方法存储类对象的bits中,类方法存储在元类的bits中。这个结构体中还有一个
cache,这个cache的用途是缓存我们调用过的方法。
2.查看源码分析cache
cache是cache_t结构体,机构体中的属性并没有我们需要的,查看它的方法,其中
void insert(SEL sel, IMP imp, id receiver) 方法中有 sel和 imp、receiver,是OC中调用objc_msgSend方法中参数
3. insert方法
进入insert 方法,可以看到此方法中一直在向bucket_t指针中存储sel和imp,进入bucket_t可以看到在从内存中读取SEL和IMP,insert方法的核心代码如下:
void cache_t::insert(SEL sel, IMP imp, id receiver)
{
省略...
bucket_t *b = buckets();
mask_t m = capacity - 1;
mask_t begin = cache_hash(sel, m);
mask_t i = begin;
// Scan for the first unused slot and insert there.
// There is guaranteed to be an empty slot.
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));
bad_cache(receiver, (SEL)sel);
#endif // !DEBUG_TASK_THREADS
}
- 其中
bucket_t *b = buckets();获取目前已经缓存的方法; mask_t m = capacity - 1;是指bucket_t的长度-1; capacity向上查找,找到如下函数:
unsigned cache_t::capacity() const
{
return mask() ? mask()+1 : 0;
}
最终:
mask_t cache_t::mask() const
{
return _maybeMask.load(memory_order_relaxed);
}
其中_maybeMask等于bucket_t的长度
- mask_t begin = cache_hash(sel, m)通过哈希函数获取最大不会超过bucket_t的长度-1,
- do-while 中是为了查找这个方法是否已经缓存,如果没有缓存并且存储在哈希冲突,去解决哈希冲突,然后存储;
4.获取bucket_t中存储的方法
4.1 根据内存平移获取cache_t
4.2 获取bucket_t和bucket_t中的第一个缓存的方法
4.3根据内存平移获取其它缓存的方法
从上面我们只能找到缓存了class方法,但是我们调用了alloc和init方法,在缓存中并没有找到,这是为什么?
5.cache_t的扩容
5.1.cache_t结构体的内存结构
struct cache_t {
private:
explicit_atomic<uintptr_t> _bucketsAndMaybeMask; //占8个字节
union {
struct {
explicit_atomic<mask_t> _maybeMask;//占4个字节
#if __LP64__
uint16_t _flags;//占2个字节
#endif
uint16_t _occupied;//占2个字节
};
explicit_atomic<preopt_cache_t *> _originalPreoptCache; //占8个字节
};
省略....
}
可以看出cache_t占16个字节内存。
5.2 insert扩容核心代码解读
void cache_t::insert(SEL sel, IMP imp, id receiver)
{
省略...
// Use the cache as-is if until we exceed our expected fill ratio.
mask_t newOccupied = occupied() + 1;
unsigned oldCapacity = capacity(), capacity = oldCapacity;
if (slowpath(isConstantEmptyCache())) {
// Cache is read-only. Replace it.
if (!capacity) capacity = INIT_CACHE_SIZE;
reallocate(oldCapacity, capacity, /* freeOld */false);
}
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);
}
省略...
}
5.2.1 mask_t newOccupied = occupied() + 1;解读
- occupied(),
_occupied是cache_t结构体中的成员变量,所以第一次进入时_occupied等于0
mask_t cache_t::occupied() const
{
return _occupied;
}
newOccupied第一次进入等于1,mask_t newOccupied = occupied() + 1;第一次进入是等于1
5.2.2 unsigned oldCapacity = capacity(), capacity = oldCapacity;解读
oldCapacity和capacity第一次进入时候等于0;capacity()方法如下
unsigned cache_t::capacity() const
{
return mask() ? mask()+1 : 0;
}
mask()方法如下,其中_maybeMask是cache_t结构体中的成员变量,第一次进入为0:
mask_t cache_t::mask() const
{
return _maybeMask.load(memory_order_relaxed);
}
5.2.3 if (slowpath(isConstantEmptyCache()))解读
INIT_CACHE_SIZE,在arm64架构下为2,在x86_64架构下为4;- capacity, capacity等于INIT_CACHE_SIZE在第一次进入时候,在arm64架构下开辟一个长度为2的桶子,在x86_64架构下开辟一个长度为4的桶子;当capacity第一进入是0,进入if中,capacity赋值
INIT_CACHE_SIZE
5.2.4 else if (fastpath(newOccupied + CACHE_END_MARKER <= cache_fill_ratio(capacity)))
CACHE_END_MARKER在arm64架构下为0,在x86_64架构下为1- cache_fill_ratio(capacity)) 在arm64架构下为bucket_t长度的7/8,在x86_64架构下为bucket_t长度的3/4
- 在arm64架构下如果缓存的大小+
CACHE_END_MARKER小于等于桶子长度的7/8跳过,在x86_64架构下如果缓存的的大小+CACHE_END_MARKER小于等于桶子长度的3/4跳过;
5.2.5 CACHE_ALLOW_FULL_UTILIZATION
CACHE_ALLOW_FULL_UTILIZATION在arm64架构下为1,在x86_64架构下为0- 在arm64架构下当桶子的长度小于等于8的时候什么也不敢
5.2.6 else扩容
- 当经过前面判断都不够存储,进行两倍扩容,如果容量大于最多容量,则等于最多容量;
总结
- 在arm64架构下,桶子的初始大小为2,当缓存的大小大于7/8的桶子长度时候,进行两倍扩容,当桶子的长度小于等于8的时候,不会扩容,直到存满才进行扩容;
- 在x86_64架构下,桶子的初始大小为4,当缓存的大小等于3/4长度的时候,进行两倍扩容;
- 在扩容的时候会释放原来的桶子,原来缓存的方法将不存在;