Cache_t 原理分析

195 阅读4分钟

1. cache_t内存结构

首先查看源码结构

struct cache_t {
#if 1 // Mac 
    struct bucket_t * _buckets;
    mask_t _mask;
#elif 1 // 真机
    uintptr_t _maskAndBuckets;
    mask_t _mask_unused;
    
    // How much the mask is shifted by.
    static constexpr uintptr_t maskShift = 48;
    
    // Additional bits after the mask which must be zero. msgSend
    // takes advantage of these additional bits to construct the value
    // `mask << 4` from `_maskAndBuckets` in a single instruction.
    static constexpr uintptr_t maskZeroBits = 4;
    
    // The largest mask value we can store.
    static constexpr uintptr_t maxMask = ((uintptr_t)1 << (64 - maskShift)) - 1;
    
    // The mask applied to `_maskAndBuckets` to retrieve the buckets pointer.
    static constexpr uintptr_t bucketsMask = ((uintptr_t)1 << (maskShift - maskZeroBits)) - 1;
#endif
    uint16_t _flags; // 标志位
    uint16_t _occupied; // 被占用的

public:
    static bucket_t *emptyBuckets();
    struct bucket_t *buckets();
    mask_t mask();
    mask_t occupied();
    void incrementOccupied();
    void setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask);
    void initializeToEmpty();
    unsigned capacity();
    bool isConstantEmptyCache();
    bool canBeFreed();
    void reallocate(mask_t oldCapacity, mask_t newCapacity, bool freeOld);
    void insert(Class cls, SEL sel, IMP imp, id receiver);
};

2. cache_t 插入一条数据, 每当对象调用一个方法时,如果在cache里面没有找到,就会insert 一条缓存

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
    mask_t newOccupied = occupied() + 1;
    unsigned oldCapacity = capacity(), capacity = oldCapacity;
    // 1. 如果Cache 是空的话,会初始化一个 4 个字节的空间
    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 <= capacity / 4 * 3)) {
        // Cache is less than 3/4 full. Use it as-is.
        // 2. newOccupied + CACHE_END_MARKER <= capacity / 4 * 3 ,直接插入
    }
    else {
        // 3. 否则会扩容
        capacity = capacity ? capacity * 2 : INIT_CACHE_SIZE;
        if (capacity > MAX_CACHE_SIZE) {
            capacity = MAX_CACHE_SIZE;
        }
        reallocate(oldCapacity, capacity, true);
    }
    // 4. 初始化一个指针数组
    bucket_t *b = buckets();
    // 5. 设置掩码为 capacity - 1
    mask_t m = capacity - 1;
    // 6. 根据sel 计算 hash 值
    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 because the
    // minimum size is 4 and we resized at 3/4 full.
    do {
        // 7. 当前 插槽 取到 的sel 地址为0, 那么插入新的值
        if (fastpath(b[i].sel() == 0)) {
            // 8. 增加占用字段并且插入
            incrementOccupied();
            b[i].set<Atomic, Encoded>(sel, imp, cls);
            return;
        }
        // 8. 多线程做的判断
        if (b[i].sel() == sel) {
            // The entry was added to the cache by some other thread
            // before we grabbed the cacheUpdateLock.
            return;
        }
        // 9. 如果当前位置已有值,那么就找下一个位置
    } while (fastpath((i = cache_next(i, m)) != begin));
}

// 10. hash 算法, 保证不会越界
static inline mask_t cache_hash(SEL sel, mask_t mask) 
{
    return (mask_t)(uintptr_t)sel & mask;
}

3. 在objc Demo 中调试 Cache

@interface Person : NSObject
@property (nonatomic, copy) NSString *nickName;
@property (nonatomic, strong) NSString *name;

- (void)test1;
- (void)test2;
- (void)test3;
- (void)test4;
- (void)test5;
- (void)test6;
- (void)test7;
- (void)test8;
- (void)test9;
- (void)eat;
@end
@implementation Person
- (void)test1{NSLog(@"%s",__func__);}
- (void)test2{NSLog(@"%s",__func__);}
- (void)test3{NSLog(@"%s",__func__);}
- (void)test4{NSLog(@"%s",__func__);}
- (void)test5{NSLog(@"%s",__func__);}
- (void)test6{NSLog(@"%s",__func__);}
- (void)test7{NSLog(@"%s",__func__);}
- (void)test8{NSLog(@"%s",__func__);}
- (void)test9{NSLog(@"%s",__func__);}
- (void)eat {NSLog(@"Person _cmd-> %p SEL-> %p", _cmd, @selector(eat));}
@end
Person *objc2 = [Person alloc];
[objc2 test1];
[objc2 test2];
// 1. 获取当前类对象
(lldb) p/x Person.class
(Class) $0 = 0x00000001000033b0 Person
// 2. 获取当前类对象 的 cache, 当前类对象 + 16 (ISA + SuperClass)
(lldb) p (cache_t *)0x00000001000033c0
(cache_t *) $1 = 0x00000001000033c0
// 3. 打印当前cache, 发现 _occupied = 0,_mask = 0;
// 说明当前的Cache是空的
(lldb) p *$1
(cache_t) $2 = {
  _buckets = {
    std::__1::atomic<bucket_t *> = 0x000000010032f410 {
      _sel = {
        std::__1::atomic<objc_selector *> = (null)
      }
      _imp = {
        std::__1::atomic<unsigned long> = 0
      }
    }
  }
  _mask = {
    std::__1::atomic<unsigned int> = 0
  }
  _flags = 32804
  _occupied = 0
}
// 4. 调用 test1 方法, 再打印Cache,发现 _occupied = 1,_mask = 3;_buckets 的首地址却是null,说明cache 没有插入在第一位置上,说明底层用的不是数组
2020-09-17 11:42:34.453871+0800 KCObjc[29193:2560315] -[Person test1]
(lldb) p *$1
(cache_t) $3 = {
  _buckets = {
    std::__1::atomic<bucket_t *> = 0x0000000100742520 {
      _sel = {
        std::__1::atomic<objc_selector *> = (null)
      }
      _imp = {
        std::__1::atomic<unsigned long> = 0
      }
    }
  }
  _mask = {
    std::__1::atomic<unsigned int> = 3
  }
  _flags = 32804
  _occupied = 1
}
// 4.1 打印 buckets 数组
(lldb) p $1->buckets()
(bucket_t *) $4 = 0x0000000100742520
// 4.1 打印 _mask 数组
(lldb) p $1->_mask
(explicit_atomic<unsigned int>) $5 = {
  std::__1::atomic<unsigned int> = 3
}
// 5.1 打印 buckets 数组第一个元素,发现是null
(lldb) p $4[0]
(bucket_t) $6 = {
  _sel = {
    std::__1::atomic<objc_selector *> = (null)
  }
  _imp = {
    std::__1::atomic<unsigned long> = 0
  }
}
// 5.2 打印 buckets 数组第二个元素,发现是null
(lldb) p $4[1]
(bucket_t) $7 = {
  _sel = {
    std::__1::atomic<objc_selector *> = (null)
  }
  _imp = {
    std::__1::atomic<unsigned long> = 0
  }
}
// 5.3 打印 buckets 数组第三个元素,发现不是null
(lldb) p $4[2]
(bucket_t) $8 = {
  _sel = {
    std::__1::atomic<objc_selector *> = ""
  }
  _imp = {
    std::__1::atomic<unsigned long> = 10416
  }
}
// 5.4 打印 bucket_t 的 SEL
(lldb) p $4[2].sel()
(SEL) $9 = "test1"
// 5.5 打印 bucket_t 的 IMP
(lldb) p $4[2].imp(Person.class)
(IMP) $10 = 0x0000000100001b00 (Objc`-[Person test1])

4. 在普通的Mac工程里面Debug 方法Cache

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

typedef uint32_t mask_t;

struct g_bucket_t {
    // 注意顺序,x86_64 和 arm64 的架构正好相反
    SEL _sel;
    IMP _imp;
};

struct g_cache_t {
    struct g_bucket_t * _buckets;
    mask_t _mask;
    uint16_t _flags;
    uint16_t _occupied;
};

struct g_class_data_bits_t {
    uintptr_t bits;
};

struct g_objc_class {
    Class ISA;
    Class superclass;
    struct g_cache_t cache;             // formerly cache pointer and vtable
    struct g_class_data_bits_t bits;    // class_rw_t * plus custom rr/alloc flags
};

void debugCache(struct g_objc_class *cls) {
    NSLog(@"-----------------------------------------");
    NSLog(@"_occupied->%hu _mask->%u",cls->cache._occupied, cls->cache._mask);
    for (int i= 0; i<cls->cache._mask; i++) {
        struct g_bucket_t bucket = cls->cache._buckets[i];
        NSLog(@"sel->%@ imp: %p", NSStringFromSelector(bucket._sel),bucket._imp);
    }
}
int main(int argc, char * argv[]) {
    
    struct g_objc_class *cls = (__bridge struct g_objc_class *)([Person class]);
    
    Person *objc2 = [Person alloc];
    debugCache(cls);
    [objc2 test1];
    debugCache(cls);
    [objc2 test2];
    [objc2 test3];
    [objc2 test4];
    [objc2 test5];
    [objc2 test6];
    [objc2 test7];
    [objc2 test8];
    [objc2 test9];
    [objc2 test1];
    [objc2 test1];
    debugCache(cls);
    
    return 0;
}

output:

 -----------------------------------------
 // 没有调用其任何方法,所有 _occupied 为0
 _occupied->0 _mask->0
 -[Person test1]
 -----------------------------------------
 // 调用了 test1 ,所有 _occupied 为1,_mask = 4 - 1
 _occupied->1 _mask->3
 sel->(null) imp: 0x0
 sel->(null) imp: 0x0
 sel->test1 imp: 0x2b90
 -[Person test2]
 -[Person test3]
 -[Person test4]
 -[Person test5]
 -[Person test6]
 -[Person test7]
 -[Person test8]
 -[Person test9]
 -[Person test1]
 -[Person test1]
 -----------------------------------------
 // 调用了很多方法 ,所有 _occupied 为3,_mask = 16 - 1, 
 // 当每次缓存不够用的话,都会重新扩容,删掉旧的缓存
 _occupied->3 _mask->15
 sel->(null) imp: 0x0
 sel->(null) imp: 0x0
 sel->(null) imp: 0x0
 sel->(null) imp: 0x0
 sel->test8 imp: 0x2960
 sel->(null) imp: 0x0
 sel->(null) imp: 0x0
 sel->(null) imp: 0x0
 sel->(null) imp: 0x0
 sel->(null) imp: 0x0
 sel->test9 imp: 0x2910
 sel->test1 imp: 0x2b90
 sel->(null) imp: 0x0
 sel->(null) imp: 0x0
 sel->(null) imp: 0x0

总结 cache_t 使用了 hash表存储缓存,每当对象调用一个方法时,如果在cache里面没有找到,就会insert 一条缓存。