美文网首页
objc_class 中的cache

objc_class 中的cache

作者: Kates | 来源:发表于2021-06-28 13:26 被阅读0次

    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.

    相关文章

      网友评论

          本文标题:objc_class 中的cache

          本文链接:https://www.haomeiwen.com/subject/xyhoyltx.html