objc_class 中的cache

cache的获取

struct objc_class : objc_object {
    Class superclass;
    cache_t cache;             // formerly cache pointer and vtable
    class_data_bits_t bits;    // class_rw_t * plus custom rr/alloc flags
}

通过名字我们猜测cache应该是缓存,但是到底是缓存了什么呢?这个就需要探索了
首先获取cache,通过之前的篇章我们知道,要获取cache,需要通过首地址编译16字节得到。

LGPerson *p  = [LGPerson alloc];
Class pClass = [LGPerson class]; //cache_t
通过lldb调试
(lldb) p/x pClass
(Class) $0 = 0x0000000100008608 LGPerson
(lldb) p (cache_t *)0x0000000100008618
(cache_t *) $1 = 0x0000000100008618
(lldb) p *$1
(cache_t) $2 = {
  _bucketsAndMaybeMask = {
    std::__1::atomic<unsigned long> = {
      Value = 4298523568
    }
  }
   = {
     = {
      _maybeMask = {
        std::__1::atomic<unsigned int> = {
          Value = 0
        }
      }
      _flags = 32808
      _occupied = 0
    }
    _originalPreoptCache = {
      std::__1::atomic<preopt_cache_t *> = {
        Value = 0x0000802800000000
      }
    }
  }
}

我们在来看看cache_t
struct cache_t {
    explicit_atomic<uintptr_t> _bucketsAndMaybeMask; // 8
    union {
        struct {
            explicit_atomic<mask_t>    _maybeMask; // 4
#if __LP64__   //__LP64__ Linux/Unix/MacOS 地址长度64位 
            uint16_t                   _flags;  // 2
#endif
            uint16_t                   _occupied; // 2
        };
        explicit_atomic<preopt_cache_t *> _originalPreoptCache; // 8
    };
}       

从打印结果来看,cache里有个_bucketsAndMaybeMask,_flags,_occupied,_originalPreoptCache,但是我们都不知道这些干嘛用的,我们到cache结构里面看看有没有什么方法可以输出查看。
void insert(SEL sel, IMP imp, id receiver); 里面有个insert方法,而且参数是sel和imp,似乎就是换成sel和imp。

void cache_t::insert(SEL sel, IMP imp, id receiver)
{
    runtimeLock.assertLocked();

    // Never cache before +initialize is done
    if (slowpath(!cls()->isInitialized())) {
        return;
    }

    if (isConstantOptimizedCache()) {
        _objc_fatal("cache_t::insert() called with a preoptimized cache for %s",
                    cls()->nameForLogging());
    }

#if DEBUG_TASK_THREADS
    return _collecting_in_critical();
#else
#if CONFIG_USE_CACHE_LOCK
    mutex_locker_t lock(cacheUpdateLock);
#endif

    ASSERT(sel != 0 && cls()->isInitialized());

    // Use the cache as-is if until we exceed our expected fill ratio.
    mask_t newOccupied = occupied() + 1; // 1+1
    unsigned oldCapacity = capacity(), capacity = oldCapacity;
    if (slowpath(isConstantEmptyCache())) {
        // Cache is read-only. Replace it.
        if (!capacity) capacity = INIT_CACHE_SIZE;//4
        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 {// 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();
    mask_t m = capacity - 1; // 4-1=3
    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
}

通过insert源码我们发现,insert是将sel和imp放到了buckets里面
接下来我们看看buckets源码和bucket_t 定义

struct bucket_t *cache_t::buckets() const
{
    uintptr_t addr = _bucketsAndMaybeMask.load(memory_order_relaxed);
    return (bucket_t *)(addr & bucketsMask);
}

struct bucket_t {
#if __arm64__
    explicit_atomic<uintptr_t> _imp;
    explicit_atomic<SEL> _sel;
#else
    explicit_atomic<SEL> _sel;
    explicit_atomic<uintptr_t> _imp;
#endif
}

似乎被我们发现了_bucketsAndMaybeMask,原来sel和imp缓存在这里

继续lldb调试
(lldb) p $2._bucketsAndMaybeMask
(explicit_atomic<unsigned long>) $3 = {
  std::__1::atomic<unsigned long> = {
    Value = 4298523568
  }
}
(lldb) p $2.buckets()
(bucket_t *) $4 = 0x00000001003643b0
(lldb) p/x 4298523568
(long) $5 = 0x00000001003643b0

到这里我们就知道了,cache缓存的是sel和imp

cache 缓存方法

继续lldb调试

(lldb) p $2.buckets()
(bucket_t *) $4 = 0x00000001003643b0
这边获取到了bucket_t 指针,查询bucket结构体里发现有sel()方法
(lldb) p *$4
(bucket_t) $13 = {
  _sel = {
    std::__1::atomic<objc_selector *> = (null) {
      Value = nil
    }
  }
  _imp = {
    std::__1::atomic<unsigned long> = {
      Value = 0
    }
  }
}
(lldb) p $4[1]
(bucket_t) $15 = {
  _sel = {
    std::__1::atomic<objc_selector *> = (null) {
      Value = nil
    }
  }
  _imp = {
    std::__1::atomic<unsigned long> = {
      Value = 0
    }
  }
}
(lldb) p $4[2]
(bucket_t) $16 = {
  _sel = {
    std::__1::atomic<objc_selector *> = (null) {
      Value = nil
    }
  }
  _imp = {
    std::__1::atomic<unsigned long> = {
      Value = 0
    }
  }
}
看起来这个数组里面并没有存放数据,想来也是我们并没有调用什么实例方法
(lldb) p [p saySomething]
2021-06-25 15:26:10.825784+0800 KCObjcBuild[14332:1681446] -[LGPerson saySomething]
(lldb) p $2.buckets()
(bucket_t *) $19 = 0x0000000100629430
(lldb) p *$19
(bucket_t) $20 = {
  _sel = {
    std::__1::atomic<objc_selector *> = (null) {
      Value = nil
    }
  }
  _imp = {
    std::__1::atomic<unsigned long> = {
      Value = 0
    }
  }
}
(lldb) p $19[1]
(bucket_t) $21 = {
  _sel = {
    std::__1::atomic<objc_selector *> = (null) {
      Value = nil
    }
  }
  _imp = {
    std::__1::atomic<unsigned long> = {
      Value = 0
    }
  }
}
(lldb) p $19[3]
(bucket_t) $22 = {
  _sel = {
    std::__1::atomic<objc_selector *> = (null) {
      Value = nil
    }
  }
  _imp = {
    std::__1::atomic<unsigned long> = {
      Value = 0
    }
  }
}
(lldb) p $19[4]
(bucket_t) $23 = {
  _sel = {
    std::__1::atomic<objc_selector *> = "" {
      Value = ""
    }
  }
  _imp = {
    std::__1::atomic<unsigned long> = {
      Value = 48312
    }
  }
}
直到第四个才有数值
(lldb) p $19[4].sel()
(SEL) $24 = "saySomething"
我们调用的saySomething找到了,果然这边缓存的就是我们调用过的函数,但是为什么缓存在4的位置呢?
接下来我们又要去研究insert函数了

cache如何缓存方法

  1. 第一次进来通过realloccate开辟空间,默认开辟4个
    capacity = INIT_CACHE_SIZE(INIT_CACHE_SIZE = (1 << INIT_CACHE_SIZE_LOG2), INIT_CACHE_SIZE_LOG2 = 2)
    reallocate(oldCapacity, capacity, /* freeOld */false);
    \color{#f00}{注意:} 这边虽然开辟了capacity个空间,但是实际存放数据为capacity-1,因为最后一个默认为1
bucket_t *cache_t::allocateBuckets(mask_t newCapacity)
{
    // Allocate one extra bucket to mark the end of the list.
    // This can't overflow mask_t because newCapacity is a power of 2.
    bucket_t *newBuckets = (bucket_t *)calloc(bytesForCapacity(newCapacity), 1);
    bucket_t *end = endMarker(newBuckets, newCapacity); // 获取最后一个位置
#if __arm__
    // End marker's sel is 1 and imp points BEFORE the first bucket.
    // This saves an instruction in objc_msgSend.
    end->set<NotAtomic, Raw>(newBuckets, (SEL)(uintptr_t)1, (IMP)(newBuckets - 1), nil);
#else
    // End marker's sel is 1 and imp points to the first bucket.
    end->set<NotAtomic, Raw>(newBuckets, (SEL)(uintptr_t)1, (IMP)newBuckets, nil); // 在最后位置插入1
#endif
    if (PrintCaches) recordNewCache(newCapacity);
    return newBuckets;
}
  1. 下次进来查看是否小于3/4 小于则不需要扩容直接添加
  2. 大于3/4 需要扩容,扩容规则capacity = capacity ? capacity * 2 : INIT_CACHE_SIZE;(2倍扩容)
  3. 扩容后并不会将旧的数据移到新容器里,只存放新数据(苹果的原则,新数据更有存储价值)
  4. 存放位置的设计
bucket_t *b = buckets();
    mask_t m = capacity - 1; // 4-1=3
    mask_t begin = cache_hash(sel, m); 通过哈希获取到begin位置
    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)) { 如果I位置为空,直接存放sel
            incrementOccupied();
            b[i].set<Atomic, Encoded>(b, sel, imp, cls());
            return;
        }
        if (b[i].sel() == sel) { // 如果i的位置已经存放过当前的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)); 如果i已经存放过了,哈希碰撞,通过cache_next获取下一个位置

#if CACHE_END_MARKER
static inline mask_t cache_next(mask_t i, mask_t mask) {
    return (i+1) & mask;
}
#elif __arm64__
static inline mask_t cache_next(mask_t i, mask_t mask) {
    return i ? i-1 : mask;  
}
注意:arm64是往前查找下一个位置
#else

不通过源码打印cache缓存

typedef uint32_t mask_t;
typedef unsigned long           uintptr_t;

struct hf_bucket_t {
    SEL _sel;
    IMP _imp;
};

struct hf_objc_object {
    Class _Nonnull isa;
};

struct hf_cache_t {
//    uintptr_t _bucketsAndMaybeMask; // 8
    struct hf_bucket_t *buckets;
    mask_t    _maybeMask;
    uint16_t  _flags;
    uint16_t  _occupied;
};

struct hf_class_data_bits_t {
    // Values are the FAST_ flags above.
    uintptr_t bits;
};

struct hf_objc_class : hf_objc_object {
    // Class ISA;
    Class superclass;
    struct hf_cache_t cache;             // formerly cache pointer and vtable
    struct hf_class_data_bits_t bits;
};

int main(int argc, char * argv[]) {
    HFObject *p = [HFObject alloc];
    [p say1];
    [p say2];
    [p say3];
    [p say4];
    [p say5];
    [p say6];
    Class pClass = [HFObject class];
    hf_objc_class *hfClass = (__bridge hf_objc_class *)pClass;
    NSLog(@"cache:%p", hfClass->cache);
    NSLog(@"%p---%d---%d", hfClass->cache.buckets, hfClass->cache._maybeMask, hfClass->cache._occupied);
    for (int i=0; i<hfClass->cache._maybeMask; i++) {
        hf_bucket_t bucket = hfClass->cache.buckets[i];
        NSLog(@"%@----%p", NSStringFromSelector(bucket._sel), bucket._imp);
    }
    return 0;
    
}

输出结果
2021-06-28 13:17:36.115573+0800 cache_tDemo[20872:2889296] -[HFObject say1]
2021-06-28 13:17:36.116421+0800 cache_tDemo[20872:2889296] -[HFObject say2]
2021-06-28 13:17:36.116530+0800 cache_tDemo[20872:2889296] -[HFObject say3]
2021-06-28 13:17:36.116637+0800 cache_tDemo[20872:2889296] -[HFObject say4]
2021-06-28 13:17:36.116735+0800 cache_tDemo[20872:2889296] -[HFObject say5]
2021-06-28 13:17:36.116851+0800 cache_tDemo[20872:2889296] -[HFObject say6]
2021-06-28 13:17:36.116956+0800 cache_tDemo[20872:2889296] cache:0x6000012b0280
2021-06-28 13:17:36.117100+0800 cache_tDemo[20872:2889296] 0x6000012b0280---7---4
2021-06-28 13:17:36.117459+0800 cache_tDemo[20872:2889296] (null)----0x0
2021-06-28 13:17:36.117863+0800 cache_tDemo[20872:2889296] say4----0x5598
2021-06-28 13:17:36.118024+0800 cache_tDemo[20872:2889296] (null)----0x0
2021-06-28 13:17:36.118349+0800 cache_tDemo[20872:2889296] say6----0x5478
2021-06-28 13:17:36.118618+0800 cache_tDemo[20872:2889296] say3----0x55e8
2021-06-28 13:17:36.118914+0800 cache_tDemo[20872:2889296] (null)----0x0
2021-06-28 13:17:36.119255+0800 cache_tDemo[20872:2889296] say5----0x5448

通过打印cache结果,可以看出,存储并不是数组存储而是通过哈希存储。在say3的时候进行了扩容,那是因为,say1,say2在加上末尾的1刚好是3/4,在say3的时候超过了即进行了扩容所以没有打印say1和say2.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,921评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,635评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,393评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,836评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,833评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,685评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,043评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,694评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,671评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,670评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,779评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,424评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,027评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,984评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,214评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,108评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,517评论 2 343

推荐阅读更多精彩内容