In-depth analysis of the underlying principle of the caching method cache_t in iOS

1. The principle of cache_t

  • There is a method cache cache_t in Class, which uses a hash table to cache the called methods, which can improve the speed of accessing methods.
	struct cache_t {
    
    
	    struct bucket_t *_buckets;// 数组
	    mask_t _mask;//
	    mask_t _occupied;
	
	public:
	    struct bucket_t *buckets();
	    mask_t mask();
	    mask_t occupied();
	    void incrementOccupied();
	    void setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask);
	    void initializeToEmpty();
	
	    mask_t capacity();
	    bool isConstantEmptyCache();
	    bool canBeFreed();
	
	    static size_t bytesForCapacity(uint32_t cap);
	    static struct bucket_t * endMarker(struct bucket_t *b, uint32_t cap);
	
	    void expand();
	    void reallocate(mask_t oldCapacity, mask_t newCapacity);
	    struct bucket_t * find(cache_key_t key, id receiver);
	
	    static void bad_cache(id receiver, SEL sel, Class isa) __attribute__((noreturn));
	};
  • Cache_t is a structure, which is equivalent to a cache. The cache is a method, which determines the number of bytes it occupies according to the size of the space contained in its own structure. (Cache_t is a structure, not a pointer to a structure, Class is a pointer to a structure, and it occupies 8 bytes)
	struct cache_t {
    
    
		struct bucket_t *_buckets; // 8
    	mask_t _mask;              // 4
    	mask_t _occupied;          // 4
    }

To be continued

Guess you like

Origin blog.csdn.net/Forever_wj/article/details/108633958