Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

说一下 Runtime 的方法缓存?存储的形式、数据结构以及查找的过程? #59

Open
tbfungeek opened this issue Feb 9, 2020 · 0 comments

Comments

@tbfungeek
Copy link
Owner

objc_class 结构体中包含了方法缓存的存储信息cache_t类型的cache属性。

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
};

cache_t结构如下所示:

struct cache_t {
    struct bucket_t *_buckets; // 散列表
    mask_t _mask; //散列表的长度 - 1
    mask_t _occupied; // 已经缓存的方法数量
}

里面最关键的属性为_buckets它实际上是一个增量扩展的哈希表结构,哈希表内部存储的是一个个bucket_t:

struct bucket_t {
private:
    MethodCacheIMP _imp;
    cache_key_t _key;
}

每个bucket_t 包含了key以及对应的函数实体地址IMP.每次查找方法的时候会将_mask与传入的key进行&操作后,作为索引值,如果发生冲突cache_next方法将索引值减1继续搜索。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant