The structure of cache_t

The caching flow for cache_t


  • if (! cls->isInitialized()) return; Checks if the class initializes the object and returns if it does not.
  • if (cache_getImp(cls, sel)) return; Search the imp from the current cache and return it directly.
  • cache_t *cache = getCache(cls); The original cache_t structure is retrieved from the current CLS.
  • cache_key_t key = getKey(sel); Sel gets his key and forces it to be cache_KEY_t.
  • mask_t newOccupied = cache->occupied() + 1; Get the occupied of the current class and +1.
  • mask_t capacity = cache->capacity(); Read the current class’s capacity(current cache capacity).
  • Cache ->isConstantEmptyCache() : specifies whether the cache is created for the first time.
  • NewOccupied: <= Capacity / 4 * 3; elseIf newOccupied: <= Capacity / 4 * 3 To expand ().
  • bucket_t *bucket = cache->find(key, receiver); Find the bucket through the key and the incoming receiver.
  • if (bucket->key() == 0) cache->incrementOccupied(); The current bucket key is occupied with 0; if yes, it is occupied with 1;
  • bucket->set(key, imp); Put the key and IMP into the bucket.

Capacity expansion code parsing:

  • uint32_t oldCapacity = capacity(); Get the previous cache capacity.
  • uint32_t newCapacity = oldCapacity ? oldCapacity2 : INIT_CACHE_SIZE; Determine if the previous capacity has a value, if so2. If not, initialize capacity 4
  • reallocate(oldCapacity, newCapacity); Create a new cache and clear the old cache.

Let’s go to RealLocate and see the detailed source code:

The process for validating CACHE_T

The code inside main:


Validation of the metaclass cache_t

conclusion

OC instance methods are cached in cache_t, and class methods are cached in metaclasses (same steps, just look them up in metaclasses). 2. The essence of the cache is a Hash table. 3. If the number of masks is full, the system expands the capacity to double the original capacity. This is because the read and write cache is not secure and also for maximum efficiency