IOS底層探索Cache_t

Cache_t的數據結構

1.下載objc818可調試源碼c++

2.在main.m文件添加以下代碼:git

#import <Foundation/Foundation.h>
@interface ABPerson : NSObject

@end
@implementation ABPerson

@end int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Class pClass = [ABPerson class];
        NSLog(@"%@",pClass);
    }
    return 0;
}
複製代碼

3.經過lldb調試Cache_t的數據結構github

圖片.png

  • p/x pClass獲取類對象的首地址
  • p/x 0x0000000100008498 + 0x10首地址偏移16個字節拿到cache_t
  • p (cache_t *)0x00000001000084a8打印cache_t地址
  • p *$2查看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
    };
複製代碼

可以發現打印出的結構與objc源碼中cache_t的結構相對應。緩存

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
}
複製代碼

cache_t中的insert方法裏面是將SELIMPbucket_t包裝起來的。markdown

struct bucket_t {
private:
    // IMP-first is better for arm64e ptrauth and no worse for arm64.
    // SEL-first is better for armv7* and i386 and x86_64.
#if __arm64__
    explicit_atomic<uintptr_t> _imp;
    explicit_atomic<SEL> _sel;
#else
    explicit_atomic<SEL> _sel;
    explicit_atomic<uintptr_t> _imp;
#endif
省略部分代碼
}
複製代碼

進一步說明了bucket_t是包裝SELIMP的。數據結構

Cache_t的數據結構圖 Untitled Diagram.pngapp

LLDB驗證方法的存儲

修改main.m文件代碼:less

@interface ABPerson : NSObject
-(void)saySomething;
@end
@implementation ABPerson
-(void)saySomething
{
    NSLog(@"saySomething");
}
@end int main(int argc, const char * argv[]) {
    @autoreleasepool {

        ABPerson *p = [ABPerson alloc];
        Class pClass = [ABPerson class];
        NSLog(@"%@",pClass);
    
    }
    return 0;
}
複製代碼

圖片.png 圖片.png

  • p/x pClass獲取類對象的首地址
  • p/x 0x00000001000084b8 + 0x10首地址偏移16個字節拿到cache_t
  • p (cache_t *)0x00000001000084c8打印cache_t地址
  • p *$2查看cache_t結構
  • p [p saySomething]調用一次方法,讓其有緩存
  • p $3.buckets()[4] 內存平移4個單位獲取值
  • p $4.imp(nil,pClass)打印imp

小規模取樣

1.參照objc_class結構定義ab_objc_classoop

struct objc_class : objc_object {
 省略部分代碼
    // Class ISA;
    Class superclass;
    cache_t cache;             // formerly cache pointer and vtable
    class_data_bits_t bits;    // class_rw_t * plus custom rr/alloc flags
     省略部分代碼
}
複製代碼
struct ab_objc_class {
    Class isa;
    Class superclass;
    struct ab_cache_t cache;            
    struct ab_class_data_bits_t bits;
};
複製代碼

2.參照cache_t定義ab_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
    };
     省略部分代碼
 }
複製代碼
typedef uint32_t mask_t;
struct ab_cache_t {
    struct ab_bucket_t *_bukets; // 8
    mask_t    _maybeMask; // 4
    uint16_t  _flags;  // 2
    uint16_t  _occupied; // 2
};
複製代碼

3.參照class_data_bits_t定義ab_class_data_bits_t

struct class_data_bits_t {
    friend objc_class;

    // Values are the FAST_ flags above.
    uintptr_t bits;
     省略部分代碼
}
複製代碼
struct ab_class_data_bits_t {
    uintptr_t bits;
};
複製代碼

4.參照bucket_t定義ab_bucket_t

struct bucket_t {
private:
    // IMP-first is better for arm64e ptrauth and no worse for arm64.
    // SEL-first is better for armv7* and i386 and x86_64.
#if __arm64__
    explicit_atomic<uintptr_t> _imp;
    explicit_atomic<SEL> _sel;
#else
    explicit_atomic<SEL> _sel;
    explicit_atomic<uintptr_t> _imp;
#endif
省略部分代碼
複製代碼
struct ab_bucket_t {
    SEL _sel;
    IMP _imp;
};
複製代碼

測試類ABPerson的定義

@interface ABPerson : NSObject

- (void)say1;
- (void)say2;
- (void)say3;
- (void)say4;
- (void)say5;
- (void)say6;
- (void)say7;
+ (void)sayHappy;

@end
複製代碼
@implementation ABPerson
- (void)say1{
    NSLog(@"ABPerson say : %s",__func__);
}
- (void)say2{
    NSLog(@"ABPerson say : %s",__func__);
}
- (void)say3{
    NSLog(@"ABPerson say : %s",__func__);
}
- (void)say4{
    NSLog(@"ABPerson say : %s",__func__);
}
- (void)say5{
    NSLog(@"ABPerson say : %s",__func__);
}
- (void)say6{
    NSLog(@"ABPerson say : %s",__func__);
}
- (void)say7{
    NSLog(@"ABPerson say : %s",__func__);
}
+ (void)sayHappy{
    NSLog(@"ABPerson say : %s",__func__);
}
@end
複製代碼

測試即輸出:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        ABPerson *p  = [ABPerson alloc];
        Class pClass = p.class;
        [p say1];
        [p say2];
        [p say3];
        [pClass sayHappy];
        struct ab_objc_class *ab_class = (__bridge struct ab_objc_class *)(pClass);
        NSLog(@"%hu - %u",ab_class->cache._occupied,ab_class->cache._maybeMask);
        
        for (mask_t i = 0; i<ab_class->cache._maybeMask; i++) {
            struct ab_bucket_t bucket = ab_class->cache._bukets[i];
            NSLog(@"%@ - %pf",NSStringFromSelector(bucket._sel),bucket._imp);
        }
        
        NSLog(@"Hello, World!");
    }
    return 0;
}
複製代碼

圖片.png 沒有報錯,而且可以正常輸出(輸出結果後面還會分析到,這裏先略過),說明小規模取樣是可行的。

insert源碼分析

void insert(SEL sel, IMP imp, id receiver);
複製代碼
void cache_t::insert(SEL sel, IMP imp, id receiver) {
   省略部分代碼
    mask_t newOccupied = occupied() + 1; // 1+1
    unsigned oldCapacity = capacity(), capacity = oldCapacity;
    //首次是空的cache
    if (slowpath(isConstantEmptyCache())) {
        // Cache is read-only. Replace it.
        // INIT_CACHE_SIZE_LOG2 = 2,
        //INIT_CACHE_SIZE = (1 << INIT_CACHE_SIZE_LOG2),
        //1左移兩位就是4,INIT_CACHE_SIZE= 4
        if (!capacity) capacity = INIT_CACHE_SIZE;//初始化容量爲4
        reallocate(oldCapacity, capacity, /* freeOld */false);
    }
    //cache_fill_ratio:capacity * 3 / 4; 若是小於等於當前容積的3/4,正常插入數據
    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 {
    //若是大於當前容積的3/4,就進行容積的2倍擴容 :4*2 = 8
        capacity = capacity ? capacity * 2 : INIT_CACHE_SIZE;
        if (capacity > MAX_CACHE_SIZE) {
            capacity = MAX_CACHE_SIZE;
        }
        //分配內存
        reallocate(oldCapacity, capacity, true);
    }

    bucket_t *b = buckets();
    //capacity首次容量爲4
    mask_t m = capacity - 1; // 4-1=3 8-1 = 7
    //經過cache_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.
    do {
         //sel首次加入進來
        if (fastpath(b[i].sel() == 0)) {
            incrementOccupied(); //occupied++;
            //bucket插入sel和imp
            b[i].set<Atomic, Encoded>(b, sel, imp, cls());
            return;
        }
        //已存在sel就跳過
        if (b[i].sel() == sel) {
            // The entry was added to the cache by some other thread
            // before we grabbed the cacheUpdateLock.
            return;
        }
        //cache_next: (i+1) & mask
    } while (fastpath((i = cache_next(i, m)) != begin));

    bad_cache(receiver, (SEL)sel);
#endif // !DEBUG_TASK_THREADS
}

複製代碼
ALWAYS_INLINE void cache_t::reallocate(mask_t oldCapacity, mask_t newCapacity, bool freeOld) {
    bucket_t *oldBuckets = buckets();
    bucket_t *newBuckets = allocateBuckets(newCapacity);

    // Cache's old contents are not propagated. 
    // This is thought to save cache memory at the cost of extra cache fills.
    // fixme re-measure this

    ASSERT(newCapacity > 0);
    ASSERT((uintptr_t)(mask_t)(newCapacity-1) == newCapacity-1);
   //存儲newBuckets指針
    setBucketsAndMask(newBuckets, newCapacity - 1);
    
    if (freeOld) {
    //回收舊內存
        collect_free(oldBuckets, oldCapacity);
    }
}

複製代碼
void cache_t::setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask) {
#ifdef __arm__
    mega_barrier();
    //存儲newBuckets指針
    _bucketsAndMaybeMask.store((uintptr_t)newBuckets, memory_order_relaxed);
    mega_barrier();
    //存儲newMask值
    _maybeMask.store(newMask, memory_order_relaxed);
    _occupied = 0;
#elif __x86_64__ || i386
    // ensure other threads see buckets contents before buckets pointer
    _bucketsAndMaybeMask.store((uintptr_t)newBuckets, memory_order_release);

    // ensure other threads see new buckets before new mask
    _maybeMask.store(newMask, memory_order_release);
    _occupied = 0;
#else
#error Don't know how to do setBucketsAndMask on this architecture. #endif } 複製代碼

小規模取樣中調用了say1say2say3,輸出cache._occupied = 1cache._maybeMask = 7。方法存儲也不是按順序存儲的。 如今去掉一個,只調用say1say2

圖片.png 輸出cache._occupied = 2cache._maybeMask = 3

根據上面分析的原理:

  • 初始化容量爲4mask = capacity - 1因此爲cache._maybeMask = 3,當前調用了2個方法因此cache._occupied = 2
  • 調用3個方法後:newOccupied = occupied() + 1,即newOccupied = 3

newOccupied + CACHE_END_MARKER = 3 + 1 = 4 > capacity * 3 / 4 = 3

擴容爲2倍的capacity,即capacity = 2 * 4 = 8mask = capacity - 1

因此cache._maybeMask = 8 - 1 = 7,又由於分配空間的時候,collect_free回收舊的內存,以前的被清空,只有新的方法say3加進去,因此cache._occupied = 1。由於是哈希表存儲的說以並非從頭開始純粹的。

最後,附上插入分析圖:

a.png

相關文章
相關標籤/搜索